This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

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.

Related