Android forces you to activate location in order to use Bluetooth

Android forces you to activate location in order to use Bluetooth


Whereas creating cell capabilities I every and every so recurrently hear complaints very like: “Why this app asks for specific permissions as quickly as I neutral acceptable should join my BLE instrument?”

Instinctively, it’s neutral acceptable now not solely acceptable—you presumably really feel the an identical.

I desired to attain the beginning set up of this requirement and puzzled if one factor may properly moreover be achieved about it. Within the keep, it proved to be a captivating toddle. On this weblog put up, I’ll specific why achieve trusty of entry to to the specific is vital on Android to work with BLE. Then, I’ll degree to what now we pay money for tried to create in our makes an attempt to reassure all privacy-alive to clients. By the keep, you possibly can know easy methods to work during the titular restriction beneath an identical outdated situations.

Why on Android Map is former for BLE?!

I want the making use of would now not should set up aside a query to for specific achieve trusty of entry to. Sadly, it’s a must pay money for since BLE efficiency might be former to uncover the person. How the app may properly uncover the person? Let’s stare what lots of the apps that join with BLE peripheral create.

First, the person installs a model unique utility that’s devoted to work with their shiny unique peripheral. Sadly, the app can’t join with it neutral acceptable off the bat on epic of it might now not know the unfamiliar identifier that could be a actually vital to avoid wasting a connection. To achieve the data about this identifier, the making use of must first enterprise a scan (in Traditional Bluetooth nomenclature the formulation turned named discovery however it should additionally now be at a loss for phrases with firms discovery in BLE) and the plot will pay money for to callback the app each time it goes to pay money for to scan a model unique BLE commercial of each and every shut by instrument.

ScanCallback onScanResult will doubtless be often known as with a ScanResult which in flip comprises a reference to a BluetoothDevice representing the peripheral and a ScanRecord reference that comprises commercial knowledge. This knowledge is sufficient to join with the peripheral the person is interested in. Sadly, with this knowledge, there are now not decrease than two methods through which one may properly try to uncover the person:

  1. There might be a sequence of BLE beacon protocols that may be former to uncover the person by means of knowledge transmitted within the ScanRecord’s knowledge.
  2. Every BluetoothDevice has its maintain unfamiliar identifier often known as the MAC maintain. It is miles more likely to uncover the person given the data of BLE peripherals places and MAC addresses.

Alright. So right here is why the Android BLE scanning API utilization wants achieve trusty of entry to to the specific from the app.

What I net complicated and what makes the abilities from the person’s standpoint worse—the Android OS has its specific achieve trusty of entry to divided between two switches:

  1. Map Permissions—needed for the making use of to realize trusty of entry to at least one factor that’s specific linked.
  2. Map Firms—a additional non everlasting specific monitoring change which is on the whole referenced as a result of the “GPS change”.


I puzzled—what exactly is a really vital for the making use of to effectively scan a peripheral? Let’s ogle into the scientific docs.

The Android BluetoothLeScanner’s API documentation of startScan(File, ScanSettings, ScanCallback) methodology states:

An app should shield ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission in order to realize outcomes.

It seems to be now not solely lawful. I in actual fact pay money for tried to call the methodology with out permissions nevertheless it failed with an error code that claims: application didn’t register the scan. The above description would greater match the case when Map Firms are grew to change into off.

The perform-model telephones for Android OS (e.g. Pixels, Motorola) tend now to now not identify the listener with scan outcomes whereas totally different distributors/devices seem now to now not care in any respect and return the outcomes fortunately even when the Map Firms are off.

As routine, it’s all about discovering the certified steadiness between usability and privateness. In the intervening time, totally different Android OS implementations are extremely inconsistent.

Why iOS is totally different?

I work largely on Android capabilities however I ogle on iOS with envy on epic of it’s designed with privateness in ideas. Apple determined to strip unfamiliar instrument knowledge from the API—issues indulge in MAC maintain, some traits and knowledge types that may properly identify a peripheral are inaccessible. Moreover, every and every iOS instrument generates its maintain unfamiliar identifier (UUID) for every and every Bluetooth peripheral it scans—these generated UUIDs will doubtless be totally different on one other iOS instrument and because of this reality can’t be trackable. Beacon commercials are additionally unavailable amongst BLE scan outcomes—there’s a separate API for them which wants achieve trusty of entry to to the specific as properly.

All this sums as much as a instrument that presents a factual person abilities with out sacrificing its privateness.


Subsequent connections

I understand that the scanning diagram might be former to uncover the person so the specific achieve trusty of entry to is justified. Nonetheless then I perception: “Isn’t it doubtless, given the peripheral’s MAC maintain, to attach with out scanning? Android API has a risk to retrieve an occasion of BluetoothDevice given its MAC take care of in any case”.

That is lawful. Sadly, Android API additionally has a unsuitable flaw—it’s exposing a 48 bit MAC maintain with out knowledge about what make of maintain is that (additional information about addresses might be reward in Core Bluetooth Spec. v4.2 [Vol 6, Part B] 1.3).

