Using the BluetoothAdapter class, you can find surrounding Bluetooth devices on your Android device and then pair (bind) them. Bluetooth communication is transmitted to each other based on the unique address MAC. Considering security issues, Bluetooth communication needs to be paired first. Then start to connect to each other. After the connection, the devices will share the same RFCOMM channel to transmit data to each other. Currently, these implementations are implemented on Android 2.0 or higher SDK.
1. Finding/discovering devices
For Android to find Bluetooth devices, use the startDiscovery() method of the BluetoothAdapter class to perform an asynchronous method to obtain surrounding Bluetooth devices. Because it is an asynchronous method, we There is no need to consider the thread being blocked. The whole process takes about 12 seconds. At this time, we then register a BroadcastReceiver object to receive the found Bluetooth device information. We filter the ACTION_FOUND Intent action to obtain the detailed information of each remote device. Each BluetoothDevice object and the device type BluetoothClass of the object are included in the Intent fields EXTRA_DEVICE and EXTRA_CLASS through additional parameters, sample code
private final BroadcastReceiver cwjReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE) ;
myArrayAdapter.add(device.getName() + " android123 " + device.getAddress()); // Get the device name and mac address
// Register this BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(cwjReceiver, filter);
Finally, android123 reminds everyone that you need to pay attention to, remember Override the onDestory() method in Service or Activity, and use the unregisterReceiver method to unregister the BroadcastReceiver object to ensure that resources are correctly recycled.
Some other status changes are ACTION_SCAN_MODE_CHANGED extra parameters EXTRA_SCAN_MODE and EXTRA_PREVIOUS_SCAN_MODE and SCAN_MODE_CONNECTABLE_DISCOVERABLE, SCAN_MODE_CONNECTABLE and SCAN_MODE_NONE, Bluetooth module
All resources on this website are contributed and published by netizens, or reprinted by major download sites. Please check the integrity of the software yourself! All resources on this website are for learning and reference only. Please do not use them for commercial purposes, otherwise you will be responsible for all consequences incurred! If there is any infringement, please contact us to delete and remove it. Contact information: admin@php.cn