我正在尝试从“蓝牙健康设备”(例如血氧计、温度计、称重机等)捕获 Android 手机上的(数据)读数。我可以将设备连接到手机,但是当它读取数据时它卡住了只在那边。我正在执行以下步骤..(Android 2.2)首先我正在创建套接字连接
BluetoothSocket Socket.connect();// for socket connection-----done successfully
BluetoothSocket tmp= device.createRfcommSocketToServiceRecord(MY_UUID);
//这里 MY_UUID="00001101-0000-1000-8000-00805F9B34FB";在阅读线程中
byte[] buffer = new byte[37];// i think here I am not getting exact bytes
int bufferBytes = 0, bufferIndex = 0;
Log.e(TAG, "MESSAGE_READ...."); Communications comms = null; Bundle bundle = new Bundle(); try { comms = new Communications(); Log.e(TAG, "After Communications...."); } catch (Exception e) { Log.e(TAG, "Error in communicaton...."); } // Keep listening to the InputStream while connected while (true) { Log.e(TAG, "Its coming in while"); try { try { bufferBytes = mmInStream.read(buffer);// the code stuck here its not going down till health device get off(its directly jump to exception after device get off) } catch (IndexOutOfBoundsException e) { Log.e(TAG, "bufferBytes error ====== +e.getMessage()); } //Log.e(TAG, "bufferBytes====== "+bufferBytes); while (bufferBytes > bufferIndex) { String[] message = comms.receiveMessage(buffer, bufferIndex, bufferBytes); bufferIndex = Integer.parseInt(message[1]); Log.e(TAG, "bufferIndex====== "+bufferIndex); if (message[0] != null) { /* * Processing message sent by device */ StringTokenizer dataInTokens = new StringTokenizer( message[0]); if (dataInTokens.hasMoreTokens() == true) { String token = dataInTokens.nextToken(); Log.e(TAG, "token====== "+token); if (token.equals("a")) { int weight = 0; if (dataInTokens.hasMoreTokens() == true) { weight = Integer.parseInt(dataInTokens .nextToken()); // Send a message back to the Activity msg = mHandler .obtainMessage(ScaleActivity.MESSAGE_READ); bundle.putInt(ScaleActivity.WEIGHT, weight); msg.setData(bundle); mHandler.sendMessage(msg); } } else if (token.equals("b")) { int weight = 0; if (dataInTokens.hasMoreTokens() == true) { weight = Integer.parseInt(dataInTokens .nextToken()); // Send a message back to the Activity msg = mHandler .obtainMessage(ScaleActivity.MESSAGE_READ); bundle.putInt( ScaleActivity.WEIGHT_TO_SAVE, weight); msg.setData(bundle); mHandler.sendMessage(msg); } } } } }
我还尝试了 Android 4.0 HDP 示例。它可以在此处获得, 但我们的健康设备不兼容 hdp。所以它也不适合我。