查找附近的蓝牙设备
先申报 BluetoothAdapter
。
BluetoothAdapter mBluetoothAdapter;
现在为 ACTION_FOUND
创建一个 BroadcastReceiver
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//Device found
if (BluetoothDevice.ACTION_FOUND.equals(action))
{
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a list
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
};
注册 BroadcastReceiver
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
然后通过调用 startDiscovery
开始发现附近的蓝牙设备
mBluetoothAdapter.startDiscovery();
别忘了取消注册 onDestroy
里面的 BroadcastReceiver
unregisterReceiver(mReceiver);