Bluetooth permissions <uses-permission android:name= "Android.permission.BLUETOOTH"/>
1, monitor the mobile phone itself Bluetooth status broadcast
Send when Bluetooth on phone is turned off
Action:BluetoothAdapter.ACTION_STATE_CHANGED
if(Action.equals (bluetoothadapter.action_state_changed)) {intState =Intent.getintextra (Bluetoothadapter.extra_state, bluetoothadapter.error); Switch(state) { CaseBLUETOOTHADAPTER.STATE_OFF:LOG.D ("AAA", "State_off phone bluetooth off"); Break; CaseBLUETOOTHADAPTER.STATE_TURNING_OFF:LOG.D ("AAA", "State_turning_off phone Bluetooth is shutting down"); Break; CaseBLUETOOTHADAPTER.STATE_ON:LOG.D ("AAA", "state_on mobile phone bluetooth on"); Break; CaseBLUETOOTHADAPTER.STATE_TURNING_ON:LOG.D ("AAA", "state_turning_on mobile phone Bluetooth is open"); Break; }}
2, monitor Bluetooth device pairing status of the broadcast
Bluetooth device is sent when pairing and de-pairing
Action:BluetoothDevice.ACTION_BOND_STATE_CHANGED
if(Action.equals (bluetoothdevice.action_bond_state_changed)) {Bluetoothdevice device=Intent.getparcelableextra (Bluetoothdevice.extra_device); String name=Device.getname (); LOG.D ("AAA", "Device Name:" +name); intState = Intent.getintextra (Bluetoothdevice.extra_bond_state,-1); Switch(state) { CaseBLUETOOTHDEVICE.BOND_NONE:LOG.D ("AAA", "Bond_none Delete pairing"); Break; CaseBLUETOOTHDEVICE.BOND_BONDING:LOG.D ("AAA", "Bond_bonding is pairing"); Break; CaseBLUETOOTHDEVICE.BOND_BONDED:LOG.D ("AAA", "bond_bonded pairing Success"); Break; }}
3. Monitor the Bluetooth device connection and connect the disconnected broadcast
Send when Bluetooth device is connected on and disconnected
Action:BluetoothDevice.ACTION_ACL_CONNECTED bluetoothdevice.action_acl_disconnected
if (Action.equals (bluetoothdevice.action_acl_connected)) { = Intent.getparcelableextra ( Bluetoothdevice.extra_device); LOG.D ("AAA", Device.getname () + "action_acl_connected"Elseif (Action.equals ( bluetoothdevice.action_acl_disconnected)) { = Intent.getparcelableextra (bluetoothdevice.extra_ DEVICE); LOG.D ("AAA", Device.getname () + "action_acl_disconnected");}
If you want to get all the Bluetooth devices that are currently connected, you can manually maintain a list of connected devices in these two broadcasts.
Bluetoothclass can get the type of Bluetooth device
Android Bluetooth device listening radio