Roy Raz
Roy Raz

Reputation: 33

Xamrin.Android Bluetooth Pairing

I'm working on a Xamarin.Android project and I need to scan for nearby Bluetooth devices and after selecting one, pair it to my device. This is what I did so far:

AndroidManifest.xml

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

BluetoothDeviceReceiver.cs

public class BluetoothDeviceReceiver : BroadcastReceiver
{
    private static BluetoothDeviceReceiver _instance;
    public static BluetoothDeviceReceiver Receiver => _instance ?? (_instance = new BluetoothDeviceReceiver());

    public override void OnReceive(Context context, Intent intent)
    {
        var action = intent.Action;

        if (action != BluetoothDevice.ActionFound)
        {
            return;
        }

        // Get the device
        var device = (BluetoothDevice)intent.GetParcelableExtra(BluetoothDevice.ExtraDevice);
    }
}

MainScreenView.cs

protected override void OnResume()
    {
        base.OnResume();
        RegisterReceiver(BluetoothDeviceReceiver.Receiver, new IntentFilter(BluetoothDevice.ActionFound));
    }

    protected override void OnPause()
    {
        base.OnPause();
        UnregisterReceiver(BluetoothDeviceReceiver.Receiver);
    }

On button command:

BluetoothAdapter.DefaultAdapter.StartDiscovery();

I placed a breakpoint in the OnReceive method but it never reached there.

What am I missing here?

UPDATE

The device I worked on has Android 8.0.0 version. It doesn't work only on that device. When switching to a different Android version, the same solution worked fine. I need to find out why it's happening

Upvotes: 0

Views: 1646

Answers (1)

Leo Zhu
Leo Zhu

Reputation: 15031

For Android 6 and above, you also need to request ACCESS_FINE_LOCATION and ACCESS_COARSE_LOCATION permissions at runtime. refer to RuntimePermissions

or you could use the nugetpackage Plugin.Permissions to request runtime permissions(Permission.Location) refer to Plugin.Permissions

after obtaining runtime permissions:

  BluetoothManager bluetoothManager = (BluetoothManager)GetSystemService(Context.BluetoothService);
  BluetoothAdapter mBluetoothAdapter = bluetoothManager.Adapter;                   
      if (!mBluetoothAdapter.IsEnabled)
         {
           Intent enableBtIntent = new Intent(BluetoothAdapter.ActionRequestEnable);
           StartActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
         }
      else
         {
           mBluetoothAdapter.StartDiscovery();
         }


  protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        // User chose not to enable Bluetooth.
        if (requestCode == REQUEST_ENABLE_BT && resultCode == Result.Canceled)
        {
            Finish();
            return;
        }

        base.OnActivityResult(requestCode, resultCode, data);
    }

Upvotes: 0

Related Questions