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