Subscribe to a characteristic and catch the value Android

我是研究僧i 提交于 2019-12-12 05:29:25

问题


I´m developing an BLE app, based on the Gatt sample project provided by google: https://developer.android.com/samples/BluetoothLeGatt/index.html. So, I can send data writing in a characteristic successfully. Now I need to know when this characteristic change its value.

DeviceActivity

private void displayGattServices(List<BluetoothGattService> gattServices) 
{
   // get services & characteristics
   ................ 

final BluetoothGattCharacteristic characteristic = mGattCharacteristics.get(2).get(0);
        final int charaProp = characteristic.getProperties();
        mWriteCharacteristic = characteristic;
        mBluetoothLeService.setCharacteristicNotification(mWriteCharacteristic, true);

   // write in the characteristic to send a reset command to BLE device

   // Start the read method, that permit subscribe to the characteristic
   BluetoothLeService.read(mWriteCharacteristic);
   BluetoothLeService.set(mWriteCharacteristic,true);
};

BluetoothLeService

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

        mBluetoothGatt.readCharacteristic(characteristic);
    };

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

        mBluetoothGatt.setCharacteristicNotification(characteristic, enabled);

        UUID uuid = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
        BluetoothGattDescriptor descriptor = characteristic.getDescriptor(uuid);
        descriptor.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        mBluetoothGatt.writeDescriptor(descriptor);
    };

But I dont know how can I catch the value of the read characteristic. Actually, I dont know if my code subscribe succesful to the characteristic or not. Some one can help me? How can I check if the value of the characteristic change really? And how can I display the new value of this characteristic in the screen? Is correct my code or I need add, modify or remove something? Thaks in advance. I guide by this question: Reading multiple characteristics from a BLE device synchronously (Recommended Method for Android)


回答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>();

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



来源:https://stackoverflow.com/questions/27136904/subscribe-to-a-characteristic-and-catch-the-value-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!