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>();
private void displayGattServices(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 & set
private void read_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.
private final BroadcastReceiver gattUpdateReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub
final String action = intent.getAction();
if (BluetoothLeService.ACTION_GATT_CONNECTED.equals(action))
{
// Connection with the BLE device successful
................
................
}
else if (BluetoothLeService.ACTION_GATT_DISCONNECTED.equals(action))
{
// BLE device is disconnected
................
................
}
else if (BluetoothLeService.ACTION_GATT_SERVICES_DISCOVERED.equals(action))
{
displayGattServices(BluetoothLeService.getSupportedGattServices());
}
else if (BluetoothLeService.ACTION_DATA_AVAILABLE.equals(action))
{
Log.i(TAG, "Collecting data");
................
................
// If there are more than one characteristic call this method again
if(mWriteCharacteristic.size() > 0)
{
read_Characteristic();
};
};
};
};
If you further need an example code to guide you through, you can refer to BLE sample code
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…