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

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

如何解决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 举报,一经查实,本站将立刻删除。