从Android到蓝牙实时传输数据的最佳实践

我有一个android应用程序,将数据写入simblee蓝牙

下面的代码我有:

public void sendData(String data) { Utils.addLog(TAG,"sending data : " + data); if (mSerialCharacterstic == null) { Utils.addLog(TAG,"serial characteristic not found yet!"); return; } Utils.addLog(TAG,"starting write data..."); mSerialCharacterstic.setValue(data); Utils.addLog(TAG,"writing : " + data); // mGatt.beginReliableWrite(); mGatt.writeCharacteristic(mSerialCharacterstic); } 

/ ******************* /

  @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { Utils.addLog(TAG,"onCharacteristicWrite : " + new String(characteristic.getValue()) + " status : " + status); // gatt.executeReliableWrite(); Utils.addLog(TAG,"data written"); super.onCharacteristicWrite(gatt, characteristic, status); } 

一切工作正常,(可靠的Write没有工作的原因,但这是另一个问题)

现在,如果我想stream数据(例如onTouch事件实时发送指示灯)

唯一与我合作的解决scheme是将数据保存在一个OnCharacteriticWrite ,然后OnCharacteriticWrite发送下一个数据发送。

  @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { Utils.addLog(TAG,"onCharacteristicWrite : " + new String(characteristic.getValue()) + " status : " + status); // gatt.executeReliableWrite(); Utils.addLog(TAG,"data written"); if (isStreaming && streamQueue.size() > 0) { streamQueue.remove(0); Utils.addLog(TAG,"item removed"); Utils.addLog(TAG,"stream size : " + streamQueue.size()); if (streamQueue.size() > 0) { Utils.addLog(TAG,"streaming next item"); sendData(streamQueue.get(0)); } else { Utils.addLog(TAG,"no more items to stream"); didSendFirstSet = false; } } super.onCharacteristicWrite(gatt, characteristic, status); } 

这是为我工作,但我敢肯定,必须有一个更可靠的方式来发送数据螺母我找不到任何适当的文件(在我的代码中发生在stream中的任何错误将停止整个stream)

Solutions Collecting From Web of "从Android到蓝牙实时传输数据的最佳实践"