Sandeep Tengale
Sandeep Tengale

Reputation: 162

Continuesly reading values for BLE in Android

I am trying to interact with BLE from the Android device and read some sensor values, I am following the API level > 21. I managed to scan the device and connect to it. First challenge was to read all the characteristic value together in one shot, some suggested to use priority queue to do that, I implemented like wise and it is working fine.

BluetoothGattCallback gattCallBack = new BluetoothGattCallback() {
        @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        switch (newState) {
            case BluetoothProfile.STATE_CONNECTED:
                Log.d(TAG, "STATE_CONNECTED");
                bluetoothGatt.discoverServices();
                break;
            case BluetoothProfile.STATE_DISCONNECTED:
                Log.d(TAG, "STATE_DISCONNECTED");
                break;
            default:
                Log.d(TAG, "STATE_OTHER");

        }
    }

    @Override
    public void onServicesDiscovered(BluetoothGatt gatt, int status) {
        final List<BluetoothGattService> services = gatt.getServices();
        characteristics = new ArrayList<BluetoothGattCharacteristic>();
        characteristics.add(services.get(1).getCharacteristics().get(0));
        characteristics.add(services.get(1).getCharacteristics().get(1));
        characteristics.add(services.get(1).getCharacteristics().get(2));

        characteristics.add(services.get(2).getCharacteristics().get(0));
        characteristics.add(services.get(2).getCharacteristics().get(1));
        characteristics.add(services.get(2).getCharacteristics().get(2));

        characteristics.add(services.get(3).getCharacteristics().get(0));
        characteristics.add(services.get(3).getCharacteristics().get(1));
        characteristics.add(services.get(3).getCharacteristics().get(2));
        requesReadCharacteristics(gatt);
    }

    public void requesReadCharacteristics(BluetoothGatt gatt) {
        gatt.readCharacteristic(characteristics.get(characteristics.size() - 1));
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        if (status == 0 ) {
            Log.d(TAG, "DeviceNameFetchFromDevice: " + characteristic.getValue());
            characteristics.remove(characteristics.get(characteristics.size() - 1));

            if (characteristics.size() > 0) {
                requesReadCharacteristics(gatt);
            } else {
                gatt.disconnect();
            }
        }
    }

    @Override
    public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        super.onCharacteristicWrite(gatt, characteristic, status);
    }
};

Now I need to continuously monitor by BLE device to read the sensor value(like every interval of 10 sec), so I need to read the characteristic values repeatedly and get their values. But the ble won't allow to call "readCharacteristic" after first call.

For now I have implemented it like, calling connectDevice(BluetoothDevice) on regular interval, which is like disconnecting the exiting connection to BLE device and connecting it again. Is there any way where we can call readCharacteristic repeatedly being connected to the device. Disconnecting and connecting the device is not that efficient according to my understanding. Please let me know, what is correct approach to read device continuesly or share if there are any example like this

https://github.com/sandeeptengale/androidble/

Upvotes: 1

Views: 3091

Answers (1)

zkywalker
zkywalker

Reputation: 169

first,you should turn on notification:

mBluetoothLeService.setCharacteristicNotification(characteristic, true);

then,you can read values in this method,when data changed:

onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic)

Upvotes: 1

Related Questions