If Bluetooth is paired and connected manually, get the connected device:
1. Check the connection status:
Java code
- int A2DP = Bluetoothadapter.getprofileconnectionstate (BLUETOOTHPROFILE.A2DP);
- int headset = bluetoothadapter.getprofileconnectionstate (Bluetoothprofile.headset);
- int health = bluetoothadapter.getprofileconnectionstate (bluetoothprofile.health);
2, depending on whether there is a connection to obtain the connected device:
Java code
- int flag =-1;
- if (A2DP = = bluetoothprofile.state_connected) {
- flag = A2DP;
- } Else if (headset = = bluetoothprofile.state_connected) {
- flag = headset;
- } Else if (health = = bluetoothprofile.state_connected) {
- Flag = health;
- }
- if (flag! =-1) {
- Bluetoothadapter.getprofileproxy (mainactivity. This, new Servicelistener () {
- @Override
- public void onservicedisconnected (int. profile) {
- //TODO auto-generated method stub
- }
- @Override
- public void onserviceconnected (int. profile, Bluetoothprofile proxy) {
- //TODO auto-generated method stub
- list<bluetoothdevice> mdevices = Proxy.getconnecteddevices ();
- if (mdevices! = null && mdevices.size () > 0) {
- For (Bluetoothdevice device:mdevices) {
- LOG.I ("W", "Device name:" + device.getname ());
- }
- } Else {
- LOG.I ("W", "mdevices is null");
- }
- }
- }, flag);
- }
Android get Bluetooth connected device