在 xamarin 表单中使用 yyy 编写特征时如何修复异常设备 xxx 已断开连接?

How to fix exception Device xxx disconnected while writing characteristic with yyy in xamarin forms?

我正在创建一个 BLE 应用程序,我已成功连接到 BLE 设备。我也能够阅读 GATT 特性。但是当我尝试进行写操作时出现异常

Device xxx disconnected while writing characteristic with yyy

这是我的代码

private async Task<string> ProcessDeviceInformationService(IService deviceInfoService)
        {
            try
            {
               await adapter.ConnectToDeviceAsync(device);
                var sb = new StringBuilder("Getting information from Device Information service: \n");
                var characteristics = await deviceInfoService.GetCharacteristicsAsync();
                var characteristic = await deviceInfoService.GetCharacteristicAsync(Guid.Parse("00002b0f-0000-1000-8000-00805f9b34fb"));

                //{
                    try
                    {
                        
deviceInfoService.GetCharacteristicAsync(GattCharacteristicIdentifiers.DataExchange);
                        if (characteristic != null)
                        {
                        var sbnew = new StringBuilder("BLE Characteristics\n");
                        byte[] senddata = Encoding.UTF8.GetBytes(string.IsNullOrEmpty(SendMessageLabel.Text) ? "0x21" : SendMessageLabel.Text);

 

                        await Task.Delay(300);
                        
                         
                        var newbytes = await characteristic.WriteAsync(senddata);
                   

                        byte[] characteristicsvalue = characteristic.Value;
                        var str = Encoding.UTF8.GetString(characteristicsvalue);



                        sbnew.AppendLine($"Characteristics found on this device: {string.Join(", ", str.ToString())}");
                        CharactericsLabel.Text = sbnew.ToString(); 

                        
                   
                    }

 
                }
                    catch (Exception ex)
                    {
                        return ex.Message;
                    }
          
                return CharactericsLabel.Text;

            }
            catch (Exception ex)
            {
             
                return ex.ToString();
            }
   
        }

经过一番搜索,我发现我需要传递十六进制值 value.Even 我尝试发送十六进制值,如您在代码中所见,但它不起作用。 对于 android 它工作正常但是对于 iOS 它仍然显示此异常 。我正在使用设备 Iphone 8 plus 和 OS Iphone 的版本是 13.5。1.The 写入操作适用于 Light Blue 应用程序,我从应用程序商店下载了 iOS。我的 write type 设置为 default,即 write with response。但它在我的 xamarin 应用程序中仅针对 iOS 部分给出了例外。我正在使用 Plugin.BLE 的最新稳定 版本 2.1.1。我不知道如何解决这个问题,有什么建议吗?

我通过从外围设备获取没有响应特性的写入来修复这个异常。