0

I'm trying to writeCharacteristic() to a BLE device.

I used Google example app to establish the connection, the connection is fine. I can see the BLE Services and the services Characteristics.

The writeCharacteristic() method return true and the onCharacteristicWrite() callback return status BluetoothGatt.GATT_SUCCESS but nothing happening with the device: I tried all the solutions in stack overflow and nothing helps. Here is my code:

In the BluetoothLeService class i have sendData() method, which gets a byte[] I'm sending each command separately because of the maximum payload of 33 bytes.

public void sendData(byte[] data) {

        String lService = "71387664-eb78-11e6-b006-92361f002671";
        String lCharacteristic = "71387663-eb78-11e6-b006-92361f002671";

        BluetoothGattService mBluetoothLeService = null;
        BluetoothGattCharacteristic mBluetoothGattCharacteristic = null;

        if (mBluetoothGattCharacteristic == null) {
            mBluetoothGattCharacteristic = mBluetoothGatt.getService(UUID.fromString(lService)).getCharacteristic(UUID.fromString(lCharacteristic));

        }

        mBluetoothGattCharacteristic.setValue(data);

        boolean write = mBluetoothGatt.writeCharacteristic(mBluetoothGattCharacteristic);
    }
Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
Igor Fridman
  • 1,267
  • 1
  • 16
  • 30
  • Are you sure the data isn't arrived at the peripheral? It seems you have done everything correct at the Android side. If you receive GATT_SUCCESS that means the remote end has answered with a Write Response. – Emil Aug 21 '17 at 16:16

3 Answers3

0

In your code you check property of characteristics? If Write something on device then characteristics must have write property or write no response property or write & notify property. So Check in your code on which characteristics you are write.

Joshi Tushar
  • 116
  • 9
0

Your code looks OK, if you've got GATT_SUCCESS it means that characteristic was written successfully. You can also try to read this characteristic and check if value is updated. Also it's possible that characteristic was written successfully but corresponded device's functionality will be triggered only after disconnection from device. And also it's possible that there is some bug on a device side.

Dr.Lean
  • 171
  • 5
0

I found the issue. Hope my answer will help others. The issue was that, I was sending byte[] array bigger than it's allow(20 byte). Also, I needed to add "\r" inside this byte[] array, thats way, the BLE Devise knows it's the end of the command. Another thing, I needed to create a queue and pop from it after I get response in onCharacteristicRead().

class DataQueues {  

        private ArrayList<byte[]> queueArray;
        private int queuSize;   

        protected DataQueues() {
            queueArray = new ArrayList<>();
        }    
        protected void addToQueue(byte[] bytesArr) {
            queueArray.add(bytesArr);    
        }  

        protected byte[] popQueue() {    
            if (queueArray.size() >= 0)
                return queueArray.remove(0);
            else {
                return null;
            }    
        }    
        protected int getArrSize() {
            return queueArray.size();
        }
    }

Hope this will help some one.

Igor Fridman
  • 1,267
  • 1
  • 16
  • 30