13

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!

user2995035
  • 131
  • 1
  • 1
  • 5
  • The question is if BLE connection is maintained when device goes in sleep mode. And I asked it here http://stackoverflow.com/questions/29118771/what-happens-to-connection-with-ble-device-when-android-goes-in-sleep-mode/29119416#29119416 – Marian Paździoch Mar 18 '15 at 10:46

3 Answers3

2

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

user3698465
  • 185
  • 1
  • 10
  • Hi, do you have a background service that has a gattCallback connected to it , and once the service starts you call connectGatt with a remove device you created with mac address you have , autoConnect = true , and the relevant callbacks? – user3698465 Jan 31 '16 at 15:01
  • @user3698465 Hi, Do you have any sample ? – msk Jan 06 '19 at 13:24
1

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.

Community
  • 1
  • 1
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
0

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.

AllDayAmazing
  • 2,383
  • 1
  • 24
  • 25