如何解决BLE GATT 在连接后有时会断开连接并出现异常 'GattCallback error: 133' in xamarin 形式
我正在以 Xamarin 形式实现 BLE。我能够执行所有 BLE 操作读取、写入和通知。但有时设备在连接后会断开连接。这是我得到的例外
Plugin.BLE.Abstractions.Exceptions.DeviceConnectionException:“GattCallback 错误:133”
这是我进行连接的代码
private async Task ScanForDevices(ScanData scanData)
{
_adapter = CrossBluetoothLE.Current.Adapter;
_adapter.ScanMode = ScanMode.LowLatency;
Device.BeginInvokeOnMainThread(async () =>
{
_adapter.DeviceDiscovered += async (s,a) =>
{
NativeDeviceAdd = DependencyService.Get<INativeDevice>().ConvertToNative(a.Device);
PropertyInfo propInfo = NativeDeviceAdd.GetType().GetProperty("Name");
BleDeviceName = (string)propInfo.GetValue(NativeDeviceAdd,null);
string substr = scanData.blename;
if (BleDeviceName == substr)
{
_device = a.Device;
await _adapter.StopScanningForDevicesAsync();
await ConnectForDevice(_characteristicsBLE);
}
};
});
await _adapter.StartScanningForDevicesAsync();
}
private async Task ConnectForDevice(ICharacteristic characteristics)
{
await _adapter.ConnectToDeviceAsync(_device);
}
这些是日志。
01-08 17:31:50.435 D/BluetoothManager(26670): getConnectionState()
01-08 17:31:50.435 D/BluetoothManager(26670): getConnectedDevices
01-08 17:31:50.439 D/BluetoothGatt(26670): connect() - device: 34:C9:F0:8D:F4:57,auto: false
01-08 17:31:50.439 D/BluetoothGatt(26670): registerApp()
01-08 17:31:50.439 D/BluetoothGatt(26670): registerApp() - UUID=84c84faa-46ff-4e02-9f9a-f8d14fed8ed0
01-08 17:31:50.441 D/BluetoothGatt(26670): onClientRegistered() - status=0 clientIf=13
01-08 17:31:51.587 D/BluetoothGatt(26670): onClientConnectionState() - status=133 clientIf=13 device=34:C9:F0:8D:F4:57
01-08 17:31:51.594 D/BluetoothGatt(26670): onClientConnectionState() - status=133 clientIf=12 device=34:C9:F0:8D:F4:57
Thread started: #10
Thread started: #11
01-08 17:31:51.680 D/BluetoothGatt(26670): close()
01-08 17:31:51.680 D/BluetoothGatt(26670): close()
01-08 17:31:51.680 D/BluetoothGatt(26670): unregisterApp() - mClientIf=12
01-08 17:31:51.680 D/BluetoothGatt(26670): unregisterApp() - mClientIf=13
**Plugin.BLE.Abstractions.Exceptions.DeviceConnectionException:** 'GattCallback error: 133'
我没有从 Xamarin 方面得到任何解决方案。有什么建议吗?
解决方法
不幸的是,错误 133 比较常见,通常与硬件/堆栈有关(尤其是在 Android 上发生这种情况时)。它还没有明确定义的修复程序(据我所知),但已经提到了一些解决方法,包括尝试静默重新连接或尝试创建绑定。查看下面的链接并搜索“133”以获取有关解决方法的更多信息:-
还有一些其他用户尝试过的参考资料:-
- GATT Error 133 on onConnectionStateChange
- Android 10 with GATT Error 133
- GATT Error 133 with Android
- Making Android BLE Work
- Lessons for Android BLE Developers
我通过为 ConnectToDeviceAsync 方法指定传输类型来解决它,如下所示:
var parameters = new ConnectParameters(forceBleTransport: true);
await _adapter.ConnectToDeviceAsync(deviceToConnect,parameters,cancellationToken.Token);
有时我还是会遇到这个问题,所以我检查了 Play 商店中的 BLE 资源管理器应用,发现问题出在广告客户身上。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。