I am working on transmitting the contents of a byte array from an Android mobile app to an MCU. I am able to successfully transfer the data byte by byte (multiple packets) but I am unable to successfully send the array as a whole (as one packet). It should be noted that the data will be transmitted via a GATT profile and that the array is successfully passed to this portion of the code.
public void writeCustomUsernameCharacteristic(byte[] byteArray) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString("00002a37-0000-1000-8000-00805f9b34fb"));
if (mCustomService == null) {
Log.w(TAG, "Custom BLE Service not found");
return;
}
mBluetoothGatt.requestMtu(244);
for (int i = 0; i < credentials.length; i++) {
individualBytes = byteArray[i];
BluetoothGattCharacteristic mWriteCharacteristic = mCustomService.getCharacteristic(UUID.fromString("00002902-0000-1000-8000-00805f9b34fb"));
mWriteCharacteristic.setValue(individualBytes, BluetoothGattCharacteristic.FORMAT_UINT8, 0);
mBluetoothGatt.writeCharacteristic(mWriteCharacteristic);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
System.out.println("got interrupted!");
}
if (mBluetoothGatt.writeCharacteristic(mWriteCharacteristic) == false) {
Log.w(TAG, "Failed to write characteristic");
}
}
}
However, if I try to set the value to the byte array itself I am unable to send the information. It should be noted that there are no errors being reported on the app side and the MCU side isn't reporting any packets being received.
public void writeCustomUsernameCharacteristic(byte[] byteArray) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString("4880c12c-fdcb-4077-8920-a450d7f9b907"));
if (mCustomService == null) {
Log.w(TAG, "Custom BLE Service not found");
return;
}
mBluetoothGatt.requestMtu(244);
BluetoothGattCharacteristic mWriteCharacteristic = mCustomService.getCharacteristic(UUID.fromString("fec26ec4-6d71-4442-9f81-55bc21d658d6"));
mWriteCharacteristic.setValue(byteArray);
mWriteCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
mBluetoothGatt.writeCharacteristic(mWriteCharacteristic);
}
Could anyone provide on any suggestions on how to transmit this byte array in on packet? Thanks in advance.
In response to the possible duplicate. It appears that the link is referencing code I have already attempted in the second block. The problem is that the mBluetoothGatt.writeCharacteristic(mWriteCharacteristic); is not sending the packets to the MCU.
public void writeCustomUsernameCharacteristic(byte[] byteArray) {
if (mBluetoothAdapter == null || mBluetoothGatt == null) {
Log.w(TAG, "BluetoothAdapter not initialized");
return;
}
BluetoothGattService mCustomService = mBluetoothGatt.getService(UUID.fromString("4880c12c-fdcb-4077-8920-a450d7f9b907"));
if (mCustomService == null) {
Log.w(TAG, "Custom BLE Service not found");
return;
}
mBluetoothGatt.requestMtu(244);
BluetoothGattCharacteristic mWriteCharacteristic = mCustomService.getCharacteristic(UUID.fromString("fec26ec4-6d71-4442-9f81-55bc21d658d6"));
mWriteCharacteristic.setValue(byteArray);
mWriteCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
mBluetoothGatt.writeCharacteristic(mWriteCharacteristic);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
System.out.println("got interrupted!");
}