Share via

Android Bluetooth help

Eduardo Gomez 4,316 Reputation points
2025-09-26T09:15:49.2933333+00:00

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();
        }
    }
}

Untitled video - Made with Clipchamp

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

Developer technologies | .NET | .NET Multi-platform App UI

Answer accepted by question author

Michael Le (WICLOUD CORPORATION) 11,325 Reputation points Microsoft External Staff Moderator
2025-09-29T09:12:31.9133333+00:00

Hello Eduardo,

Thank you for reaching out.

After reviewing everything, the issue is not with your code but with the environment you are testing in.

See https://stackoverflow.com/questions/22604305/how-to-use-android-emulator-for-testing-bluetooth-appl… for more details.

The standard Android Emulator does not have the capability to use your computer's physical Bluetooth adapter to scan for or connect to real-world Bluetooth devices. While your code can call the Bluetooth APIs without crashing, the emulator has no actual radio hardware to receive signals from nearby devices. This is why your scan is starting but never finding anything.

To properly test your Bluetooth scanning feature, you will need to deploy and run the application on a physical Android device.

Once you run it on a real phone or tablet, your current code should be able to discover Classic Bluetooth devices. As mentioned in the previous answer, if you also need to find modern accessories like earbuds or trackers (which use Bluetooth LE), you will eventually need to implement the BluetoothLeScanner as well. However, the first step is to move your testing to a physical device.

I hope this clears things up.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.