0

我正在开发一个 BLE 应用程序,基于谷歌提供的 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);
};

蓝牙服务

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 features from a BLE device synchronously (Recommended Method for Android)

4

1 回答 1

0

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

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

一旦您确定了哪些特性适用于您的 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 示例代码

于 2014-11-27T08:24:46.370 回答