if either is denied permission, request permission from the user if (coarseLocationPermissionGranted = Permission. check if the app has permission to access fine location var fineLocationPermissionGranted = ContextCompat. check if the app has permission to access coarse location var coarseLocationPermissionGranted = ContextCompat. activity_main) Ĭonst int locationPermissionsRequestCode = 1000 Protected override void OnCreate( Bundle savedInstanceState) Private BluetoothDeviceReceiver _receiver
If they aren’t, you need to add code to prompt the user to grant these permissions.
This is the bit that is sometimes missed in other tutorials, and that’s possibly because this is only needed for more recent versions of Android, so older tutorials wouldn’t have needed this step.īasically even though the Access Coarse and Fine Location permissions are already specified in the AndroidManifest.xml file, if you’re using later than version 23 of the Android SDK, you need to also check that the permissions are correctly set at runtime. Finally – check permissions are applied at runtime ‘Bonded’) and again my class just writes some details to the console about the Bluetooth device that its found.īut we’re not quite done yet – there’s one more very important piece of code which is necessary for modern versions of Android. Then it checks that the devices are not already paired (i.e. When the Android device finds a new Bluetooth device and calls the OnReceive method, the class checks that the event is definitely the right one (i.e. RegisterReceiver(_receiver, new IntentFilter( BluetoothDevice.
_receiver = new BluetoothDeviceReceiver() Register for broadcasts when a device is discovered
The code below shows how to register the receiver to trigger when a Bluetooth device is detected. Xamarin.Android does this through something called an ‘Intent’. This receiver class is registered with the application and told to activate when the Android device detects specific events – such as finding a new Bluetooth device. IsEnabled)įoreach ( var pairedDevice in BluetoothAdapter. DefaultAdapter != null & BluetoothAdapter.
Obviously you’d probably want to write the output to a UI component rather than write to the console, but for this article I wanted to cut this down to only the Bluetooth interactions and not focus on UI interactions. It will not list other devices which haven’t already been paired with each other (I write about this later in the article). This part is very straightforward – remember the code below will list to console only the Bluetooth devices which have already been detected and paired with the Android device. List the Bluetooth devices which the Android device has already paired with I’ve pasted my AndroidManifest.xml file below – yours will look slightly different, but I’ve highlighted the important bit in red. When the application loads on the Android device for the first time, the user will be challenged to allow the application permission to use these hardware services. For detecting and interacting with Bluetooth hardware, there are four services to add to the application AndroidManifest.xml:
Update AndroidManifest.xml with Bluetooth and Location permissionsįirst I had to make sure that my application told the device what hardware services it needed to access. One thing to note – I can test detecting Bluetooth devices by deploying my code directly onto an Android device, but I can’t use the Android emulator as it doesn’t have Bluetooth support.Īs usual I’ve uploaded my code to GitHub ( you can get it here). In this post I’ll write about the steps needed to successfully use the Bluetooth hardware on your Android phone with.
I’m working on an Xamarin.Android app at the moment – for this app, I need to detect what Bluetooth devices are available to my Android phone (so the user can choose which one to pair with).įor modern versions of Android, it’s not as simple as just using a BroadcastReceiver (although that is part of the solution).