Android Bluetooth device listening radio

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.