订阅特征并捕捉Android的价值 [英] Subscribe to a characteristic and catch the value Android

查看:98
本文介绍了订阅特征并捕捉Android的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于Google提供的Gatt示例项目开发BLE应用程序: https://developer.android.com/samples/BluetoothLeGatt/index.html .因此,我可以成功地发送写入特征的数据.现在,我需要知道该特征何时更改其值.

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);
    };

但是我不知道如何捕捉读取特性的值.实际上,我不知道我的代码是否成功订阅了该特性.有人可以帮助我吗?如何检查特征值是否确实发生了变化?以及如何在屏幕上显示此特性的新值?我的代码是否正确,或者我需要添加,修改或删除某些内容? 提前解冻. 我通过以下问题进行指导:从BLE中读取多个特征同步设备(Android推荐方法)

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)

推荐答案

您需要在此函数内运行一个循环- displayGattServices(List gattServices),并获取所连接BLE的全部服务和特征设备.

You need to run a loop inside this function - displayGattServices(List gattServices) and get the entire services and characteristics of your connected BLE device.

要根据UUID值确定特征名称属性,可以参考

For determining characteristics name and properties based on the UUID value you can refer to BLE characteristics

确定了适用于BLE设备的特征后,将其添加到队列中并读取/设置它们.

Once you have determined which characteristics is applicable for your BLE device, add it in a Queue and read/set them.

@您的 DeviceActivity

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();
    };
};

还将以下功能添加到您的 DeviceActivity 类中,

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();
   };

在您的Service方法中设置它们之后,您需要在 DeviceActivity 活动类中具有一个广播接收器,以处理由 BluetoothLeService 服务类触发的各种事件.

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

这篇关于订阅特征并捕捉Android的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