MAUI使用Plugin.BLE启用Telink蓝牙Mesh设备通知事件报错
我在MAUI应用中尝试为Telink蓝牙Mesh设备启用通知事件,环境如下:
- Visual Studio 2022(版本17.7.2)
- 使用Plugin.BLE(版本3.0.0-beta.5)
目前能正常连接设备、发送命令且设备已执行对应操作,但始终收不到命令执行结果的回复。调用通知启用逻辑时抛出GattStatus 133错误,GetNotifyCharacteristic()能正确返回目标通知特征。
相关代码
private async Task EnableNotifications() { try { // 通知特征Guid: "00010203-0405-0607-0809-0a0b0c0d1911" NotifyCharacteristic = await GetNotifyCharacteristic(); NotifyCharacteristic.WriteType = CharacteristicWriteType.Default; if (NotifyCharacteristic.CanUpdate) { NotifyCharacteristic.ValueUpdated -= CharacteristicOnValueUpdated; NotifyCharacteristic.ValueUpdated += CharacteristicOnValueUpdated; await NotifyCharacteristic.StartUpdatesAsync(); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Enable Notify Error : " + ex.ToString()); } } private void CharacteristicOnValueUpdated(object sender, CharacteristicUpdatedEventArgs characteristicUpdatedEventArgs) { var test = characteristicUpdatedEventArgs.Characteristic.Value; }
错误日志
[BluetoothGatt] setCharacteristicNotification() - uuid: 00010203-0405-0607-0809-0a0b0c0d1911 enable: true
[BluetoothGatt] onClientConnectionState() - status=0 clientIf=9 device=FF:FF:20:98:3B:C0
[BluetoothGatt] close()
[BluetoothGatt] unregisterApp() - mClientIf=9[0:] Enable Notify Error : System.Exception: GattStatus: 133 at Plugin.BLE.Abstractions.Utils.TaskBuilder.<SafeEnqueueAndExecute>d__11`1[[System.Boolean, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext() in D:\a\dotnet-bluetooth-le\dotnet-bluetooth-le\Source\Plugin.BLE\Shared\Utils\TaskBuilder.cs:line 96 at Plugin.BLE.Abstractions.Utils.TaskBuilder.<FromEvent>d__9`3[[System.Boolean, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.EventHandler`1[[Plugin.BLE.Android.CallbackEventArgs.DescriptorCallbackEventArgs, Plugin.BLE, Version=3.0.0.0, Culture=neutral, PublicKeyToken=null]], System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.EventHandler, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].MoveNext() in D:\a\dotnet-bluetooth-le\dotnet-bluetooth-le\Source\Plugin.BLE\Shared\Utils\TaskBuilder.cs:line 48 at Plugin.BLE.Android.Characteristic.StartUpdatesNativeAsync(CancellationToken cancellationToken)
Plugin.BLE示例应用错误详情

解决建议
GattStatus 133是Android平台常见的BLE通用错误,结合Telink蓝牙Mesh设备特性,可尝试以下方案:
检查Descriptor写入权限与类型
启用BLE通知时,需要正确写入Client Characteristic Configuration Descriptor(UUID: 00002902-0000-1000-8000-00805f9b34fb),确保写入值为0x0100(启用通知)。部分设备需显式指定写入类型:var descriptor = await NotifyCharacteristic.GetDescriptorAsync(Guid.Parse("00002902-0000-1000-8000-00805f9b34fb")); await descriptor.WriteAsync(new byte[] { 0x01, 0x00 }, CharacteristicWriteType.NoResponse);调整连接参数
Telink设备对BLE连接间隔、超时参数有特定要求,连接时可设置合适参数:var connectionParameters = new ConnectionParameters { AutoConnect = false, ConnectionTimeout = TimeSpan.FromSeconds(10), Interval = ConnectionInterval.Medium, SlaveLatency = 0, SupervisionTimeout = TimeSpan.FromSeconds(20) }; await device.ConnectAsync(connectionParameters);降级Plugin.BLE版本
当前使用的是测试版,可能存在兼容性问题,尝试降级到稳定版(如2.5.1)验证是否解决问题。验证设备端配置
确认Telink设备固件已开启对应特征的通知广播权限,设备端逻辑是否正确触发通知事件。完善Android权限与后台配置
在AndroidManifest.xml中添加必要权限:<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" /> <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />Android 12+需添加前台服务类型以保持后台BLE连接:
<service android:foregroundServiceType="connectedDevice" />
内容的提问来源于stack exchange,提问作者Shane

