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

Why BLE Apps receive 1 byte only From NRF52840DK

I am using nrf52840 . I am sending data  2 bytes as FF .I put the data in 2d array like this.

char dataSend[1][5]={"FF"};
This char dataSend has 1 row and 5 length .This should send as 2 bytes .

for(int i=0;i<1;i++){
printf("&d",dataSend[i]); 
}

I already debug in terminal as it will print and send as 255 in decimal.


Android apps will prepare to receive the bytes data. But it fail to receive as 2 bytes.It will receive as single byte . So the byte data is

byte[0]=70
byte[1]=70

70 decimal
hex is F

I expected the data as
byte[0]=255.
255 decimal
hex is FF



private final BleManagerGattCallback mGattCallback = new BleManagerGattCallback() {
    @Override
    protected void initialize() {

        setNotificationCallback(mTXCharacteristic).with((device, data) -> 
        { 
        
        
        byte[]=data.getValue;
        //not receiving as 255 for 2 bytes
        //only receive as 46 as 1 bytes
            



            
            
       






        });

        requestMtu(260).enqueue();
        enableNotifications(mTXCharacteristic).enqueue();

    }

    @Override
    public boolean isRequiredServiceSupported(@NonNull final BluetoothGatt gatt) {
        final BluetoothGattService service = gatt.getService(LBS_UUID_SERVICE);
        if (service != null) {
            mRXCharacteristic = service.getCharacteristic(LBS_UUID_BUTTON_CHAR);
            mTXCharacteristic = service.getCharacteristic(LBS_UUID_LED_CHAR);
        }
        boolean writeRequest = false;
        boolean writeCommand = false;
        if (mRXCharacteristic != null) {
            final int rxProperties = mRXCharacteristic.getProperties();
            writeRequest = (rxProperties & BluetoothGattCharacteristic.PROPERTY_WRITE) > 0;
            writeCommand = (rxProperties & BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE) > 0;

            // Set the WRITE REQUEST type when the characteristic supports it.
            // This will allow to send long write (also if the characteristic support it).
            // In case there is no WRITE REQUEST property, this manager will divide texts
            // longer then MTU-3 bytes into up to MTU-3 bytes chunks.
            if (writeRequest)
                mRXCharacteristic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
            else
                mUseLongWrite = false;
        }

        return mRXCharacteristic != null && mTXCharacteristic != null && (writeRequest || writeCommand);
    }

    @Override
    protected void onDeviceDisconnected() {
        mRXCharacteristic = null;
        mTXCharacteristic = null;
        mUseLongWrite = true;
    }
};

  • Hi 

    When you are using quotations the data is treated as a null terminated ASCII string, and {"FF"} essentially becomes {70, 70, 0}. 

    To assign 0xFF to a variable you just use the hex representation directly, without the quotations:

    char dataSend[1][5]={0xFF};

    Please be aware that the compiler might complain about this, since the value of a normal char is -128 to 127, and a value of 255 would be outside this range. 
    You should use an unsigned char instead (or uint8_t in the nRF5 SDK):

    uint8_t dataSend[1][5] = {0xFF};

    I am not sure why you only receive a single byte on the Android side. Can you tell me which example you are using on the nRF52 side, and send me the code you use to send the data from the nRF52?

    Unless I missed it I can only see the Android code in your post. 

    Best regards
    Torbjørn

Related