0

I am developing an android application with BLE. The requirement of this application is to update the voltage variation in a specific hardware with various inputs.

So I am writing the characters to the BLE as 8-bit input. Each bit value contains its own representations. Based on each request hardware will respond and provide various output combinations. The output contains 24 bytes of information. Each byte position represents different value. eg: position 1& 2 represent current, 3 & 4 represent voltage etc. My problem here is, I am getting the output as 4 parts. Each message contains 6 bytes. Is it possible to get the same in a single message?

Implementation

 public void writeCharacteristic(BluetoothGattCharacteristic characteristic) {
        if (mBluetoothAdapter == null || mBluetoothGatt == null) {                      //Check that we have access to a Bluetooth radio
            Log.w(TAG, "BluetoothAdapter not initialized");
            return;
        }
        int test = characteristic.getProperties();                                      //Get the properties of the characteristic
        if ((test & BluetoothGattCharacteristic.PROPERTY_WRITE) == 0 && (test & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) == 0) { //Check that the property is writable
            return;
        }
        DebugLogs.writeToFile("BLE MODULE Before Write " + characteristic);
        if (mBluetoothGatt.writeCharacteristic(characteristic)) {                       //Request the BluetoothGatt to do the Write
            Log.v(TAG, "****************WRITE CHARACTERISTIC SUCCESSFULL**" + characteristic);                               //The request was accepted, this does not mean the write completed
            DebugLogs.writeToFile("BLE MODULE AFTER Write SUCCESS " + characteristic);
        } else {
            Log.d(TAG, "writeCharacteristic failed");                                   //Write request was not accepted by the BluetoothGatt
            DebugLogs.writeToFile("BLE MODULE AFTER Write FAIL " + characteristic);
        }
    }

And the response is getting in the Gatt callback

@Override
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            Log.w(TAG, "**ACTION_DATA_AVAILABLE**" + characteristic.getUuid());//Indication or notification was received
            broadcastUpdate(BLEConstants.ACTION_DATA_AVAILABLE, characteristic);                     //Go broadc

ast an intent with the characteristic data }

Nithinjith
  • 1,775
  • 18
  • 40

1 Answers1

0

Unfortunately not. While you could reduce the number of transmissions IF you are the author of the code in the BLE peripheral, you cannot fit 24 bites into a single characteristic, because BLE limits the width of a characteristic to 20 bytes. If you are the BLE peripheral author, you could perhaps change it to send 2 12 byte packets.

If you aren't, then you are probably trying to collect all the data before sending it. An easy way to do this would be create a byte array of length 24 when onCharacteristicWrite is called. Then, every time onCharacteristicChanged is called, add the data to the array. Once you've added all 24 bytes to the array, broadcast it.

Hope this helps!

spurrkins
  • 126
  • 2
  • 11
  • Ok, Thanks for your answer. – Nithinjith Aug 27 '16 at 04:55
  • 1
    I think the BLE specification allows a maximum of 512 bytes for a characteristic, can you point me to the specification section which mentions there is a 20 byte limitation or is it an android specific limitation? – bare_metal Aug 27 '16 at 05:47
  • I believe that you may be able to get packets that large if you change the MTU size, but only if both devices involved can support that. Also, I think that increasing the MTU just makes it break it up into more packets behind the scenes. [Here is some more info,](https://community.nxp.com/thread/332030) and [here is the android spec for changing MTU.](https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#requestMtu%28int%29) – spurrkins Aug 29 '16 at 16:53
  • It should be noted that unless OP has control of the code on the peripheral, a request to change the MTU will either be denied, or it won't actually help. Most likely. – spurrkins Aug 29 '16 at 17:00