Xamarin Forms Plugin.BLE找不到任何设备

问题描述

我想编写一个简单的应用程序,将位发送到蓝牙设备。目前,我不知道出什么问题了,也不知道该错误在哪里。 我拥有所有权限,我正在寻求这些权限(屏幕上显示ony本地化)

    <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.ACCESS_COARSE_LOCATION" />

我正在使用Plugin.BLE

  public partial class MainPage : ContentPage
    {
        IBluetoothLE ble;
        IAdapter adapter;
        ...
    }
    public MainPage()
        {
            InitializeComponent();
            ble = CrossBluetoothLE.Current;
            adapter = CrossBluetoothLE.Current.Adapter;
            Devices = new ObservableCollection<Device_bluetooth>();
            ble.StateChanged += (s,e) =>
            {
                labelTest.Text=($"The bluetooth state changed to {e.NewState}");
                displayAlert("Notice",e.NewState.ToString(),"OK !");
            };
        }

事件ble.StateChanged工作正常。

        private async void ScanAsync()
        {
            try
            {
                Devices.Clear();
                adapter.Devicediscovered += (s,a) =>
                {
                    Devices.Add(new Device_bluetooth() { Device = a.Device,Name = "Name: " + a.Device.Name.ToString(),Uuid = "UUID:" + a.Device.Id.ToString() });
                };

                if (!ble.Adapter.IsScanning)
                {
                    await adapter.StartScanningForDevicesAsync();
                }
            }
            catch (Exception ex)
            {
                await displayAlert("Notice",ex.Message.ToString(),"Error !");
            }

此事件-调试时未输入adapter.Devicediscovered。当然,可以使用蓝牙设备进行发现。 Device_bluetooth是我的设备类。我在ListView上显示它们

        <ListView x:Name="mylistview" ItemsSource="{Binding Devices}" HasUnevenRows="True">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell Height="100">
                        <StackLayout>
                            <Frame  BackgroundColor="White"
                              HasShadow="true">
                                <StackLayout HeightRequest="100">
                                    <Label Text="{Binding Name,Mode=TwoWay}"   />
                                    <Label Text="{Binding Uuid,Mode=TwoWay}"   />
                                </StackLayout>
                            </Frame>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>

            </ListView.ItemTemplate>
        </ListView>

请帮助。我已经尝试了几乎所有内容,但无法解决

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)