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

标签 xamarin notifications xamarin.android bluetooth-lowenergy characteristics

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

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

  • 扫描设备
  • 连接设备
  • 发现服务
  • 对于每个发现的服务,遍历特征并启用所需的特征
  • 处理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");
            }
        }
    }
    

    谁能解释以下几行的用途

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

    最佳答案

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

  • 安卓 >= 4.3: 4
    http://androidxref.com/4.3_r2.1/xref/external/bluetooth/bluedroid/bta/gatt/bta_gattc_int.h#301
  • 安卓 >= 4.4:7 http://androidxref.com/4.4.4_r1/xref/external/bluetooth/bluedroid/bta/gatt/bta_gattc_int.h#333
  • 安卓 >= 5.0:15 http://androidxref.com/5.0.0_r2/xref/external/bluetooth/bluedroid/bta/gatt/bta_gattc_int.h#345

  • 因此,您的应用不应该依赖于您支持的最低 Android 版本的最大通知特性数量。

    关于xamarin - 如何在 Xamarin/Android 上启用多个 BLE 特征通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36539473/

    相关文章:

    c# - Xamarin MVVM 自定义模型属性?

    android - 应用程序被销毁后立即发出通知

    android - 可扩展通知android 4.1

    c# - 奇怪的句柄必须是对话框中的有效异常

    c# - 如何创建 xamarin 表单跨平台汉堡幻灯片 View

    java - '警告 BG8102 : Class X has unknown base type Y' warning while binding java code in xamarin

    android - 适用于 Android 的 Mono 的 Xamarin 设计器使用什么主题?

    java - 在 OS X 上安装 Java 8 会影响需要 Java 7 的 Xamarin 吗?

    c# - 如何在 xamarin android 项目上集成 webrtc android native 库?

    ios - 用户信息中带有自定义对象的 UNMutableNotificationContent