Xamarin表单插件,BLE找不到任何设备

zbdgwd5y  于 2023-09-28  发布在  其他
关注(0)|答案(1)|浏览(137)

我想写一个简单的应用程序,发送位到蓝牙设备。现在我不知道什么是错的,我不知道我从哪里得到的错误。我有所有的权限,我要求他们(只有本地化显示在屏幕上)

  1. <uses-permission android:name="android.permission.BLUETOOTH" />
  2. <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
  3. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  4. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

我用的是Plugin.BLE NuGet packageStateChangedEvent工作得很好。但是,当我扫描时,没有找到任何设备(即使我有可用于发现的设备)。

  1. public partial class MainPage : ContentPage
  2. {
  3. IBluetoothLE ble;
  4. IAdapter adapter;
  5. ...
  6. }
  7. public MainPage()
  8. {
  9. InitializeComponent();
  10. ble = CrossBluetoothLE.Current;
  11. adapter = CrossBluetoothLE.Current.Adapter;
  12. Devices = new ObservableCollection<Device_bluetooth>();
  13. ble.StateChanged += (s, e) =>
  14. {
  15. labelTest.Text=($"The bluetooth state changed to {e.NewState}");
  16. DisplayAlert("Notice", e.NewState.ToString(), "OK !");
  17. };
  18. }

用于扫描以发现以下设备的功能:

  1. // Issue is here
  2. private async void ScanAsync()
  3. {
  4. try
  5. {
  6. Devices.Clear();
  7. adapter.DeviceDiscovered += (s, a) =>
  8. {
  9. Devices.Add(new Device_bluetooth() { Device = a.Device, Name = "Name: " + a.Device.Name.ToString(), Uuid = "UUID:" + a.Device.Id.ToString() });
  10. };
  11. if (!ble.Adapter.IsScanning)
  12. {
  13. await adapter.StartScanningForDevicesAsync();
  14. }
  15. }
  16. catch (Exception ex)
  17. {
  18. await DisplayAlert("Notice", ex.Message.ToString(), "Error !");
  19. }

Device_bluetooth是存储设备的类,我在ListView中显示它们,如下所示:

  1. <ListView x:Name="mylistview" ItemsSource="{Binding Devices}" HasUnevenRows="True">
  2. <ListView.ItemTemplate>
  3. <DataTemplate>
  4. <ViewCell Height="100">
  5. <StackLayout>
  6. <Frame BackgroundColor="White"
  7. HasShadow="true">
  8. <StackLayout HeightRequest="100">
  9. <Label Text="{Binding Name ,Mode=TwoWay}" />
  10. <Label Text="{Binding Uuid ,Mode=TwoWay}" />
  11. </StackLayout>
  12. </Frame>
  13. </StackLayout>
  14. </ViewCell>
  15. </DataTemplate>
  16. </ListView.ItemTemplate>
  17. </ListView>

请帮帮我我几乎什么都试过了,但我无法科普。

igetnqfo

igetnqfo1#

对我来说,这是一个Android缺少的ACCESS_BACKGROUND_LOCATION权限。

相关问题