The author has recently been doing Bluetooth status to get things, summed up a set of Bluetooth status code, share the following:
Private Bluetoothadapter BA; Bluetooth Adapter BA = Bluetoothadapter.getdefaultadapter (); Whether the Bluetooth adapter is present, that is, whether an error occurred if (BA = = null) {Isbluecon =-1; Error}else if (ba.isenabled ()) {int A2DP = ba.getprofileconnectionstate (BLUETOOTHPROFILE.A2DP);//can manipulate Bluetooth devices, such as the Bluetooth headset with play suspend function int headset = ba.getprofileconnectionstate (bluetoothprofile.headset);//bluetooth headset, support voice input output int health = BA. Getprofileconnectionstate (bluetoothprofile.health);//Bluetooth wearable device//check whether Bluetooth is connected to one of three devices to determine whether it is connected or open and not connected to the status 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; }//Describes an if (flag = = 1) connection on three devices {Isbluecon = 1;//discontinued} else if (flag = =-1) {Networkinfo NetI NFO = Cm.getnetworkinfo (Connectivitymanager.type_bluetooth); if (NetInfo = = null) {Isbluecon = 1;//discontinued} else {state BLT = netinfo.getstate (); Isbluecon = Getdevstate (BLT); Inside the system, return the connection or not}}}else {Isbluecon = 2;//shut off}
public static enum state { CONNECTED, connecting, disconnected, disconnecting, SUSPENDED, UNKNOWN; Private state () { } }
Android Get Bluetooth status