Android Nearby Devices Permission Bluetooth Permissions and Setup Explained

Author

Reads 630

Close-up of a Bluetooth code reader tool connected inside a vehicle, illuminated by a blue light.
Credit: pexels.com, Close-up of a Bluetooth code reader tool connected inside a vehicle, illuminated by a blue light.

To use Bluetooth on your Android device, you need to grant the "Location" and "Bluetooth" permissions. This allows your device to access nearby devices and their locations.

The "Location" permission is required for Bluetooth Low Energy (BLE) scanning, which is used to discover and connect to nearby devices. This permission is necessary because BLE scanning relies on the device's location to determine the proximity of other devices.

Granting the "Bluetooth" permission allows your device to turn on the Bluetooth hardware and connect to nearby devices. This permission is required for all Bluetooth-related features, including file transfer and audio streaming.

To set up Bluetooth permissions on your Android device, go to Settings > Location > Location services and toggle the switch to the "On" position. You'll also need to go to Settings > Connections > Bluetooth and toggle the switch to the "On" position.

On a similar theme: Android Go Devices

Declaring Permissions

Declaring the Bluetooth permission BLUETOOTH is essential to use Bluetooth features in your application.

Credit: youtube.com, What Is Nearby Devices Permission In Android? - Be App Savvy

You need to declare this permission to perform any Bluetooth communication, such as requesting a connection, accepting a connection, and transferring data.

If you want your app to initiate device discovery or manipulate Bluetooth settings, you must also declare the BLUETOOTH_ADMIN permission.

This permission is necessary for discovering local Bluetooth devices, but other abilities granted by this permission should only be used if your application is a "power manager" that modifies Bluetooth settings upon user request.

To declare Bluetooth permissions, you must do so in your application manifest file.

Explore further: Android Auto Settings

Why Are Permissions Crucial?

Permissions are foundational to building BLE apps, directly influencing your app’s ability to access BLE hardware.

Permissions are critical for building privacy and trust into your app, especially when it comes to accessing sensitive user data like location.

Android’s permissions framework ensures that users stay in control of their data, and it’s your responsibility as a developer to design an app that aligns with these expectations.

Each new Android version introduces new rules or adjustments that can impact how your app functions, making it essential to stay up-to-date on Android permissions requirements.

Falling behind on these changes can lead to errors, crashes, or even the inability for your app to access BLE hardware at all.

Declaring Permissions in AndroidManifest.xml

Credit: youtube.com, Troubleshooting INTERNET Permission in Your AndroidManifest.xml

Declaring permissions in the AndroidManifest.xml file is crucial for your app's functionality and user experience.

To declare the Bluetooth permission, you must include the BLUETOOTH permission in your application manifest file.

This permission is necessary for any Bluetooth communication, such as requesting a connection, accepting a connection, and transferring data.

If your app needs to initiate device discovery or manipulate Bluetooth settings, you must also declare the BLUETOOTH_ADMIN permission.

The BLUETOOTH_ADMIN permission is required solely for the ability to discover local Bluetooth devices, unless your app is a "power manager" that will modify Bluetooth settings upon user request.

A different take: Bluetooth Dual Audio App

Requesting User Permissions

Requesting user permissions is a crucial step in developing an Android app that uses Bluetooth to interact with nearby devices. Permissions are foundational to building BLE apps, directly influencing your app's ability to access BLE hardware.

To request user permissions, you must declare the necessary permissions in your application manifest file. You need the BLUETOOTH permission to perform any Bluetooth communication, such as requesting a connection or transferring data.

Credit: youtube.com, Galaxy S23's: How to Allow/Don't Allow Nearby Devices Permission

Declaring the BLUETOOTH_ADMIN permission is also required if you want your app to initiate device discovery or manipulate Bluetooth settings. Most applications need this permission solely for the ability to discover local Bluetooth devices.

Implementing permissions thoughtfully during development helps you create an app that is transparent, respects user privacy, and builds trust. By customizing your app permissions, you maintain control over your data while enabling apps to offer the features you value.