You learn it neutral acceptable. The plot made by mighty Google has some broad shortcomings that are solely undocumented.

To effectively join with a BLE peripheral every and every the MAC maintain and its type are required. Nonetheless how Android OS solves this specific? It connects in any case.

In case the peripheral turned scanned its maintain type seems to be cached by the lower layers of BLE stack. Nonetheless, if cache has no knowledge a couple of express MAC maintain, the plot makes use of inside heuristics to net out the roughly maintain it’s working with—if the wager is lawful then a profitable connection will doubtless be established with out scanning—if now not one may properly set up aside a query to the obscure blueprint=133 (GATT_ERROR) being returned though the peripheral is promoting neutral acceptable beside the instrument.

I made a take a look at by connecting to a instrument with a acknowledged MAC maintain. From my abilities, Nordic Semiconductor nRF5 chips in default configuration are every and every so recurrently guessed badly. These chips are the exhaust of random addresses out of the sphere. Most presumably that is on epic of public maintain home pay money for to be bought from Bluetooth Signature Crew. As nRF chips are alleged to be programmed and former by third events, it’s now not of their trade to pay for the general public home.

Subsequently, Android caches the maintain type internally after the primary scan. Sadly (another time) caches may properly achieve cleared at some degree which leaves us with the sinful bug I in actual fact pay money for neutral acceptable described.

I requested myself: “Is it more likely to video show the cache validity?”. Current exams began.
Radiant that nRF gadgets don’t join with out their maintain type cached, I went with checking when the connections will open up to fail.

It snappy grew to change into out that subsequent connections on most gadgets may properly now not be profitable if the Bluetooth will doubtless be vitality cycled. Curiously sufficient, on some, it might quiet work till a whole change off turned made.

I puzzled if it should in all probability properly possibly be more likely to hint the second when a model unique scan have to be made to make the connection doubtless. Two doubts got here to my ideas:

  1. Extra moderen variations of the OS introduce background route of limitations

    1. A route of can’t flee with out a foreground notification
    2. Bluetooth adapter vitality specific broadcast can’t be registered within the AndroidManifest and because of this reality wake the app
  2. It is miles now not more likely to net out when the cache will get cleared with out experimentation

As a result of the making use of route of may properly achieve killed at random moments within the background, it can’t reliably observe whether or not or now not a scan is a really vital earlier than the connection—the solely protected formulation to reliably join is to repeatedly scan after the making use of open up. However one other grief.

Robust blueprint quo

Are we doomed to repeatedly set up aside a query to for the person achieve trusty of entry to to their specific to work with BLE peripherals? Is there no totally different formulation?

A while in the past, I turned requested this interrogate by absolutely considered one of our purchasers and have become about to combat in opposition to the plot. I already knew that the autoConnect flag, that’s handed when establishing the connection, changes one thing extra than impartial appropriate the interval of time desired to connect so I made up my ideas to play with it a shrimp.

I stumbled on out that when the exhaust of autoConnect=lawful we had been prepared to attach with our goal instrument reliably, although it needed an extended time. The primary suspicion turned that, on this self-discipline, the MAC maintain type decision might be deferred to the time of the primary discover of the peripheral.

Bluetooth Low Vitality is all about maintaining vitality. The gadgets try to sleep so long as doubtless and shield aware neutral acceptable for a brief timeframe after promoting—right here is the time a cellular phone may properly join with them. So every and every connection to a peripheral repeatedly begins with a Bluetooth scan. When the exhaust of autoConnect=lawful, a accountability cycle of scanning is considerably relaxed. I ran some exams when doing my first steps in BLE subject and stumbled on that it’s going to additionally take up to loads of minutes for a connection to be established on this case. It pay money for to be more likely to energy a excessive accountability scan by totally different methodology although.


The best way to scan with out achieve trusty of entry to to Map?

No particular person can exhaust BLE scan API with out a necessity Map Permissions, and, on most gadgets, it’s pointless with out energetic Map Firms. Nonetheless, Bluetooth Low Vitality is an extension to so-known as Bluetooth Traditional and curiously sufficient Google determined this API would now not want specific permissions to be often known as.

Diversified Bluetooth variations are largely backward properly matched. There might be a factual likelihood that every and every Traditional and LE scans are principally solely the an identical beneath the hood if a handset helps every and every. The solely distinction is that the caller will achieve urged in several methods.
Nonetheless, the Traditional model of the scan is now not going to return any BLE commercial packet knowledge. There’ll doubtless be no risk to detect in case your BLE instrument is promoting until its MAC maintain. This system solely works if utilized for subsequent connections to an already acknowledged peripheral.

It snappy grew to change into out that the BluetoothLeScanner javadoc annotation about permissions is a much bigger match for the conventional API. It principally is now not going to return any outcomes till the making use of will pay money for specific permissions nevertheless the scan seems to be working exactly (checked by subscribing to BluetoothAdapter.ACTION_DISCOVERY_STARTED and BluetoothAdapter.ACTION_DISCOVERY_FINISHED).


Making an attempt out BLE with out Map Permissions

