The Bluetooth guide in the Android documentation
解释如下:
为了接收有关发现的每个设备的信息,应用程序必须为ACTION\u FOUND intent注册一个BroadcastReceiver。系统为每个设备广播此意图。intent包含额外字段extra\u DEVICE和extra\u CLASS,这两个字段分别包含一个BluetoothDevice和一个BluetoothClass。
还包括以下示例代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
// Register for broadcasts when a device is discovered.
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
}
// Create a BroadcastReceiver for ACTION_FOUND.
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
}
}
};
如果您正在Android上使用蓝牙,我建议您仔细阅读该指南。然后再读一遍;-)