This is good to as long as my application is running but how can I get a current list?
–
dchappelleJan 18 '11 at 16:45

How do you plan on executing code without your application "running"? If you mean you need to access this from something other than an Activity... go google Android Services, build one of those to listen to the broadcasts, and persist it to a list.
–
skylarsuttonJan 18 '11 at 19:44

3

I can listen for the intents BUT how can I get the initial list of connected Bluetooth devices? If any are already connected by the time my Activity or Service is started I won't know. I would imagine (hope) this data is available somewhere? I wouldn't want to have to create a service (that runs constantly as long as the phone is turned on) just to listen for these intents.
–
dchappelleJan 19 '11 at 13:28

6

There is no way to retrieve the list of connected devices at application startup. The Bluetooth API will only let you listen to connection changes. So yes, the only way to do it is create a long running service and add/remove to a public list. It's a big complaint by a lot of developers. Depending on your performance needs you could retrieve the list of paired devices and try to connect to each one. If it fails, it's not available. Word of warning though: .connect() is a blocking operation.
–
skylarsuttonJan 20 '11 at 18:57

1

@skylarsutton +1 Thanks a bunch for this answer, helped me getting started on bluetooth
–
ZainodisFeb 14 '12 at 17:23

Big thanks to Skylarsutton for his answer. I'm posting this as a response to his, but because I'm posting code I can't reply as a comment. I already upvoted his answer so am not looking for any points. Just paying it forward.

For some reason BluetoothAdapter.ACTION_ACL_CONNECTED could not be resolved by Android Studio. Perhaps it was deprecated in Android 4.2.2? Here is a modification of his code. The registration code is the same; the receiver code differs slightly. I use this in a service which updates a Bluetooth-connected flag that other parts of the app reference.

With Boost2525's help the solution to this problem was to was to retrieve the list of paired devices and try to connect to each one in a separate thread. The problem of trying to connect to each one was answered here: Android Bluetooth: Service Discovery Failed Exception