Hello, I have a Problem
I have a Bluetooth service for Android and is doesn't find any device
using Android.Bluetooth;
using Android.Content;
namespace SnapLabel.Platforms.Android {
public class AndroidBluetoothScanner : IBluetoothService {
public event Action<BluetoothDeviceModel>? DeviceFound;
private readonly BluetoothAdapter _bluetoothAdapter = BluetoothAdapter.DefaultAdapter;
private readonly HashSet<string> _seenDevices = [];
private BroadcastReceiver? _receiver;
public void StartScan() {
if(_bluetoothAdapter is null || !_bluetoothAdapter.IsEnabled)
return;
if(!BluetoothPermissionHelper.EnsureBluetoothScanPermission()) {
return;
}
_seenDevices.Clear();
_receiver = new BluetoothScanReceiver(device => {
if(_seenDevices.Add(device.Address)) {
DeviceFound?.Invoke(device);
}
});
var filter = new IntentFilter(BluetoothDevice.ActionFound);
Platform.AppContext.RegisterReceiver(_receiver, filter);
_bluetoothAdapter.StartDiscovery();
}
public void StopScan() {
if(_bluetoothAdapter?.IsDiscovering == true)
_bluetoothAdapter.CancelDiscovery();
if(_receiver != null) {
Platform.AppContext.UnregisterReceiver(_receiver);
_receiver = null;
}
}
private class BluetoothScanReceiver(Action<BluetoothDeviceModel> onDeviceFound) : BroadcastReceiver {
public override void OnReceive(Context context, Intent intent) {
if(intent.Action != BluetoothDevice.ActionFound)
return;
var device = (BluetoothDevice?)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
if(device == null || string.IsNullOrWhiteSpace(device.Name))
return;
onDeviceFound(new BluetoothDeviceModel {
Name = device.Name,
Address = device.Address!,
FontIcon = GetFontIcon(device)
});
}
private static string GetFontIcon(BluetoothDevice device) {
var btClass = device.BluetoothClass;
if(btClass == null)
return FontsConstants.Bluetooth;
int major = (int)btClass.MajorDeviceClass;
return major switch {
1 => FontsConstants.Computer,
2 => FontsConstants.Smartphone,
4 => FontsConstants.Headphones,
5 => FontsConstants.Mouse,
6 => FontsConstants.Print,
7 => FontsConstants.Keyboard,
_ => FontsConstants.Bluetooth_audio
};
}
}
}
}
I also Have a service for windows, and it finds things
using Windows.Devices.Bluetooth;
using Windows.Devices.Enumeration;
namespace SnapLabel.Platforms.Windows {
/// <summary>
/// Windows-specific implementation of IBluetoothService.
/// Uses DeviceWatcher to discover nearby Bluetooth devices.
/// </summary>
public class WindowsBluetoothScanner : IBluetoothService {
public event Action<BluetoothDeviceModel>? DeviceFound;
private DeviceWatcher? _watcher;
private bool _keepScanning;
private readonly HashSet<string> _seenDevices = [];
public void StartScan() {
_keepScanning = true;
_seenDevices.Clear();
StartWatcher();
}
public void StopScan() {
_keepScanning = false;
if(_watcher != null &&
(_watcher.Status == DeviceWatcherStatus.Started ||
_watcher.Status == DeviceWatcherStatus.EnumerationCompleted)) {
_watcher.Stop();
_watcher = null;
}
}
private void StartWatcher() {
string selector = "System.Devices.Aep.ProtocolId:=\"{e0cbf06c-cd8b-4647-bb8a-263b43f0f974}\"";
_watcher = DeviceInformation.CreateWatcher(
selector,
[
"System.Devices.Aep.DeviceAddress",
"System.ItemNameDisplay"
],
DeviceInformationKind.AssociationEndpoint
);
_watcher.Added += async (s, deviceInfo) => {
string name = !string.IsNullOrWhiteSpace(deviceInfo.Name)
? deviceInfo.Name
: deviceInfo.Properties.TryGetValue("System.Devices.Aep.DeviceAddress", out var addr)
? addr?.ToString() ?? "Unknown"
: "Unknown";
if(name.EndsWith("(Bluetooth)", StringComparison.OrdinalIgnoreCase))
name = name.Replace("(Bluetooth)", "").Trim();
if(!_seenDevices.Add(name))
return;
string fontIcon = FontsConstants.Bluetooth;
try {
var btDevice = await BluetoothDevice.FromIdAsync(deviceInfo.Id);
if(btDevice != null) {
uint major = (uint)btDevice.ClassOfDevice.MajorClass;
fontIcon = major switch {
1 => FontsConstants.Computer,
2 => FontsConstants.Smartphone,
4 => FontsConstants.Headphones,
5 => FontsConstants.Mouse,
6 => FontsConstants.Print,
7 => FontsConstants.Keyboard,
_ => FontsConstants.Bluetooth_audio
};
}
} catch(Exception ex) {
Debug.WriteLine($"[WARN] Could not get ClassOfDevice for {name}: {ex.Message}");
}
DeviceFound?.Invoke(new BluetoothDeviceModel {
Name = name,
Address = deviceInfo.Properties.TryGetValue("System.Devices.Aep.DeviceAddress", out var a)
? a?.ToString() ?? ""
: "",
FontIcon = fontIcon
});
};
_watcher.EnumerationCompleted += (s, e) => {
Debug.WriteLine("[INFO] Enumeration completed. Watching for new devices...");
};
_watcher.Updated += (s, update) => {
// Optional: handle property changes
};
_watcher.Removed += (s, update) => {
// Optional: handle device removal
};
_watcher.Start();
}
}
}

As you can see my windows app is crazy fast, but android doesn't find anything