Regularly reviewing and adjusting permissions is a good habit for maintaining both security and efficiency on your Android device. This ensures your app remains secure, compliant, and functional, delivering the best possible experience to your users.

Bluetooth Setup

To set up Bluetooth on your Android app, you'll need to check if Bluetooth is enabled and prompt the user to turn it on if it's not. This can be done using the BluetoothAdapter.ACTION_REQUEST_ENABLE intent action, but you'll need the BLUETOOTH_CONNECT permission on Android 12 and above.

You'll also need to declare the necessary Bluetooth permissions in your app's manifest file, including BLUETOOTH and BLUETOOTH_ADMIN if you plan to discover devices or manipulate Bluetooth settings.

Ensuring Bluetooth Is Enabled

Credit: youtube.com, How to Enable Bluetooth Pair Compatible Devices in Android

If your app relies on Bluetooth being enabled, you'll want to ensure users keep it on while using your app. Fortunately, Android provides an Intent action to prompt users to turn on Bluetooth.

To check if Bluetooth is enabled, you can use the BluetoothAdapter in your Activity's onResume() method. If it's not enabled, you can display an alert to prompt the user to turn it on.

You'll need the BLUETOOTH_CONNECT permission to use this Intent action on Android 12 and above. If you don't have this permission, the app will fail to launch the Intent.

To request the user to enable Bluetooth, you can use the ACTION_REQUEST_ENABLE Intent action. This will display a system alert prompting the user to turn on Bluetooth.

If the user dismisses the dialog, the app will keep displaying the same alert until they accept the recommendation to turn on Bluetooth. In a production app, you can create a more sophisticated UX around this, such as displaying an informational alert or kicking off a user education flow.

Enabling Discoverability

Credit: youtube.com, How do I turn on discoverable mode?

To make your device discoverable to other devices, you'll need to call startActivityForResult with the ACTION_REQUEST_DISCOVERABLE action Intent. This will issue a request to enable discoverable mode through the system settings.

By default, the device will become discoverable for 120 seconds, but you can define a different duration by adding the EXTRA_DISCOVERABLE_DURATION Intent extra. You can set the duration to as long as 3600 seconds, or a value of 0 to make the device always discoverable.

A dialog will be displayed, requesting user permission to make the device discoverable, and if the user responds "Yes", the device will become discoverable for the specified amount of time. If the user responds "No" or if an error occurs, the result code will be RESULT_CANCELED.

Note that if Bluetooth has not been enabled on the device, enabling device discoverability will automatically enable Bluetooth. The device will silently remain in discoverable mode for the allotted time, unless you register a BroadcastReceiver for the ACTION_SCAN_MODE_CHANGED Intent to be notified when the discoverable mode has changed.

Vendor-Specific AT Commands

Credit: youtube.com, Bluetooth HC-06 2018 latest v3.0 AT commands.

Vendor-Specific AT Commands can be received by applications starting in Android 3.0. This allows applications to receive system broadcasts of pre-defined vendor-specific AT commands sent by headsets.

Some headsets, like Plantronics, send +XEVENT commands that can be received by applications. These commands can indicate a connected device's battery level.

To handle vendor-specific AT commands, you need to create a broadcast receiver for the ACTION_VENDOR_SPECIFIC_HEADSET_EVENT intent. This will allow your application to receive and respond to these commands.

For example, you could use this feature to notify the user of a connected device's battery level. This can be a useful feature for applications that need to interact with headsets.

A different take: Connected Devices Android

Android Studio and Permissions

Android Studio may emit a "MissingPermission" warning for classes that utilize the Android Bluetooth APIs without an explicit runtime permission check.

To resolve this, you can add a @SuppressLint annotation to suppress the warning. This is particularly useful for classes that encapsulate all the BLE scanning and connecting logic in one or two main classes.

Nearby Devices

Credit: youtube.com, Nearby device scanning setting || how to use nearby device scanning setting on Samsung

Nearby Devices are a convenient way to connect to Bluetooth or other nearby devices. However, be cautious when granting this to apps that don’t require it.

To prevent unwanted pairing or data sharing, it's essential to understand how Nearby Devices work. Android-powered devices are not discoverable by default, and a user must make the device discoverable for a limited time through the system settings or an application can request that the user enable discoverability without leaving the application.

