-
-
Notifications
You must be signed in to change notification settings - Fork 209
Permissions
When targetting Android SDK 31 and above you can select the required permissions from here to add to your manifest a complete example with legacy support would look like:-
<!-- Request legacy Bluetooth permissions on older devices. -->
<uses-permission android:name="android.permission.BLUETOOTH"
android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"
android:maxSdkVersion="30" />
<!-- Needed only if your app looks for Bluetooth devices.
If your app doesn't use Bluetooth scan results to derive physical
location information, you can strongly assert that your app
doesn't derive physical location. -->
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" />
<!-- Needed only if your app makes the device discoverable to Bluetooth
devices. -->
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<!-- Needed only if your app communicates with already-paired Bluetooth
devices. -->
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<!-- Needed only on SDK 31 and above if your app uses Bluetooth scan results to derive physical location. -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
When targeting Android SDK 30 and earlier you must add the BLUETOOTH permission and ACCESS_FINE_LOCATION if you want to do device discovery.
See the Android documentation for more details.
For Bluetooth Classic there is no direct API but 32feet.NET uses the ExternalAccessory API. To add support for a particular device type you must add a key to the Info.plist with the vendor specific protocol. For example a Zebra Bluetooth printer would require
<key>UISupportedExternalAccessoryProtocols</key>
<array>
<string>com.zebra.rawport</string>
</array>
For Bluetooth LE you must add a description to be displayed to the user when Bluetooth functionality is requested:-
<key>NSBluetoothAlwaysUsageDescription</key>
<string>This app requires Bluetooth to demonstrate the Bluetooth LE APIs</string>
If targeting devices prior to iOS 13 you must also add:-
<key>NSBluetoothPeripheralUsageDescription</key>
<string>This app requires Bluetooth to demonstrate the Bluetooth LE APIs</string>
See the Apple documentation for more details.
For Bluetooth LE you must add a description to be displayed to the user when Bluetooth functionality is requested. Add the following to your Info.plist:-
<key>NSBluetoothAlwaysUsageDescription</key>
<string>This app requires Bluetooth to demonstrate the Bluetooth LE APIs</string>
Check the Bluetooth capability from the Package.appxmanifest editor or add directly to the raw file like so
<Capabilities>
<Capability Name="Bluetooth"/>
</Capabilities>
For desktop apps, including WinUI 3, WPF, WinForms etc, the app has full trust and the capability does not need to be set.
When the platform requires a runtime request - to prompt the user for permission - you must call the relevant native API to request the permission. For cross-platform development there are a number of options to simplify this.
Use Xamarin.Essentials and the Permissions class. InTheHand.Bluetooth.Permissions provides a plug-in permission for Bluetooth. See the Xamarin Essentials documentation for more details.
PermissionStatus status = await Permissions.RequestAsync<InTheHand.Bluetooth.Permissions.Bluetooth>();
Similarly .NET MAUI contains an infrastructure for permissions, but prior to .NET 8.0 this requires a third-party extension. InTheHand.Bluetooth.Permissions provides a plug-in permission for Bluetooth. See the .NET MAUI Permissions documentation for more details.
PermissionStatus status = await Permissions.RequestAsync<InTheHand.Bluetooth.Permissions.Bluetooth>();
32feet.NET - Personal Area Networking for .NET
In The Hand Ltd