蓝牙发现问题
Problems with Bluetooth discovery
所以我需要设备 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。两个设备都应该有一个接收器并且应该是可发现的。
发现设备时是否超时?如果接收器尝试连接但失败,它会发生什么事吗?我试过查找它,但我对蓝牙还是个新手,所以我不知道从哪里开始。如果需要 post 更多代码,我会非常乐意
是的。每个设备都有发现超时。最好的起点是 bluetooth.com,那里有大量的视频和文档。发现、连接、数据传输等蓝牙中的每个activity都有超时
所以我需要设备 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。两个设备都应该有一个接收器并且应该是可发现的。
发现设备时是否超时?如果接收器尝试连接但失败,它会发生什么事吗?我试过查找它,但我对蓝牙还是个新手,所以我不知道从哪里开始。如果需要 post 更多代码,我会非常乐意
是的。每个设备都有发现超时。最好的起点是 bluetooth.com,那里有大量的视频和文档。发现、连接、数据传输等蓝牙中的每个activity都有超时