Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper method for reading a GATT characteristic in Android?

In attempting to read the value of a Bluetooth Low-Energy GATT characteristic in the Android API 18, I came across the following dilemma: What is the proper way to retrieve the value stored in a characteristic? And at which level of the stack should this action take place?

In conducting my own research, I stumbled upon what I understand are two possible methods:

  • BluetoothGatt.readCharacteristic(BluetoothGattCharacteristic characteristic)
  • BluetoothGattCharacteristic.getValue()

    public void onClick(View v){        
        byteValue = mBTValueCharacteristic.getValue();
        if ((byteValue[0] & 0x01) == 1)
            byteValue[0] = 0x00;
        else
            byteValue[0] = 0x01;
    
        mBTValueCharacteristic.setValue(byteValue);
        mBTGatt.writeCharacteristic(mBTValueCharacteristic);
    }
    

Above is the original code which led me to this issue. In it, I attempt to read the value of a characteristic, and simply toggle its state using a button.

like image 690
estebro Avatar asked Nov 16 '13 02:11

estebro


People also ask

What is a GATT characteristic?

A GATT characteristic is a basic data element used to construct a GATT service, BluetoothGattService . The characteristic contains a value as well as additional information and optional GATT descriptors, BluetoothGattDescriptor .

What is GATT service on Android?

The GATT profile is a general specification for sending and receiving short pieces of data known as "attributes" over a BLE link. All current BLE application profiles are based on GATT.

What is GATT protocol?

GATT is a protocol style of exchange data over the wireless Bluetooth connection that is introduced for BLE 4.0. The Bluetooth connection can consist of many services. A service is like data channel or view as a data object passing to and from the Bluetooth device.

How do you retrieve data from a BLE device?

Set the BLE hardware of your device to either peripheral or central mode. In the Peripheral (server) mode, you must specify the Service and Characteristic of your device. Other devices then receive the data sent from your Android™ mobile device with the same service and characteristic.


1 Answers

BluetoothGatt.readCharacteristic(BluetoothGattCharacteristic characteristic)

This function is updating your BluetoothGattCharacteristic object (on your Android device) using characteristic value from the Bluetooth .

BluetoothGattCharacteristic.getValue()

This function is just a getter function of the BluetoothGattCharacteristic object. There is not any transaction between android and the bluetooth device.

like image 173
reTs Avatar answered Sep 27 '22 23:09

reTs