17

I am no Bluetooth specialist and wondering what possibilities are available to find already paired Bluetooth devices automatically when they are range of each other.

Background: In our case an Android application needs to connect to a dedicated accessory via Bluetooth (Rfcomm). Both devices are known to each other (they are paired). The Android application registers a broadcast receiver. During the startup of the application, the app initiates a discovery to find the dedicated accessory. If the accessory is in range everything works great.

Problem: The user starts the application outside the range of the dedicated accessory. The Android application tries to discover the accessory without success. Then the user goes into the range of the Bluetooth accessory. The broadcast receiver won’t get notified about the accessory that is in range now.

Similar Thread / Possible Solutions Similar questions were already asked on stackoverflow (e.g. autoconnect to bluetooth device when in range). But continuously trying to discover Bluetooth devices in range isn't what I am looking for because this would cause too much battery drain of the Android device.

Another solution would be to try to connect to the paired device in the onResume method of the Activity. This would work but has the disadvantage that the application can’t run in the background. So the user had to bring the application at least once to the foreground to initiate the connection.

A third idea I thought about is to implement a server socket into the Android application too. When the android application is started and the discovery finished without success, the Android application could create server socket and to listen to incoming notifications of the accessory. This would help in some scenarios (e.g. the user starts his application, approaches the accessory, activates the accessory and the accessory notifies the application on startup that it is in range now). But this is still no 100% solution because both devices can start outside the range of each other. Also it would be mandatory to implement additional functionality (Bluetooth server socket in the Android device…).

So I am wondering if better solutions exist. I am looking for a solution where no additional ServerSockets are required and I always get the notification that the two already paired devices are in range of each other :-)

Thanks for any help!

Community
  • 1
  • 1
mheymel
  • 209
  • 3
  • 7
  • I would go by your 2nd solution in which phone tries to connect the accessory without searching for the same, take the example of phone and a headset, if connected and 1 goes out of range, normally headsets tries to connect to previously paired devices for a specific duration of time (depending on headset implementation). – ashish Jul 23 '13 at 13:17
  • Hey! Thanks for your reply. The disadvantage of the second solution is that this has to be triggered somehow (e.g. onResume). In the case of onResume the user has to bring the app into the foreground. I am looking for a possibility to do it automatically at the correct time to make it easy for the user :-) – mheymel Jul 23 '13 at 13:29
  • Is it not possible to have a service running in background which will take care of bluetooth device connect/search – ashish Jul 24 '13 at 04:11
  • Yeah that is the solution. You let the user pair the device manually. A background service tries to create a rfcomm socket to the bound device. Due to this no discovery is required. But for this approach it is mandatory, that the accessory hosts the bluetooth server and the app acts as the client – mheymel Jul 21 '15 at 08:00

2 Answers2

1

After connecting the device for the first time, keep the mac address in a local list. On disconnect, use connectGatt with autoconnect set to true to automatically re-connect when you are in range.

Lars
  • 371
  • 3
  • 8
0

Not a full solution, but maybe it's sufficient for your app to poll the accesory's presence whenever the screen is turned on? In that case, this may be helpful: Start Activity on wake up/sleep in Android

Community
  • 1
  • 1
dojoe
  • 29
  • 2
  • Hey. Thanks for your reply! This is a good solution too (better then the "onResume"-solution). But you are right, still not the 100% solution. After having a look on ACL and the low level messages it seems to be the best way to use solution 3 - since we are having the comfort of a dedicated accessory. [link](http://stackoverflow.com/questions/9537833/what-triggers-the-bluetoothdevice-action-acl-broadcasts) – mheymel Jul 24 '13 at 13:38