Subscribe To A Characteristic And Catch The Value Android
Solution 1:
You need to run a loop inside this function - displayGattServices(List gattServices) and get the entire services and characteristics of your connected BLE device.
For determining characteristics name and properties based on the UUID value you can refer to BLE characteristics
Once you have determined which characteristics is applicable for your BLE device, add it in a Queue and read/set them.
@ your DeviceActivity class
List <BluetoothGattCharacteristic> bluetoothGattCharacteristic = new ArrayList<BluetoothGattCharacteristic>();
Queue<BluetoothGattCharacteristic> mWriteCharacteristic = new LinkedList<BluetoothGattCharacteristic>();
privatevoiddisplayGattServices(List<BluetoothGattService> gattServices)
{
for(BluetoothGattService service : gattServices)
{
Log.i(TAG, "Service UUID = " + service.getUuid());
bluetoothGattCharacteristic = service.getCharacteristics();
for(BluetoothGattCharacteristic character: bluetoothGattCharacteristic)
{
Log.i(TAG, "Service Character UUID = " + character.getUuid());
// Add your **preferred characteristic** in a Queue
mWriteCharacteristic.add(character);
}
}
if(mWriteCharacteristic.size() > 0)
{
read_Characteristic();
};
};
Add the below function also in your DeviceActivity class,
// make sure this method is called when there is more than one characteristic to read & setprivatevoidread_Characteristic()
{
BluetoothLeService.read(mWriteCharacteristic.element());
BluetoothLeService.set(mWriteCharacteristic.element(),true);
mWriteCharacteristic.remove();
};
After setting them in your Service method, you need to have a broadcast receiver in your DeviceActivity activity class to handle the various events fired by your BluetoothLeService service class.
privatefinalBroadcastReceivergattUpdateReceiver=newBroadcastReceiver()
{
@OverridepublicvoidonReceive(Context context, Intent intent)
{
// TODO Auto-generated method stubfinalStringaction= intent.getAction();
if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action))
{
// Connection with the BLE device successful
................
................
}
elseif (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action))
{
// BLE device is disconnected
................
................
}
elseif (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action))
{
displayGattServices(BluetoothLeService.getSupportedGattServices());
}
elseif (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action))
{
Log.i(TAG, "Collecting data");
................
................
// If there are more than one characteristic call this method againif(mWriteCharacteristic.size() > 0)
{
read_Characteristic();
};
};
};
};
If you further need an example code to guide you through, you can refer to BLE sample code
Post a Comment for "Subscribe To A Characteristic And Catch The Value Android"