Bluetooth Low Energy get characteristic description

5.3k views Asked by At

I have one bluetooth 4.0 sensor which send me many services and characteristics, like this draft example:

<service uuid="ServiceUUID" advertise="true">
  <description>Test Service</description>
     <characteristic uuid="CharacteristUUID" id="test_rate">
         <properties read="true" notify = "true"/>
            <value length="1" type="hex"></value>
            <description>Test Rate</description>
     </characteristic>
</service>

I start with android sample BluetoothLEGatt and I want get the characteristic description name for example (Test Rate) dynamically, because in sample they use SampleGattAttributes class to get the description programmatically.

Anyone know how it is possible?

Regards.

1

There are 1 answers

4
Dinesh Prajapati On

For BluetoothGatt dynamic data you have to follow below steps to create the call back methods

  1. Create object of Bluetooth Device, device Address will be the one which you will get after connection

     BluetoothManager m = (BluetoothManager)context.getSystemService(Context.BLUETOOTH_SERVICE);
            m_adapter = m.getAdapter();
    BluetoothDevice device = m_adapter.getRemoteDevice(deviceAddress);
    
  2. Create BluetoothGatt object

        private BluetoothGatt m_gatt;
        m_gatt = m_device.connectGatt(m_context, false, m_callback);
    

    3.Call back implementation

      private final BluetoothGattCallback m_callback = new BluetoothGattCallback()
     {        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
        String testid = characteristic.getDescriptor(characteristic.getUuid()).getCharacteristic().getStringValue(0);
    };
                    }
    

Make sure for the bluetooth permission and other minor variable declaration.