14

Goal:

I'm trying to get my android service to auto-connect to an already paired bluetooth device when its in range.

Example:

I have my iphone paired to my car. When I sit down in my car music starts automatically playing. I don't need to connect it again. I don't need to start a song. It just launches itunes and starts playing music. I want this kind of connectivity in my Android application.

One obvious solution is to have a thread running in a background services that tries to connect every X seconds. This however is not optimal due to the toll on battery life.

I tried setting up broadcast receivers to get triggered on ACTION_ACL_CONNECTED but this seems to only get called after I connect to the device.

Is there no way my app can get a notification when the phone walks into range of the device?

I've spent the better part of the last two days trying to get this to work with no luck. There are a billion tutorials on how to connect to a bt device but none to auto connect when in range. Starting to question if this even possible.. I'll be blown away if its not :(

related questions:

Find already paired bluetooth devices automatically, when they are in range

Community
  • 1
  • 1
Mahir
  • 215
  • 1
  • 4
  • 10

3 Answers3

7

As far as I know there is no way to automatically connect to a generic Bluetooth device as soon as it's in range. Your best bet is to have an inquiry loop, periodically querying available devices and connecting to ones you are bonded with (although, as you said, this is fairly battery intensive).

However, if your device (both the phone and the peripheral) support Bluetooth 4.0 (LE) as well as GATT, you can use connectGatt method that will automatically connect as soon as the device becomes available (again, not too sure about battery implications although limited testing I have done thus far shows that it is not significant).

I'm not entirely certain what the behaviour of connectGatt is when device is non-GATT but it may be worth investigating - my guess would be that you'd receive a callback with status GATT_FAILURE when device becomes in range but doesn't support GATT (this would at least give you an indication of when to connect).

If you find a solution or investigate the behaviour of this method on non-LE / non-GATT devices please do update your question as I'd be quite curious to find out more about it.

Bart Platak
  • 4,387
  • 5
  • 27
  • 47
  • Thanks Bart. I'll def give GATT a try. Any chance you have some insight on how scenario 3 would be implemented here (http://stackoverflow.com/questions/17810582/find-already-paired-bluetooth-devices-automatically-when-they-are-in-range). If I create a Server socket to detect when I'm in range and a regular BT socket to connect (not even sure if I understand it) – Mahir Nov 10 '14 at 06:12
  • BLE devices supporting connections are required to support GATT I believe, so that shouldn't be an issue. – Emil Apr 14 '21 at 07:40
2

As mentioned before, there’s no easy way to do that with Bluetooth Classic other than by constantly scanning.

The car radio scenario you mentioned, however, works with some car radios—mine automatically establishes a Bluetooth connection with my phone as soon as I turn the radio on. I have observed similar behavior with Bluetooth keyboards.

I can only speculate that these peripherals periodically try to connect to paired devices until it is sucessful. This makes sense for a peripheral in certain conditions:

  • Draining the battery isn’t a concern. (The radio runs off a car battery, and most of the time it is on, the engine is running and the battery gets charged.)
  • The peripheral talks to only one phone at a time, and can thus suspend its scans while it is connected to one. (This particular radio can only handle one phone at a time.)
  • The peripheral is mostly useless when not connected to another device, and can thus be expected to be connected to something for most of the time it is powered on. (This goes for headsets, keyboards, mice and the like.)

Obviously, that depends on your ability to influence the behavior of the paired device. If it’s an off-the-shelf device, it either works or it doesn’t—but if you’re designing a custom peripheral, it’s worth considering.

user149408
  • 5,385
  • 4
  • 33
  • 69
0

And what about another case - as soon as BT device X connects with the phone, connect to BT device Y when in range (and start latest playing app)?

The real life situation: in my Volvo car I have a built in BT for calls and I have added additional BT for audio. The internal BT connects to the phone as soon as I start a car. But the added one does not.

Bodi
  • 1