Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 486
mheymel Avatar asked Jul 23 '13 12:07

mheymel


People also ask

Where can I find previously paired Bluetooth devices?

Android mobile devices (smartphone, tablet)Select Connected devices or Device Connection. Select Previously connected devices or Bluetooth. If the Bluetooth function is OFF, turn it ON. The mobile device displays a list of saved Bluetooth devices.

Can someone connect to my Bluetooth from far away?

The range of the Bluetooth® connection is approximately 30 feet (10 meters). However, maximum communication range will vary depending on obstacles (person, metal, wall, etc.) or electromagnetic environment.

Why is my Bluetooth constantly searching for other devices?

No bluetooth does not continually search for devices. You have to initiate the pairing and search for other devices. When you do this it is vulnerable as it is in active mode to look for other devices that can transmit bluetooth signals. That is the only time it is looking for other bluetooth devices.

Can you track devices connected to Bluetooth?

Using the BluetoothAdapter , you can find remote Bluetooth devices either through device discovery or by querying the list of paired devices. Make sure you have the appropriate Bluetooth permissions and set up your app for Bluetooth before attempting to find Bluetooth devices.


1 Answers

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.

like image 83
Lars Avatar answered Oct 09 '22 06:10

Lars