I have an android application that writes data to a simblee bluetooth.
Below the code I have :
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);
}
Everything works correctly, (reliableWrite wasn't working for some reason but that's for another question)
Now if I want to stream data (for example onTouch event send the led to light in realtime)
The only solution that's working with me, is saving the data to stream in an arraylist, and OnCharacteriticWrite
send the next data to send.
@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);
}
This is working for me, but I'm sure there must be a more reliable way to send the data nut I couldn't find any proper documentation for it (in my code any error that happens during streaming will stop the whole stream)