所以我需要设备 A(客户端)连接到设备 B(服务器)并且我已经让它工作,但并非在所有情况下都是如此。打开应用程序后,我希望允许设备被发现
private void enableDiscoverability() {
Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,
3600);
startActivity(discoverableIntent);
}
,我设置了一个接收器来寻找一个特定的设备(就目前而言,我仍在学习如何使用蓝牙)
private final BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
TextView output = findViewById(R.id.output);
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 temp = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = temp.getName();
Log.i("Device ", deviceName + " was found");
String deviceHardwareAddress = temp.getAddress();
if (deviceHardwareAddress.equalsIgnoreCase("DC:F7:56:DD:73:8F")) {
device = temp;
startClient();
}
}
}
};
所以这是我的问题。当设备 A 保持打开状态时,它会在一段时间内发现新设备,但几分钟后就不会发现新设备 B。两个设备都应该有一个接收器并且应该是可发现的。
发现设备时是否超时?如果接收器尝试连接但失败,会发生什么情况吗?我试图查找它,但我对蓝牙还是新手,所以我不知道从哪里开始。如果需要的话,我非常乐意发布更多代码
最佳答案
是的。每个设备都有发现超时。最好的起点是 bluetooth.com,其中有大量视频和文档。蓝牙中的每个 Activity (例如发现、连接、数据传输等)都有超时。
关于java - 蓝牙发现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58377796/