0

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

Roy Raz
  • 33
  • 1
  • 8

1 Answers1

1

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);
    }
Leo Zhu
  • 15,726
  • 1
  • 7
  • 23
  • 1
    Now what's the problem? Do you make sure the bluetooth is on when you match the device ?You can make a judgment before the match. I will update the judgment code in the answer ! – Leo Zhu Feb 27 '19 at 02:23
  • Yes, the Bluetooth is ON and if I'm searching from the OS I see all the devices. – Roy Raz Feb 27 '19 at 07:30