user2995035
user2995035

Reputation: 131

Android: Is it possible to automatically connect to Bluetooth LE device when it comes in range and starts advertising ?

I'm looking for a way to automatically connect Android device to a Bluetooth LE (4.0 Smart) device when it comes in range (and starts advertising). I expect it to work like a Bluetooth headphones - when turned on they automatically connect to a paired phone (if it is in range).

I thought about running a Service in background that will periodically (every 50-100ms) check for advertisement. However one of the main concerns is battery life of Android device.

Could someone please advice what would be the best way to implement such functionality on Android 4.3 and up ?

Thanks in advance!

Upvotes: 13

Views: 17604

Answers (3)

user3698465
user3698465

Reputation: 195

set autoConnect parameter in connectGatt() to true. (second parameter)

Upvotes: 2

AllDayAmazing
AllDayAmazing

Reputation: 2383

Actually, that only works for standard Bluetooth. For Bluetooth smart ACTION_ACL_CONNECTED/ACTION_ACL_DISCONNECTED broadcasts do not work.

For BLE you must work with the APIs and callbacks in mBluetoothAdapter.startLeScan(mLeScanCallback); once a device is found.

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234847

You can register a broadcast receiver to receive broadcasts when a Bluetooth device is found. See the API guide topic Bluetooth for details and sample code.

Also look at the topic Bluetooth Low Energy.

Finally, take a look at this thread, which is basically the same question.

Upvotes: 1

Related Questions