Is it more likely to join reliably to a peripheral shining its MAC maintain solely? I wanted an reply to a spread of various handsets. Radiant knowledge from outdated paragraphs, I wrote an algorithm confirmed beneath in pseudocode (runnable code is accessible here):

bustle (
  const linked = autoconnect(deviceId)
  const stumbled on = classicBluetoothDiscovery().filter(deviceId)
  const timeout = timeoutAfter(30 seconds)

if (linked) => return


Before every and every take a look at, the Bluetooth cache turned cleared. Within the initiating, three issues occur:

  1. An oblique connection is began (autoConnect=lawful)
  2. A Bluetooth conventional scan is began
  3. A timeout diagram is began

If the connection will occur first, the exams keep. On some mobiles, the Traditional scan will return outcomes though specific achieve trusty of entry to is now not granted. If right here is so and the goal peripheral is stumbled on, the bustle terminates and the app can proceed to show connection. Timeout additionally finishes the bustle after which a acceptable away connection try is made (which pay money for to be faster). The take a look at measures the time desired to realize a sound connection.

The outcomes:

cellular phone model


maintain type cache sure second

average workaround connection time (s)

Nexus 5X


adapter cycle


Huawei P8 Lite


adapter cycle


OnePlus 3T


vitality cycle




vitality cycle


Asus Zenfone AR


vitality cycle




vitality cycle




adapter cycle


Moto X 2nd Gen


adapter cycle

(~90% success worth) 35

Nexus 6


adapter cycle


The workaround behaved considerably properly and I turned prepared to realize connections with out specific achieve trusty of entry to. There might be, on the other hand, one instrument (Moto X 2nd Gen) which repeatedly timed out and solely the 2nd, educate connection succeeds.

I had confirmed that the Motorola cellular phone would now not return any Traditional Bluetooth scan leads to principally the freshest setup. Moreover, now not absolutely among the many successes befell earlier than the timeout. The conclusion turned that second of maintain decision takes specific on the time of the decision, now not decrease than for this handset.

Anyway, the interval of time needed for connecting turned unacceptable offered that the take a look at peripheral turned repeatedly promoting neutral acceptable beside the cellular phone.

Even though our methodology turned now not an ultimate success, I stumbled on it to be promising and determined to dig deeper.

Current findings

Subsequent step I took turned to check how the maintain type decision is principally made. To create so, a search into the implementation of Android BLE stack turned needed. I confirmed that the plot does withhold a cache of resolved MAC maintain types. Moreover, if the maintain is now not cached, a “wager” is made; on all Android variations in educate connection mode (autoConnect=counterfeit), the kind is believed to be public. In oblique mode the subject is a shrimp totally different—because of one other bug (which by the way is hilarious because of an obvious misunderstanding of Core Bluetooth Spec. v4.2 [Vol 6, Part B] 1.3.2); on Android Nougat and Oreo the kind would default to random. On totally different OS variations, it might quiet be handled as public.

Getting help to the outdated desk it now had masses additional sense. The maintain type decision is repeatedly made on the time of the decision.

The workaround did work for me because of a couple of causes:

  1. Undocumented distinction linked to autoConnect flag and the kind decision.
  2. Some telephones being now not consequent with returning outcomes when there is no longer any achieve trusty of entry to to precise.
  3. Bluetooth Traditional scan additionally resolving BLE peripherals maintain type.

With this knowledge, it pay money for to be more likely to repeatedly join with any peripheral if its maintain type is acknowledged to the developer now not decrease than on Android 7 and eight. Gadgets that are returning scan outcomes with out specific achieve trusty of entry to would work as properly.


The lacking fragment

Now not too way back, I in actual fact pay money for realized that there definitely is a diagram to confirm whether or not or now not a express peripheral is already within the cellular phone’s maintain type cache. BluetoothDevice has a carry out getType() which returns if the peripheral is a ‘BLE’, ‘conventional’ or ‘twin-mode’ instrument. It should additionally return a fourth alternative although—unknown. It seems that unknown is returned each when Bluetooth is at the moment switched off or the instrument turned now not however set up aside into the cache. Bingo!

I need to point out that the above habits turned resolute by studying Android AOSP implementation. It is miles now not described within the documentation, because of this reality now not assured and may pay money for to alter within the kill.


With this unique knowledge, it’s in all probability going to realize rid of the maintain type decision bug! The connection, regardless of if oblique or educate mode, have to be requested solely after the peripheral is set up aside into the cache. Combining this with the pliability to flee a scan with out the necessity of specific achieve trusty of entry to now we pay money for an open path to jot down Android BLE apps that want the specific achieve trusty of entry to neutral acceptable for the preliminary connection so long as commercial knowledge is now not related afterwards. How chilly is that?! Specifically whereas quiet conserving person’s specific personal!

To create that effectively, critically within the background, is a completely totally different matter.
Assassinate it’s important to even pay money for questions? Are looking to hide a mistake or give attention to? For sure be at liberty to contact us — I’ll try to reply to as quickly as doubtless.


Please enter your comment!
Please enter your name here