【问题标题】:Subscribe to a characteristic and catch the value Android订阅一个特征并捕捉价值 Android
【发布时间】:2015-01-24 01:23:38
【问题描述】:

我正在开发一个 BLE 应用程序,基于 google 提供的 Gatt 示例项目:https://developer.android.com/samples/BluetoothLeGatt/index.html。所以,我可以成功发送数据写入一个特性。现在我需要知道这个特征什么时候改变它的值。

设备活动

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

但我不知道如何获取读取特征的值。实际上,我不知道我的代码是否成功订阅了该特征。有人可以帮助我吗?如何检查特征值是否真的发生了变化?以及如何在屏幕上显示此特性的新值?我的代码是否正确,或者我需要添加、修改或删除某些内容? 提前谢谢。 我以这个问题为指导:Reading multiple characteristics from a BLE device synchronously (Recommended Method for Android)

【问题讨论】:

    标签: android bluetooth-lowenergy subscription descriptor characteristics


    【解决方案1】:

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

    关于根据 UUID 值确定特征名称属性,您可以参考BLE characteristics

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

    @你的 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 类中也添加以下函数,

       // 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 服务类触发的各种事件。

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

    如果您还需要示例代码来指导您完成,您可以参考BLE sample code

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-21
      • 1970-01-01
      • 1970-01-01
      • 2012-12-16
      • 1970-01-01
      • 1970-01-01
      • 2021-10-22
      • 2021-12-11
      相关资源
      最近更新 更多