1

This code is working and LED is also turning off.

                byte cmd[] = {(byte) 0xff};
                mWrChar.setValue(cmd);
                mBleGatt.writeCharacteristic(mWrChar);

But I want to pass "0x0801000" as byte array in BluetoothGattCharacteristic, how to do this?

Similar nRF Connect application.

enter image description here

Chitrang
  • 5,097
  • 1
  • 35
  • 58

2 Answers2

2

You can just pass an array to the variable cmd. But you need to know if the byte array is MSO (most significant octet) -> LSO (least significant octet) or LSO -> MSO. Typically, characteristics use LSO -> MSO, which means that the first octet in your byte array is the least significant octet.

In the concrete case, note that your characteristic is composed of four bytes: 0x08|01|00|00

Then you have:

MSO -> LSO: 0x08|01|00|00 -> {0x08, 0x01, 0x00, 0x00}

LSO -> MSO: 0x08|01|00|00 -> {0x00, 0x00, 0x01, 0x08}

Check out which one is relevant in your case, or try out both and see what happens. Your code will then be something like this (I assume LSO -> MSO):

byte[] cmd = {0x00, 0x00, 0x01, 0x08};
mWrChar.setValue(cmd);
mBleGatt.writeCharacteristic(mWrChar);
Thern
  • 1,017
  • 8
  • 17
  • Thanks for the solution. I have tried the solution in question and another solution from https://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java. But in both case after writeCharacteristic device gets disconnected with status code 8 in onConnectionStateChange call back. – Chitrang Jan 30 '18 at 17:01
  • Status code 8 is a connection timeout. That can have many reasons, but obviously your remote device is not responding. It is hard to diagnose why. So if you just pass 0x00 (a single byte) instead of a byte array with everything else identical, does it work then? – Thern Jan 30 '18 at 17:09
  • Yes, status code 8 seems difficult to debug. And yest with single byte 0xff and multiple byte of 0x08010000 have same result of disconnection with paired device. – Chitrang Jan 30 '18 at 17:26
  • Sorry for the question, but are you really sure you have a connection at that point when you send the write request? – Thern Jan 31 '18 at 08:12
  • I am definitely sure about it. And now that I have also tried with this https://github.com/NordicSemiconductor/Android-nRF-Blinky code and try to pass same code "0x08010000" and then same result occurs. – Chitrang Feb 01 '18 at 04:30
  • Do you have access to the operation of the remote device? Can you see what it does? There may be an authorization necessary for the write request, and if it never comes, there will be no response. Or the device may wait for another sort of input. Please check that the device on the other side receives the write request and is not hung up or waiting for input or whatever. If this is possible, that means. – Thern Feb 01 '18 at 08:23
0

This is the method I use to format a hex data, like yours 0x08010000, as a byte array. Then you can write it to characteristics.

public static byte[] hexToByteData(String hex)
{
    byte[] convertedByteArray = new byte[hex.length()/2];
    int count  = 0;

    for( int i = 0; i < hex.length() -1; i += 2 )
    {
        String output;
        output = hex.substring(i, (i + 2));
        int decimal = (int)(Integer.parseInt(output, 16));
        convertedByteArray[count] =  (byte)(decimal & 0xFF);
        count ++;
    }
    return convertedByteArray;
}

Hope it helps.

gbossa
  • 377
  • 1
  • 3
  • 18