Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically tell if a Bluetooth device is connected?

I understand how to get a list of paired devices, but how can I tell if they are connected?

It must be possible since I see them listed in my phone's Bluetooth device list and it states their connection status.

like image 331
dchappelle Avatar asked Jan 17 '11 17:01

dchappelle


People also ask

Can you track Bluetooth connections?

Can you track someone through Bluetooth? Tracking Bluetooth users with discoverable devices is possible, but tracking someone specifically generally isn't unless you physically follow them, which isn't practical.

How do I know if my Bluetooth is connected?

Make sure Bluetooth is turned on. Touch and hold Bluetooth . In the list of paired devices, tap a paired but unconnected device. When your phone and the Bluetooth device are connected, the device shows as "Connected."

How do you check Bluetooth is on or off in Android programmatically?

Call isEnabled() to check whether Bluetooth is currently enabled. If this method returns false, then Bluetooth is disabled. To request that Bluetooth be enabled, call startActivityForResult() , passing in an ACTION_REQUEST_ENABLE intent action.


1 Answers

Add the Bluetooth permission to your AndroidManifest,

<uses-permission android:name="android.permission.BLUETOOTH" /> 

Then use intent filters to listen to the ACTION_ACL_CONNECTED, ACTION_ACL_DISCONNECT_REQUESTED, and ACTION_ACL_DISCONNECTED broadcasts:

public void onCreate() {     ...     IntentFilter filter = new IntentFilter();     filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);     filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);     filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);     this.registerReceiver(mReceiver, filter); }  //The BroadcastReceiver that listens for bluetooth broadcasts private final BroadcastReceiver mReceiver = new BroadcastReceiver() {     @Override     public void onReceive(Context context, Intent intent) {         String action = intent.getAction();         BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);          if (BluetoothDevice.ACTION_FOUND.equals(action)) {            ... //Device found         }         else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {            ... //Device is now connected         }         else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {            ... //Done searching         }         else if (BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED.equals(action)) {            ... //Device is about to disconnect         }         else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {            ... //Device has disconnected         }     } }; 

A few notes:

  • There is no way to retrieve a list of connected devices at application startup. The Bluetooth API does not allow you to query, instead it allows you to listen to changes.
  • A hoaky workaround to the above problem would be to retrieve the list of all known/paired devices... then trying to connect to each one (to determine if you're connected).
  • Alternatively, you could have a background service watch the Bluetooth API and write the device states to disk for your application to use at a later date.
like image 71
Skylar Sutton Avatar answered Oct 03 '22 16:10

Skylar Sutton