我想写一个简单的应用程序,发送位到蓝牙设备。现在我不知道什么是错的,我不知道我从哪里得到的错误。我有所有的权限,我要求他们(只有本地化显示在屏幕上)
<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 NuGet packageStateChangedEvent
工作得很好。但是,当我扫描时,没有找到任何设备(即使我有可用于发现的设备)。
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 !");
};
}
用于扫描以发现以下设备的功能:
// Issue is here
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 !");
}
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>
请帮帮我我几乎什么都试过了,但我无法科普。
1条答案
按热度按时间igetnqfo1#
对我来说,这是一个Android缺少的ACCESS_BACKGROUND_LOCATION权限。