我使用这段代码来检查我的蓝牙是否找到了我的“朋友电话”,问题是有时当我运行它时,我得到'错误接收广播意图'。它说'BluetoothDevice.ACTION_FOUND.equals(action)'有问题 我在stackoverflow上研究了一些其他问题,有人建议使用'intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);'我尝试了,但似乎没有任何影响......任何人都知道为什么这对我不起作用或者对如何修复它有所了解?
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
if (bluetoothDevice.getName().equals("friends phone"))) {
Toast.makeText(getApplicationContext(), "Found it!", Toast.LENGTH_SHORT).show();
}
}
}
};
答案 0 :(得分:0)
我以这种方式使用BL设备发现并且可靠地工作:
在您的活动中定义这些:
private final IntentFilter deviceFoundFilter = new IntentFilter();
private final BroadcastReceiver deviceFoundReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String name = device.getName();
String address = device.getAddress();
int bondState = device.getBondState();
if (bondState != BluetoothDevice.BOND_BONDED)
deviceListAdapter.add(device.getName() + "\n" + device.getAddress());
}
};
让我们在onCreate方法或任何其他类似init的方法中添加以下行:
deviceFoundFilter.addAction(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(deviceFoundReceiver, deviceFoundFilter);
希望它有所帮助。