如何在Xamarin/Android上启用多个BLE特征通知?

Dun*_*ald 10 notifications xamarin.android characteristics bluetooth-lowenergy xamarin

我正在尝试使用Xamarin/Android启用多个BLE特性的通知,但似乎无法这样做.如果我尝试一次启用多个BLE事件,该应用程序似乎停止接收任何BLE事件.

任何人都可以使用Tamarin/Android确认这是否可行.我们有一个原生的iOS应用程序,可以正常启用多个通知.我们使用的基本步骤如下:

  1. 扫描设备
  2. 连接到设备
  3. 发现服务
  4. 对于每个发现的服务,迭代特征并启用所需的服务
  5. 处理BLE回调中的每个异步回调事件

每当我们尝试在多个特征上启用通知时,我们就不再接收任何事件.

我也无法找到任何启​​用多个特性的示例.

我希望我在这里错过了使用Xamarin/Android API的基本信息.

public override void OnServicesDiscovered (BluetoothGatt gatt, GattStatus status)
{
    base.OnServicesDiscovered (gatt, status);
    foreach (BluetoothGattService service in gatt.Services) {
        string uuid = service.Uuid.ToString ().ToUpper();
        if (uuid.Equals (BLEServices.HRService.ToUpper())) {
            _Adap.LogMessage ("HRService discovered");
            foreach(BluetoothGattCharacteristic characteristic in service.Characteristics) {
                string c_uuid = characteristic.Uuid.ToString ().ToUpper ();
                _Adap.LogMessage (" HRCharacteristic: " + c_uuid);

                if (c_uuid.Equals(_Adap.useCharacteristic.ToUpper())) {
                    _Adap.LogMessage ("  enabling HRCharacteristic");
                    gatt.SetCharacteristicNotification(characteristic, true);
                    BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor (Java.Util.UUID.FromString (BLEServices.CLIENT_CHARACTERISTIC_CONFIG), GattDescriptorPermission.Write | GattDescriptorPermission.Read);
                    characteristic.AddDescriptor (descriptor);
                    descriptor.SetValue (BluetoothGattDescriptor.EnableNotificationValue.ToArray ());
                    gatt.WriteDescriptor (descriptor);
                    _Adap.StartTimer ();
                }
            }

        } else if (uuid.Equals (BLEServices.BatteryService.ToUpper())) {
            _Adap.LogMessage ("BatteryService discovered");
            foreach (BluetoothGattCharacteristic characteristic in service.Characteristics) {
                string c_uuid = characteristic.Uuid.ToString ().ToUpper ();
                _Adap.LogMessage (" BatteryService: " + c_uuid);

                if (c_uuid.Equals (_Adap.useCharacteristic.ToUpper ())) {
                    _Adap.LogMessage ("  reading batteryCharacteristic");
                    // This may only be reported when the battery level changes so get the level first by doing a read
                    gatt.ReadCharacteristic (characteristic);

                    //gatt.SetCharacteristicNotification (characteristic, true);
                    //BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor (Java.Util.UUID.FromString (BLEServices.CLIENT_CHARACTERISTIC_CONFIG), GattDescriptorPermission.Write | GattDescriptorPermission.Read);
                    //characteristic.AddDescriptor (descriptor);
                    //descriptor.SetValue (BluetoothGattDescriptor.EnableNotificationValue.ToArray ());
                    //gatt.WriteDescriptor (descriptor);
                }
            }
        } else if (uuid.Equals (BLEServices.DeviceInfoService.ToUpper())) {
            _Adap.LogMessage ("DeviceInfoService discovered");
            foreach (BluetoothGattCharacteristic characteristic in service.Characteristics) {
                string c_uuid = characteristic.Uuid.ToString ().ToUpper ();
                _Adap.LogMessage (" DeviceInfoService: " + c_uuid);
                if (c_uuid.Equals (BLEServices.kModelNumberCharacteristicUuidString.ToUpper ())) {
                    //gatt.ReadCharacteristic (characteristic);
                }
            }
        } else if (uuid.Equals (BLEServices.kHxM2CustomServiceUuidString.ToUpper())) {
            _Adap.LogMessage ("HxM2CustomService discovered");
            foreach (BluetoothGattCharacteristic characteristic in service.Characteristics) {
                string c_uuid = characteristic.Uuid.ToString ().ToUpper ();
                _Adap.LogMessage (" HxM2CustomCharacteristic: " + c_uuid);

                if (c_uuid.Equals (_Adap.useCharacteristic.ToUpper ())) {
                    _Adap.LogMessage ("  enabling HxM2 characteristic: "+_Adap.useCharacteristic);
                    gatt.SetCharacteristicNotification (characteristic, true);
                    BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor (Java.Util.UUID.FromString (BLEServices.CLIENT_CHARACTERISTIC_CONFIG), GattDescriptorPermission.Write | GattDescriptorPermission.Read);
                    characteristic.AddDescriptor (descriptor);
                    descriptor.SetValue (BluetoothGattDescriptor.EnableNotificationValue.ToArray ());
                    gatt.WriteDescriptor (descriptor);
                    // Start a timer to make sure that we can recover if we never receive any data from the device
                    _Adap.StartTimer ();
                }

            }
        } else {
            _Adap.LogMessage ("Unknown Service "+uuid+" discovered");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以解释以下行是什么

BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor (Java.Util.UUID.FromString (BLEServices.CLIENT_CHARACTERISTIC_CONFIG), GattDescriptorPermission.Write | GattDescriptorPermission.Read);
characteristic.AddDescriptor (descriptor);
descriptor.SetValue (BluetoothGattDescriptor.EnableNotificationValue.ToArray ());
gatt.WriteDescriptor (descriptor);
Run Code Online (Sandbox Code Playgroud)

Sve*_*übe 4

除了您找到的解决方案之外:请注意,您无法听取无限数量的特征。最大值在 android 源代码中被硬编码为BTA_GATTC_NOTIF_REG_MAX.

因此,您的应用程序不应依赖超过最低支持 Android 版本的通知特征的最大数量。