@Override public void onReceive(Context context, Intent intent) { Log.v(TAG, "BluetoothReceiver onReceive"); if (intent.getAction().equals(OperationActivityHD.BLUETOOTHRECEIVER_ACTION)) { int cmd = intent.getIntExtra("cmd", -1); // switch (cmd) { case REQUIRE_CONNECT: // BluetoothService.addNewDevice=true; STOPFLAG = false; ADDRESS = intent.getStringExtra(OperationActivityHD.DEVICE_ADDRESS); BluetoothDevice device = mAdapter.getRemoteDevice(ADDRESS); Log.d(TAG, "Receiver get address----------->" + "--" + ADDRESS); BluetoothService.this.connect(device); break; case REQUIRE_WRITE: final byte[] command = intent.getByteArrayExtra("command"); // int value = intent.getIntExtra("value", 0); write(command); if (D) Log.d( TAG, "·¢ËÍÖ¸Áî---------->" + RokolUtil.bytesToHexString(command, command.length)); break; case 2: break; case 10: Log.d(TAG, "Close socket----------->"); break; } } }
/** * @author lmt @ there is a bug with Android InputStream it is reported as below: {@link http * ://stackoverflow.com/questions/12294705/error -in-reading-data- * from-inputstream-in-bluetooth-on-android?rq=1} */ public void run() { Log.i(TAG, "BEGIN mConnectedThread"); byte[] buffer = new byte[3]; byte[] buffers = new byte[13]; byte[] tempBuffer = new byte[100]; // StringBuilder stringbuilder=new StringBuilder(); int bytes = 0; int index = 0; int sum = 0; STOPFLAG = true; connectTime = 0; // Keep listening to the InputStream while connected while (true) { bytes = 0; try { // while (true) { int flagcount = 0; // bytes += mmInStream.read(buffer,bytes,12-bytes); bytes = mmInStream.read(buffer); // String a=Integer.toHexString(bytes); Log.d( TAG, "The first buffer.length is: " + bytes + ". buffer is: " + RokolUtil.bytesToHexString(buffer, bytes)); if (buffer[0] == (byte) 0xF1 || buffer[0] == (byte) 0xF5 || buffer[0] == (byte) 0xF2 || sum > 12) { index = 0; sum = 0; while (buffer[0] == (byte) 0xF1 && bytes < 13) { if (flagcount == 0) { System.arraycopy(buffer, 0, buffers, 0, bytes); flagcount++; } bytes += mmInStream.read(buffers, bytes, 13 - bytes); } if (buffer[0] == (byte) 0xF2 || buffer[0] == (byte) 0xF5) { if (bytes < 3) { Log.d(TAG, "------->bytes<3 read F2"); try { bytes += mmInStream.read(buffer, bytes, 3 - bytes); } catch (IOException e) { break; } } System.arraycopy(buffer, 0, buffers, 0, bytes); } Log.d( TAG, "---InStream.read() buffer is:> " + RokolUtil.bytesToHexString(buffers, bytes)); byte[] arr = new byte[bytes]; System.arraycopy(buffers, 0, arr, 0, bytes); for (int i = 0; i < bytes; i++) { if (i > 0 && (arr[i] == (byte) 0xF1 || arr[i] == (byte) 0xF2 || arr[i] == (byte) 0xF5)) { // Log.d(TAG, // "break i =" // + i // + (i > 0 && (arr[i] == (byte) 0xF1 // || arr[i] == (byte) 0xF2 || arr[i] == (byte) 0xF5))); break; } tempBuffer[i + index] = arr[i]; sum++; } // if (D) // Log.d(TAG, ""//mmInStream.available() // + " 2 mminsream---" + index + " " + sum // + " (tempBuffer[0]==(byte)0xF1)=" // + (tempBuffer[0] == (byte) 0xF1) // + " (tempBuffer[0]==(byte)0xF5)=" // + (tempBuffer[0] == (byte) 0xF5) // + " (tempBuffer[0]==(byte)0xF2)=" // + (tempBuffer[0] == (byte) 0xF2) + " sum=" // + sum); index = index + bytes; if (sum == 13 && tempBuffer[0] == (byte) 0xF1) { // Send the obtained bytes to the UI Activity Bundle bundle = new Bundle(); bundle.putInt(BUNDLE_TYPE, OperationActivityHD.MESSAGE_READ); bundle.putByteArray("buffer", tempBuffer); bundle.putInt("length", sum); showMessage(bundle); index = 0; sum = 0; } if (sum == 3 && (tempBuffer[0] == (byte) 0xF2 || tempBuffer[0] == (byte) 0xF5)) { Log.d(TAG, "return data sum=3" + RokolUtil.bytesToHexString(tempBuffer, sum)); // Send the obtained bytes to the UI Activity Bundle bundle = new Bundle(); bundle.putInt(BUNDLE_TYPE, OperationActivityHD.MESSAGE_READ); bundle.putByteArray("buffer", tempBuffer); bundle.putInt("length", sum); showMessage(bundle); index = 0; sum = 0; } } /* * else { SystemClock.sleep(100); } */ } catch (IOException e) { Log.e(TAG, "connectedthread---------disconnected", e); connectionLost(); // Start the service over to restart listening mode // BluetoothService.this.start(); 20150130 TRS break; } } }