0

I am developing app which sets time on the BLE device. For that to work I need to send array of values bigger that 127 (Values like 239,254, etc). But BLE setting value for characteristics supports byte array which has limit of value 128. So how can I send bigger values than 128 to BLE device from Android Device? PFA the code snippet, but it doesn't work. View Code Snippet

public static boolean writeble_Set_time_Healthyu(BluetoothGatt mBluetoothGatt) {
    try {

        Calendar calendar = Calendar.getInstance();
        Date date = new Date();
        calendar.setTime(date);

        byte[] list_data = new byte[15];

        list_data[0] = new Integer(Integer.parseInt(Integer.toString(254, 16), 16)).byteValue();
        list_data[1] = new Integer(Integer.parseInt(Integer.toString(239, 16), 16)).byteValue();
        list_data[2] = new Integer(Integer.parseInt(Integer.toString(1, 16), 16)).byteValue();
        list_data[3] = new Integer(Integer.parseInt(Integer.toString(250, 16), 16)).byteValue();
        list_data[4] = new Integer(Integer.parseInt(Integer.toString(7, 16), 16)).byteValue();
        list_data[5] = new Integer(Integer.parseInt(Integer.toString(calendar.get(Calendar.HOUR), 16), 16)).byteValue();
        list_data[6] = new Integer(Integer.parseInt(Integer.toString(calendar.get(Calendar.MINUTE), 16), 16)).byteValue();
        list_data[7] = new Integer(Integer.parseInt(Integer.toString(calendar.get(Calendar.SECOND), 16), 16)).byteValue();
        list_data[8] = new Integer(Integer.parseInt(Integer.toString(calendar.get(Calendar.DATE), 16), 16)).byteValue();
        list_data[9] = new Integer(Integer.parseInt(Integer.toString(calendar.get(Calendar.MONTH) + 1, 16), 16)).byteValue();
        list_data[10] = new Integer(Integer.parseInt(Integer.toString(Integer.parseInt((calendar.get(Calendar.YEAR) + "").substring(0, 1)), 16), 16)).byteValue();
        list_data[11] = new Integer(Integer.parseInt(Integer.toString(Integer.parseInt((calendar.get(Calendar.YEAR) + "").substring(2, 3)), 16), 16)).byteValue();
        list_data[12] = new Integer(Integer.parseInt(Integer.toString(Integer.parseInt((calendar.get(Calendar.YEAR) + "").substring(2, 3)), 16), 16)).byteValue();
        list_data[13] = new Integer(Integer.parseInt(Integer.toString(0, 16), 16)).byteValue();
        list_data[14] = new Integer(Integer.parseInt(Integer.toString(165, 16), 16)).byteValue();

        List<BluetoothGattService> serviceList = mBluetoothGatt.getServices();
        for (BluetoothGattService service : serviceList) {
            List<BluetoothGattCharacteristic> characteristics = service.getCharacteristics();
            for (BluetoothGattCharacteristic characteristic : characteristics) {

                characteristic.setWriteType(FORMAT_UINT32);
                characteristic.setValue(list_data);
                boolean status = mBluetoothGatt.writeCharacteristic(characteristic);
            }
        }
        return true;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}
  • Please do not post text and source code as image. [Edit] your question, copy, paste and format the code properly. Also add further information and error messages why your code snippet "doesn't work". – Michael Kotzjan Nov 09 '21 at 11:33
  • @MichaelKotzjan there no error as other ble device reads value successfully. But to set the time, array should start with 254. But while sending the value 254 is converted to signed byte value which is less than 127. So other ble device considers that as garbage value. And by doesn't work I meant time is not setting properly, communication is successful, but ble device do not get the required values to set the time. – Nitin Kabra Nov 09 '21 at 11:38
  • What does it matter if it is signed or unsigned? You receive bytes. Every byte is eight bits. You can interprete those bits as you want. So the receiver should handle them like it should. To check what you actually send and receive log the byte values in hexadecimal notation. Both when sending and receiving. The hexadecimal notation should be equal. And if they are equal well be happy.. – blackapps Nov 09 '21 at 11:47
  • bytes are apparently signed in java https://stackoverflow.com/a/7401635/7473793 – Michael Kotzjan Nov 09 '21 at 11:53
  • `list_data[0] = new Integer(Integer.parseInt(Integer.toString(254, 16), 16)).byteValue(); `. That is incredible complicated code. Try: `list_data[0] = 0xFE;` – blackapps Nov 09 '21 at 11:53
  • @blackapps Actually I am trying to send the values, and firmware is already developed for ble device which reads 254, 239 values. So I can't really change the firmware, values has to sent as 254, 239, etc. – Nitin Kabra Nov 09 '21 at 11:54
  • @blackapps Hey, I tried both ways (even this, list_data[0] = 0xFE;), but value that is sent to Ble device is -2, -17 and so on. Needs to be 254, 239 – Nitin Kabra Nov 09 '21 at 11:58
  • @MichaelKotzjan, actually that's my question, how can I send unsigned values? – Nitin Kabra Nov 09 '21 at 11:59
  • Could you try `list_data[0] = 0xFE & 0xFF;` – Michael Kotzjan Nov 09 '21 at 12:00
  • `characteristic.setWriteType(FORMAT_UINT32);` ??? 32 is for four bytes of an integer ... You should have a format for bytes. Does it have any effect if you choose a different format? – blackapps Nov 09 '21 at 12:01
  • @MichaelKotzjan Everything is type casted into byte so the values it won't help. – Nitin Kabra Nov 09 '21 at 12:04
  • @blackapps Actually it do not have any effect have being trying multiple formats, non of those work. – Nitin Kabra Nov 09 '21 at 12:05
  • 1
    Please print your list_data array in hexadecimal notation just before sending. Please tell the results. We wanna know what is in the array. If you try to put 0xFE in it you should check if 0xFE is in it while printing and before sending. – blackapps Nov 09 '21 at 12:07
  • @blackapps [-2, -17, 1, -6, 7, 5, 26, 28, 9, 11, 2, 2, 2, 0, -91] these are what values are sent to ble device – Nitin Kabra Nov 09 '21 at 12:10
  • 1
    Well that is signed decimal notation. I asked you to print the values in hexadecimal notation. Please do. – blackapps Nov 09 '21 at 12:10
  • @blackapps 0 = {Integer@10109} 254 1 = {Integer@10110} 239 2 = {Integer@10111} 1 3 = {Integer@10112} 250 4 = {Integer@10113} 7 5 = {Integer@10114} 5 6 = {Integer@10115} 46 7 = {Integer@10116} 11 8 = {Integer@10117} 9 9 = {Integer@10118} 11 10 = {Integer@10119} 2 11 = {Integer@10120} 2 12 = {Integer@10121} 2 13 = {Integer@10122} 0 14 = {Integer@10123} 165 These are the values by doing list_data[0] = 0xFE & 0xFF;, before converting it to byte – Nitin Kabra Nov 09 '21 at 12:17
  • ???????????????? What are you doing? We are not interested in things before conversion. What you have is a byte array. Now after you have initiated that array and just before sending print the hexadecimal byte values of that array. So add some code to do so just before `List – blackapps Nov 09 '21 at 12:20
  • @blackapps here is the hex values "FE EF 01 FA 07 05 37 21 09 0B 02 02 02 00 A5 " – Nitin Kabra Nov 09 '21 at 12:26
  • 1
    Well then nice! That is exactly what you did put in it isn't it? Why didn't you comment? Now where do things go wrong? Two possibilities: With `characteristic.setValue(list_data);` or while executing `mBluetoothGatt.writeCharacteristic(characteristic)`. Cant you do something like `mBluetoothGatt.write(list_data);`? – blackapps Nov 09 '21 at 12:37
  • Unfortunately No, there is only one way to write to characteristics i.e. .setValue() according to Android documentation https://developer.android.com/reference/android/bluetooth/BluetoothGattCharacteristic let me know if I am missing something. – Nitin Kabra Nov 09 '21 at 12:41
  • Why dont you use a BluetoothSocket? – blackapps Nov 09 '21 at 13:13

0 Answers0