searching nearby available bluetooth in Xamarin android 9

I want to search for available bluetooth but I can't find a solution that works in Xamarin android 9,

1 answer

  • answered 2020-02-13 01:39 Lucas Zhang - MSFT

    Firstly, add the following permissions to AndroidManifest.xml

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

    And for Bluetooth scan you could use the plugin Plugin.BLE from Nuget .

    Usage

    ObservableCollection<Plugin.BLE.Abstractions.Contracts.IDevice> deviceList;
    
    
    deviceList = new ObservableCollection<Plugin.BLE.Abstractions.Contracts.IDevice>();
    var adapter = CrossBluetoothLE.Current.Adapter;
    
    adapter.DeviceDiscovered += (s, a) =>
    {
       Console.WriteLine(a.Device.Name);
       deviceList.Add(a.Device);
    };
    await adapter.StartScanningForDevicesAsync();
    

    For more details and usage of the plugin you could check https://github.com/xabre/xamarin-bluetooth-le .