Currently connected bluetooth device android

2019-06-15 01:47发布

问题:

I can able to see two states in Bluetooth device in Android. 1. Paired 2. Connected. -
I am trying to get currently connected Bluetooth device in Android. But I am getting only paired device list from adapter.getBondedDevices(); I need currently connected device. How can i get this. Please someone help me to achieve this. Thanks in advance.

回答1:

That's pretty straight forward. Android BluetoothManager have method of

getConnectedDevices()

Implementation like:

BluetoothManager manager = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
    List<BluetoothDevice> connected = manager.getConnectedDevices(GATT);
    Log.i("Connected Devices: ", connected.size()+"");

If you want more details about connected devices then you can use the above list method put it into for loop and get the inner details of each Bluetooth device which are connected.

Logs:

12-20 18:04:09.679 14933-14933/com.salman.dleague.blescanning I/Connected Devices:: 2

Hope its helpful :)



回答2:

Add this in your manifest file

<receiver android:name=".MyBluetoothReceiver" >
<intent-filter>
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
<action android:name="android.bluetooth.device.action.ACL_DISCONNECTED" 
/>
<action 
android:name="android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED" 
/>           
</intent-filter>  
</receiver>  

Add this Class

public class MyBluetoothReceiver extends BroadcastReceiver {
 @Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    // When discovery finds a device
    if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {

    BluetoothDevice device = intent
                .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

     Toast.makeText(getApplicationContext(),device.getName() +" CONNECTED",Toast.LENGTH_LONG).show();

    } else if (BluetoothAdapter.ACL_DISCONNECTED
            .equals(action)) {

    }
}
}