GattCallback错误:133在向设备写入字符时?

时间:2019-06-22 04:54:27

标签: xamarin xamarin.android bluetooth-lowenergy bluetooth-gatt

向设备写入数据时出现GattCallback错误:133,发生的原因是应用程序处于后台模式或已终止状态。

            using (IDevice _device = await adapter.ConnectToKnownDeviceAsync(deviceID, new ConnectParameters(false, false), cancellationToken: _cancellationTokenSource.Token))
            {
                _cancellationTokenSource = new CancellationTokenSource();
                _cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(30));

                //    TestingValue("49");

                using (IService _service = await _device.GetServiceAsync(Guid.Parse("00008592-0000-1000-8000-00805f7b45fg"), _cancellationTokenSource.Token))
                {
                    if (_service != null)
                    {
                        ICharacteristic _characteristic =
                            await _service.GetCharacteristicAsync(Guid.Parse("00002a06-0000-1000-8000-00805f7b45fg"));

                        if (_characteristic != null)
                        {
                            _characteristic.WriteType = CharacteristicWriteType.WithoutResponse;

                            _cancellationTokenSource = new CancellationTokenSource();
                            _cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(30));

                            await _characteristic.WriteAsync(data, _cancellationTokenSource.Token);
                            await Task.Delay(3000);
                        }
                    }
                }

我想在选择选项或应用程序中出现通知时在ble设备中发出声音,它在某些设备上可以正常工作,但是在某些设备中,当应用程序在后台模式下时出现133 Gatt错误并杀死了状态,因为它不能在所有状态下正常工作。

请帮助我...

1 个答案:

答案 0 :(得分:0)

我最近遇到了这个问题。在研究期间,我在GitHub上发现了此问题:https://github.com/xabre/xamarin-bluetooth-le/issues/276在将autoConnect设置为true的情况下初始化ConnectParameters似乎很麻烦。我热烈鼓励您阅读此主题。

new ConnectParameters(true, false)

我将进一步研究它,尤其是adepter调用不是线程安全的问题。

此外,在文档https://github.com/xabre/xamarin-bluetooth-le中,有一些有趣的信息(“注意!重要说明/ API限制”和“最佳实践”部分),我想检查一下。