Bluetooth not showing devices in Maui - plugin.Ble #19753
babucr
started this conversation in
Upgrading from Xamarin
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Bluetooth does not show any device after scanning.
I use Plugin.Ble version 3 and .net 8 . Also use latest version of vs Version 17.8.3.
Also added all permissions, also added runtime permission.
This is the code I use
private async void CounterBtn_Clicked(object sender, EventArgs e)
{
await ScanAsync();
}
private async Task ScanAsync()
{
var ble = CrossBluetoothLE.Current;
var adapter = CrossBluetoothLE.Current.Adapter;
var state = ble.State;
adapter.ScanTimeout = 20000;
await RequestBluetooth();
await adapter.StartScanningForDevicesAsync();
List deviceList = new List();
ToastDuration duration = ToastDuration.Long;
double fontSize = 18;
if (deviceList.Count == 0)
{
string text = "Unable to find Bluetooth LE devices";
var toast = Toast.Make(text, duration, fontSize);
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
await toast.Show(cancellationTokenSource.Token);
return;
}
else
{
string text = "Found Devices";
var toast = Toast.Make(text, duration, fontSize);
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
await toast.Show(cancellationTokenSource.Token);
// adapter.DeviceDiscovered += OnDeviceDiscovered;
foreach (var device in deviceList)
{
deviceList.Add(device);
}
}
}
async Task RequestBluetooth()
{
if (DeviceInfo.Platform != DevicePlatform.Android) { return; }
var Status = PermissionStatus.Unknown;
if (DeviceInfo.Version.Major >= 12)
{
Status = await Permissions.CheckStatusAsync();
if (Status == PermissionStatus.Granted)
{
return;
}
if (Permissions.ShouldShowRationale())
{
await Shell.Current.DisplayAlert("Permission needs", "Bluetooth", "Ok");
}
Status = await Permissions.RequestAsync();
}
else
{
if (Status != PermissionStatus.Granted)
{
await Shell.Current.DisplayAlert("Permission Required", "Bluetooth", "Ok");
}
}
}
Beta Was this translation helpful? Give feedback.
All reactions