ReadWriteListener not getting called when writing SHA-256 bytes into characteristics
ujjvaljoshi4 opened this issue · 3 comments
I have cloned repo from the main branch and update code for required changes in toolbox application. I have converted the string to hex bytes and then to SHA-256 bytes. I am trying to write SHA-256 bytes to characteristic using write(). The write() function is getting called but didn't get any success or failure callback in ReadWriteListener.
Here is the code from the example if it is required:
byte[] prefix = {(byte) 0xFF};
byte[] token = parseHexBinary("f17164e350e8044d47b8223ca9b961d1");
byte[] result = new byte[prefix.length + token.length];
System.arraycopy(prefix, 0, result, 0, prefix.length);
System.arraycopy(token, 0, result, prefix.length, token.length);
byte[] finalDigest256 = calculateSH256(result);
writeData(finalDigest256);
// Convert string to hex bytes
public static byte[] parseHexBinary(String s) {
final int len = s.length();
// "111" is not a valid hex encoding.
if (len % 2 != 0)
throw new IllegalArgumentException("hexBinary needs to be even-length: " + s);
byte[] out = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
int h = hexToBin(s.charAt(i));
int l = hexToBin(s.charAt(i + 1));
if (h == -1 || l == -1)
throw new IllegalArgumentException("contains illegal character for hexBinary: " + s);
out[i / 2] = (byte) (h * 16 + l);
}
return out;
}
private static int hexToBin(char ch) {
if ('0' <= ch && ch <= '9') return ch - '0';
if ('A' <= ch && ch <= 'F') return ch - 'A' + 10;
if ('a' <= ch && ch <= 'f') return ch - 'a' + 10;
return -1;
}
// Convert hex bytes to SHA-256 bytes
public static byte[] calculateSH256(byte[] bytes) {
final MessageDigest digest;
byte[] digestResult = new byte[0];
try {
digest = MessageDigest.getInstance("SHA-256");
digest.update(bytes);
digestResult = digest.digest();
} catch (Exception e) {
e.printStackTrace();
}
return digestResult;
}
public void writeData(byte[] data) {
m_device.write(new BleWrite(UUID.fromString("00000040-0001-1100-B007-00000067F000"), UUID.fromString("00000041-0001-1100-B007-00000067F000")).setWriteType(Type.WRITE).setBytes(data), e -> {
/////// NOT GETTING ANY CALLBACK HERE
if (e.wasSuccess()) {
// Success
} else {
// Fail
}
});
}
The service and characteristic UUIDs I have provided in the above code are a dummy to show the format of the UUIDs. It is used to authenticate BLE devices with a mobile application.
Current behavior: The ReadWriteListener is not getting called for either success or failure when performing the write operation on a characteristic.
Expected behavior: The ReadWriteListener needs to be called for either success or failure when performing the write operation on a characteristic.
@ryanhubbell Could you please help me with this?
The issue is fixed by updating the MTU size in the code.
Now getting GATT_ERROR 133 when trying to connect BLE device after successfully paired.
Here is the log from the Android studio:
2022-12-01 13:02:07.441 22470-22598 BluetoothGatt com.idevicesinc.sweetblue.toolbox D registerApp()
2022-12-01 13:02:07.441 22470-22598 BluetoothGatt com.idevicesinc.sweetblue.toolbox D registerApp() - UUID=1ca54f53-1c38-4470-a1df-ec9f8d7960ff
2022-12-01 13:02:07.446 22470-23408 BluetoothGatt com.idevicesinc.sweetblue.toolbox D onClientRegistered() - status=0 clientIf=16
2022-12-01 13:02:07.679 22470-22470 GestureDetector com.idevicesinc.sweetblue.toolbox I handleMessage TAP
2022-12-01 13:02:12.474 22470-22486 BluetoothGatt com.idevicesinc.sweetblue.toolbox D onClientConnectionState() - status=133 clientIf=16 device=5D:FA:A5:0A:C5:10
2022-12-01 13:02:12.518 22470-22486 P_BleDevic...r [Native] com.idevicesinc.sweetblue.toolbox W ABE(22486) onConnectionStateChange() [5D:FA:A5:0A:C5:10] - GATT_ERROR(133) STATE_DISCONNECTED(0)
2022-12-01 13:02:12.520 22470-22598 P_BleDevic...iveManager com.idevicesinc.sweetblue.toolbox I UPDATE(22598) updateNativeConnectionState() - STATE_DISCONNECTED(0)
2022-12-01 13:02:12.521 22470-22598 PA_Task com.idevicesinc.sweetblue.toolbox I UPDATE(22598) setState() - Connect(FAILED kd01rba1168_C510 148399736 ) - 3316
2022-12-01 13:02:12.521 22470-22598 P_TaskManager com.idevicesinc.sweetblue.toolbox I UPDATE(22598) print() - no current task []
2022-12-01 13:02:12.523 22470-22598 BluetoothGatt com.idevicesinc.sweetblue.toolbox D close()
2022-12-01 13:02:12.531 22470-22598 BluetoothGatt com.idevicesinc.sweetblue.toolbox D unregisterApp() - mClientIf=16
2022-12-01 13:02:12.537 22470-22598 P_ConnectionFailManager com.idevicesinc.sweetblue.toolbox W UPDATE(22598) onConnectionFailed() - NATIVE_CONNECTION_FAILED, timing=EVENTUALLY
2022-12-01 13:02:12.542 22470-22598 P_TaskManager com.idevicesinc.sweetblue.toolbox I UPDATE(22598) addTask() - Adding task to queue: Connect(CREATED kd01rba1168_C510 199015095 )
2022-12-01 13:02:12.543 22470-22598 P_TaskManager com.idevicesinc.sweetblue.toolbox I UPDATE(22598) print() - no current task [Connect(QUEUED kd01rba1168_C510 199015095 )]
2022-12-01 13:02:12.547 22470-22598 P_TaskManager com.idevicesinc.sweetblue.toolbox I UPDATE(22598) print() - Connect(EXECUTING kd01rba1168_C510 199015095 ) []
The second comment there looks to be a different issue. Please open a new issue if you haven't figured out the problem yet. Also, please be a bit more specific (does that happen every time? Have you tried bonding after getting connected?)
@ryanhubbell I opened a new issue for GATT_ERROR 133 with required details. Please check it out here.