Expand your knowledge: Scan Nearby Devices Android

Article Summary

This article is designed to be a practical, code-focused resource for managing BLE permissions on Android, cutting through complexity to provide clear, actionable solutions.

We assume you already have your BLE project set up, so we can dive straight into the intricacies of permissions handling.

If you're still in the early stages and need help getting started, it's recommended to check out the Ultimate Guide to Android BLE, which covers project setup in detail.

This guide will equip you with the knowledge to handle BLE permissions setup, runtime management, and user education, taking your BLE app to the next level.

Nearby Devices

Credit: youtube.com, Galaxy S22/S22+/Ultra: How to Allow/Don't Allow Nearby Devices Permissions

Nearby Devices can be a blessing and a curse. They're a great way to connect with other devices, but you have to be careful about what you're allowing access to.

Nearby Devices allows you to connect to Bluetooth or other nearby devices. Be cautious when granting this to apps that don’t require it.

You can find remote Bluetooth devices either through device discovery or by querying the list of paired (bonded) devices. Device discovery is a scanning procedure that searches the local area for Bluetooth enabled devices.

A Bluetooth device will respond to a discovery request only if it is currently enabled to be discoverable. If a device is discoverable, it will respond to the discovery request by sharing some information, such as the device name, class, and its unique MAC address.

Once a connection is made with a remote device for the first time, a pairing request is automatically presented to the user. When a device is paired, the basic information about that device is saved and can be read using the Bluetooth APIs.

To be paired means that two devices are aware of each other's existence, have a shared link-key that can be used for authentication, and are capable of establishing an encrypted connection with each other.

Connecting

Credit: youtube.com, Galaxy S23's: How to Turn On/Off Nearby Device Scanning

Connecting to nearby devices can be a bit tricky, but don't worry, I've got you covered. To create a connection between two devices, you must implement both the server-side and client-side mechanisms.

One device must open a server socket and the other one must initiate the connection using the server device's MAC address. This is because one device must open a server socket and the other one must initiate the connection.

The server device will receive the required BluetoothSocket when an incoming connection is accepted, while the client device will receive it when it opens an RFCOMM channel to the server.

The connection procedure will block until the user has successfully paired, or will fail if the user rejects pairing, or if pairing fails or times out.

Here are the basic steps to initiate a connection with a remote device:

  1. Get a BluetoothDevice object that represents the remote device.
  2. Use the BluetoothDevice to acquire a BluetoothSocket and initiate the connection.

You should always ensure that the device is not performing device discovery when you call connect(). If discovery is in progress, then the connection attempt will be significantly slowed and is more likely to fail.

Credit: youtube.com, How Hackers are Hacking Your Phone via Bluetooth

When you're done with your BluetoothSocket, always call close() to clean up. Doing so will immediately close the connected socket and clean up all internal resources.

Here's a basic example of a thread that initiates a Bluetooth connection:

Notice that cancelDiscovery() is called before the connection is made. You should always do this before connecting and it is safe to call without actually checking whether it is running or not.

Apps Need Updates for New Permissions

Apps will need to update to use the new Bluetooth Scan and Bluetooth Connect permissions.

Developers will need to update their apps to use these new permissions, which means you won't see the new feature pop up until apps are compatible.

The new permissions are a step in the right direction when it comes to privacy, as they'll reduce the number of apps that can access your location.

Currently, there are no apps that are compatible with these new permissions, so you won't see the new feature yet.

The good news is that this means less apps will be using your location, which also saves some battery.

Check this out: Android Auto 14.0 Update

Frequently Asked Questions

Should nearby device scanning be on or off?

Turn off nearby device scanning to protect your phone from external vulnerabilities and conserve battery life

Willie Walsh

Junior Assigning Editor

Willie Walsh is an accomplished Assigning Editor with a keen eye for detail and a passion for delivering high-quality content. With a strong background in research and editing, Willie has honed their skills in identifying and assigning relevant topics to writers. Willie's expertise spans a wide range of categories, including technology, productivity, and education.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.