Android: Sending data >20 bytes by BLE

I am able to send data upto 20 bytes by connecting to an external BLE device. How do I send data greater than 20 bytes. I have read that we have to either fragment the data or split characteristic to required parts. If I assume my data is 32 bytes, could you tell me changes I need to make in my code to get this working? Following are the required snippets from my code:

public boolean send(byte[] data) {
if (mBluetoothGatt == null || mBluetoothGattService == null) {
    Log.w(TAG, "BluetoothGatt not initialized");
    return false;
}

BluetoothGattCharacteristic characteristic =
        mBluetoothGattService.getCharacteristic(UUID_SEND);

if (characteristic == null) {
    Log.w(TAG, "Send characteristic not found");
    return false;
}

characteristic.setValue(data);
characteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
return mBluetoothGatt.writeCharacteristic(characteristic);
}

This is the code I used for sending the data. The "send" function is used in the following onclick event.

sendValueButton = (Button) findViewById(R.id.sendValue);
sendValueButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String text = dataEdit.getText().toString();                           
        yableeService.send(text.getBytes());
    }
    });

When the String text is greater than 20 bytes then only the first 20 bytes are received. How to rectify this?

To test sending multiple characteristics I tried this:

sendValueButton = (Button) findViewById(R.id.sendValue);
sendValueButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    String text = "Test1";                           
    yableeService.send(text.getBytes());

    text = "Test2";                           
    yableeService.send(text.getBytes());

    text = "Test3";                           
    yableeService.send(text.getBytes());
}
});

But I only received "Test3". What mistake did I commit? I am new to BLE so please ignore any naiveness.

  • Hi Ankit, Seems that on Android long write is not supported, you would need to split your large data in to chunks of 20 bytes packet.

    You would need to make sure the packet is queued correctly before you send the next packet. First you have to check for the return code of BluetoothGatt.writeCharacteristic(). Second you would need to wait for the call back BluetoothGattCallback: onCharacteristicWrite() before you queue the next packet.

  • I got this working finally. onCharacterisiticWrite() was important but if we only write and don't read from the firmware, then writing is a very fast process and after implementing this too I only get the last part. The trick for this is Thread.sleep(150). Delay the process by 150 milliseconds and it works! Thanks for the guidance.

  • Hi Ankit, I've a problem with the simple write data throught characteristic. I can't do that. I need just transmit a simple byte. I'm guiding by android LeGatt sample project. But I've try a lot of things and doesn't works. The problem is that I'm not a expert android programmer and also not exist good documentation about the matter. If you can share the project, it will be so useful for me. Or if you can explain me how can I do the write characteristic. I'm really frustrated by this. Please, help me. My email is orlandoperalta23@gmail.com if you need it

  • The requestMtu method has been added in the Android API 21. @hungbui could you please confirm if any SoftDevice support larger MTU (the ATT_OP_MTU_REQ opcode 0x02 according to the spec) ?

    According to this commit of the Android-nRF-Toolbox project, long writes are supported by the Android BLE stack if the write type is set to WRITE_TYPE_DEFAULT (ATT write request).

  • Hey can I get this working for iOS..??, I'm able to send data to BLE but on Serial printer it shows the partial data only.

Related