我正在开发蓝牙应用程序。在我的应用程序中,我有以下代码。当蓝牙关闭时意味着我的 else 语句在发现设备列表时打开蓝牙(它工作正常),否则 if 语句将运行并获取设备列表。
My problem is "if condition is runnning but it doesn't discover the devices"
. 任何人都可以提出解决方案。
if (btAdapter.isEnabled()) {
registerReceiver(ActionFoundReceiver, new IntentFilter(
BluetoothDevice.ACTION_FOUND));
btAdapter.startDiscovery();
} else {
Intent i1 = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(i1);
new java.util.Timer().schedule(new java.util.TimerTask() {
@Override
public void run() {
registerReceiver(ActionFoundReceiver, new IntentFilter(
BluetoothDevice.ACTION_FOUND));
btAdapter.startDiscovery();
}
}, 5000);
}
//接收器检测设备代码
private final BroadcastReceiver ActionFoundReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice device = intent
.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
items += device.getName() + ",";
}
Toast.makeText(getApplicationContext(), items, Toast.LENGTH_LONG)
.show();
}
};