微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我无法在使用Android 9或+以及使用Plugin.BLE Xamarin Forms的情况下更新蓝牙设备

如何解决我无法在使用Android 9或+以及使用Plugin.BLE Xamarin Forms的情况下更新蓝牙设备

我可以获取信息并在iPhone和旧版Android上更新设备蓝牙。 但是我不能在Android 9和10上使用await特性.StartUpdateAsync()。

在iOS或其他版本的android上尝试try characterist.StartUpdateAsync()不要停止应用程序并从设备获取更多信息。

我的代码

公共异步无效GetDevice(IAdapter适配器) {

        try
        {

            if (bluetoothLE.State == BluetoothState.Off)
            {
                await displayAlert("Atenção","É necessário estar com o Bluetooh ativado.","ok");
                return;
            }


            var location = await Geolocation.GetLastKNownLocationAsync();
            if (location == null)
            {
                await displayAlert("Atenção","É necessário estar com a localização ativada.","ok");
                return;
            }

            adapter.ScanTimeout = 20000;
            adapter.ScanMode = ScanMode.Balanced;


            adapter.Devicediscovered += (objeto,a) =>
            {
                if (a.Device.Name != null  && a.Device.Name.ToLower().Contains("luckie"))
                {
                    device = a.Device;
                }
            };


            await adapter.StartScanningForDevicesAsync();

            if (device != null)
            {
                await adapter.StopScanningForDevicesAsync();
                await adapter.ConnectToDeviceAsync(device);

                var services = await device.GetServicesAsync();

                foreach (var service in services)
                {
                    var caracteristicas = await service.GetcharacteristicsAsync();


                    foreach (var carac in caracteristicas)
                    {
                        //Bug android 9 ou > -- Problemas ao notificar o device
                        //Checa se é Bateria ou Temperatura
                        if (carac.Uuid == "2a19" || carac.Uuid == "2a1c"
                            || carac.Name.ToLower().Equals("temperature measurement")
                            || carac.Name.ToLower().Equals("battery level"))
                        {
                            
                            carac.ValueUpdated += (o,args) =>
                            {
                                var bytes = args.Characteristic.Value;
                            };

                            await carac.StartUpdatesAsync();

                            //Temperatura
                            if (carac.Uuid == "2a1c" || carac.Name.ToLower().Equals("temperature measurement"))
                            {
                                byte[] byteValues = carac.Value;
                                string hexadecimal = BitConverter.ToString(byteValues);
                                string[] hexSplit = hexadecimal.Split('-');

                                //Temperatura
                                string medicaoHex = "0x" + hexSplit[2] + hexSplit[1];
                                string result = Convert.ToInt32(medicaoHex,16).ToString();
                                string temperatura = result.Substring(0,2) + "," + result.Substring(2,2);

                                //Horário
                                string horaHex = "0x" + hexSplit[9];
                                string minutoHex = "0x" + hexSplit[10];
                                string segundoHex = "0x" + hexSplit[11];

                                int hora = Convert.ToInt32(horaHex,16);
                                int minuto = Convert.ToInt32(minutoHex,16);
                                int segundo = Convert.ToInt32(segundoHex,16);
                                TimeSpan horario = new TimeSpan(hora,minuto,segundo);

                                temperature.Text = temperatura + "º";

                                titleTemperature.Text = "Ultima medição em " + minuto + " minutos";

                                activeLoad.IsRunning = false;
                                activeLoad.IsVisible = false;

                                frameTemperature.IsVisible = true;
                            }

                            if (carac.CanRead)
                            {
                                if (carac.Uuid == "2a19" || carac.Name.ToLower().Equals("battery level"))
                                {
                                    byte[] byteValues = carac.Value;
                                    string hexadecimal = BitConverter.ToString(byteValues);
                                    int batterylevel = 0;
                                    if (!string.IsNullOrEmpty(hexadecimal))
                                    {
                                        string[] hexSplit = hexadecimal.Split('-');

                                        //Bateria
                                        string batteryHex = "0x" + hexadecimal;
                                        batterylevel = Convert.ToInt32(batteryHex,16);
                                    }

                                }


                            }

                        }
                    }
                }
            } else
            {
                activeLoad.IsVisible = false;
                titleTemperature.Text = "Nenhum dispositivo foi enconTrado. " +
                    "Verifique se você está próximo a algum dispositivo.";

            }


        } catch(DeviceConnectionException ex)
        {
         await displayAlert("Atenção","Não foi possível conectar " + ex.DeviceName,"ok");
        }
        
    }

感谢我的帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。