我是 android 的新手。我想开发一个应用程序以通过编程方式使用蓝牙来查找范围内的设备。如果有人有想法,请给我一些示例代码。
最佳答案
以编程方式使用蓝牙查找范围内的设备。
是的,您可以使用 BroadcastReceiver 来做到这一点, 查看下面的代码,它会对你有所帮助。
开始搜索
mBluetoothAdapter.startDiscovery();
mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
找到设备
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 ListView
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
};
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
关于android - 如何使用蓝牙找到范围内的设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6464015/