3

I want to read data from ble hardware continuously in android app via bluetooth. Connection has done i can send data from app to ble but cant read data from ble.

onCharactersticChanged method has to be called when getting some data from ble but this callback method is not calling. and i'm trying to notify to ble but

writeChar.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(BLEUtils.CLIENT_CHARACTERISTIC_CONFIG_UUID, BluetoothGattDescriptor.PERMISSION_WRITE_SIGNED);

descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);

gatt.writeDescriptor(descriptor);

  // Enable local notifications
        gatt.setCharacteristicNotification(writeChar, true);

 boolean succes = gatt.writeDescriptor(descriptor);

its returns false

Zoe
  • 27,060
  • 21
  • 118
  • 148
sushma1008
  • 125
  • 5
  • 14
  • what do you mean continuously? i think, ble is for transferring small amount of data. you can use it this way but you have to read chunk of data separately.[ for example](https://developer.android.com/guide/topics/connectivity/bluetooth-le.html#read) `format = BluetoothGattCharacteristic.FORMAT_UINT16; int heartRate = characteristic.getIntValue(format, 1);` – Mehran Zamani Feb 12 '17 at 10:12
  • yes but i am sending data in string and also receives same,problem is callback methods are not calling like onCharactersticChanged() and onCharacteristicRead() etc. – sushma1008 Feb 12 '17 at 18:10
  • put your code, not just some part. onCharactersticChanged and onCharacteristicRead – Mehran Zamani Feb 12 '17 at 18:58
  • i didn't get your answer can you ellaborate – sushma1008 Feb 13 '17 at 06:52
  • i mean send us your code, add it to your question. don't give us fractions – Mehran Zamani Feb 13 '17 at 06:54

2 Answers2

3

I suggest you should set the ENABLE_NOTIFICATION_VALUE when subscribing to the characteristics. This is my code to do this:

public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            List<BluetoothGattService> services = getSupportedGattServices();
            for (BluetoothGattService service : services) {
                if (!service.getUuid().equals(UUID_TARGET_SERVICE))
                    continue;

                List<BluetoothGattCharacteristic> gattCharacteristics =
                        service.getCharacteristics();

                // Loops through available Characteristics.
                for (BluetoothGattCharacteristic gattCharacteristic : gattCharacteristics) {
                    if (!gattCharacteristic.getUuid().equals(UUID_TARGET_CHARACTERISTIC))
                        continue;

                    final int charaProp = gattCharacteristic.getProperties();

                    if ((charaProp | BluetoothGattCharacteristic.PROPERTY_NOTIFY) > 0) {
                        setCharacteristicNotification(gattCharacteristic, true);
                    } else {
                        Log.w(TAG, "Characteristic does not support notify");
                    }
                }
            }
        } else {
            Log.w(TAG, "onServicesDiscovered received: " + status);
        }
    }

then you can subscribe to the characteristics with NOTIFICATION:

public void setCharacteristicNotification(BluetoothGattCharacteristic characteristic,
                                          boolean enabled) {
    if (mBluetoothAdapter == null || mBluetoothGatt == null) {
        Log.w(TAG, "BluetoothAdapter not initialized");
        return;
    }
    mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

    BluetoothGattDescriptor descriptor = characteristic.getDescriptor(UUID_DESCRIPTOR);
    descriptor.setValue(enabled?BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
                                :BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE);
    mBluetoothGatt.writeDescriptor(descriptor);

}

after this you can start writing to the device:

    @Override
    public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
        if (status == BluetoothGatt.GATT_SUCCESS) {
            if (descriptor.getCharacteristic().getUuid().equals(UUID_TARGET_CHARACTERISTIC)) {
                Log.i(TAG, "Successfully subscribed");
            }

            byte[] data = <Your data here>;
            BluetoothGattService Service = mBluetoothGatt.getService(UUID_TARGET_SERVICE);

            BluetoothGattCharacteristic charac = Service
            .getCharacteristic(UUID_TARGET_CHARACTERISTIC);

            charac.setValue(data);
            mBluetoothGatt.writeCharacteristic(charac);
        } else {
            Log.e(TAG, "Error subscribing");
        }
    }

And you will receive the callback onCharactersticChanged for reading, no need to actually call the read operation.

Remember mBluetoohGatt can only handle 1 operation at a time, if you execute another one while the previous one is unfinished, it won't put in queue, but will return false.

Jeff Hoang
  • 68
  • 6
1

If it returns false it usually means you already have a pending GATT operation. You need to structure your code so that you only have one outstanding GATT operation at a time. You need to wait for the corresponding callback to arrive until you can execute the next operation.

Emil
  • 16,784
  • 2
  • 41
  • 52
  • i am doing read and write operation simultaneously,means i am sending some command to ble after that ble will read this respond for this command – sushma1008 Feb 12 '17 at 18:09