Ajay Veer
Ajay Veer

Reputation: 9

How to read data from custom BLE service(e.g smartwatch)

I have a BLE device (smartwatch) in which i want to read all data from services and use in my application(Swift and Android), How can i access data from the custom services, but i cant see the UUID in the GATT services and characteristics

Upvotes: 0

Views: 2375

Answers (1)

Joshi Tushar
Joshi Tushar

Reputation: 116

You can read data using this code. it's work for me. When you Smartwatch connected then you needs to send request for service discover. So, Below callback method triggered. In this method get your service and characteristics.

 @Override
        public void onServicesDiscovered(final BluetoothGatt gatt, final int status) {
            Log.e(TAG, "onServicesDiscovered: ");

            if (status != BluetoothGatt.GATT_SUCCESS) {
                Log.w(TAG, "onServicesDiscovered received: " + status);
                return;
            }

            BluetoothGattService customService = gatt.getService(CUSTOM_SERVICE_UUID);
            if (customService != null) {
                customNotifyCharacteristic = customService.getCharacteristic(CUSTOM_CHARACTERISTICS_NOTIFY_UUID);
                customWriteCharacteristic = customService.getCharacteristic(CUSTOM_CHARACTERISTICS_WRITE_UUID);
                customReadCharacteristic = customService.getCharacteristic(CUSTOM_CHARACTERISTICS_READ_UUID);
            }
        }

After getting all characteristics and services send read request like below. put below line where you need to read data.

readCharacteristic(customReadCharacteristic);

below explain readCharacteristic method.

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

                mBluetoothGatt.readCharacteristic(characteristic);
            }

So, onCharacteristicRead callback triggered.

 @Override
        public void onCharacteristicRead(BluetoothGatt gatt,
                                         BluetoothGattCharacteristic characteristic,
                                         int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {
               byte[] data = characteristic.getValue();
            }
        }

byte[] data = characteristic.getValue(); return your data which you want to read form device.

Upvotes: 1

Related Questions