Accessing Bluetooth
Data sometimes needs to be transferred to another device, but to one where there is no network infrastructure. We may therefore want to integrate Bluetooth into our app to transfer data or to interact with various devices that cannot be accessed via the Internet or a local network.
How to do it...
To interact with the various Bluetooth services, we need the BluetoothAdapter
instance:
As with most hardware services, we need to request permission to access Bluetooth:
[assembly: UsesPermission(Manifest.Permission.Bluetooth)]
If we want to directly control the Bluetooth hardware, we need to specify an additional permission:
[assembly: UsesPermission( Manifest.Permission.BluetoothAdmin)]
Once we have the permissions we need, we obtain the
BluetoothAdapter
instance:var bluetooth = BluetoothAdapter.DefaultAdapter; if (bluetooth == null) { // device does not support Bluetooth } else { State state = bluetooth.State; }
If Bluetooth is not enabled, we can request the user to enable...