I have the following error due to the problem with writing data to BLE device - due to the issue with the BLE device for example:
After 4 retries application crasshes.
I had a look at the BLE2 code:
I thought that it would be better way of handling of writing error than throwing RuntimeException and crashing the app after number of retries!
Edit:
I noticed that there is an event raised in callback function onCharacteristicWrite(), with the status of the write process:
So do we need to have a RuntimeException?
B4X:
retries: 4
retries: 3
retries: 2
retries: 1
java.lang.RuntimeException: Error writing data to: 00001f1f-0000-1000-8000-00805f9b34fb
at anywheresoftware.b4a.objects.BleManager2.WriteData(BleManager2.java:426)
at b4a.ble3.starter._writedata(starter.java:829)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
at anywheresoftware.b4a.keywords.Common.CallSub4(Common.java:1082)
at anywheresoftware.b4a.keywords.Common.CallSubNew2(Common.java:1037)
at b4a.ble3.main._bgetname_click(main.java:1356)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:197)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
After 4 retries application crasshes.
I had a look at the BLE2 code:
Java:
/**
* Writes the data to the specified characteristic.
*/
public void WriteData(String Service, String Characteristic, byte[] Data) throws InterruptedException {
BluetoothGattCharacteristic chr = getChar(getService(Service), Characteristic);
chr.setValue(Data);
int retries = 5;
while (true) {
if (!gatt.writeCharacteristic(chr)) {
if (--retries <= 0)
throw new RuntimeException("Error writing data to: " + Characteristic);
}
else
break;
BA.Log("retries: " + retries);
Thread.sleep(150 * (5 - retries));
}
}
Edit:
I noticed that there is an event raised in callback function onCharacteristicWrite(), with the status of the write process:
Java:
public void onCharacteristicWrite(BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic, int status) {
if (characteristic == null || characteristic.getUuid() == null)
return;
ba.raiseEventFromDifferentThread(BleManager2.this, null, 0, eventName + "_writecomplete", false,
new Object[] {characteristic.getUuid().toString(), status});
}
Last edited: