当我第一次尝试连接时,服务器端设备在范围内 - 一切正常!
但是,如果设备无法访问(服务发现失败),我的 BT 会保存该 SDP 记录并且不想再连接。我真的认为缓存这个非常愚蠢,它看起来像“哦,我们没有在这个设备上找到这个 UUID。它在整个世界中都不存在,让我们永远记住这一点”。
仅当我更改 UUID 或解决它时再试一次。但是当客户端找不到设备时,我无法关闭旧的并打开新的 UUID 侦听端口。
我怎么能告诉他“清除这个愚蠢的记录”或类似的东西。
PS:我需要 API10 来说明为什么我使用“bConnected”而不是 isConnected();
客户端代码:
public void fire(View view) {
final BluetoothAdapter BA = BluetoothAdapter.getDefaultAdapter();
final String sUuid = "SuperHidenUuid";
final UUID uuid = UUID.nameUUIDFromBytes(sUuid.getBytes());
Thread t = new Thread(new Runnable() {
@Override
public void run() {
OutputStream sOut;
InputStream sIn;
BluetoothSocket sConnection;
BA.cancelDiscovery(); //shotgun debuging
BluetoothDevice BD = BA.getRemoteDevice(sVictimBsid);
try {
sConnection = BD.createInsecureRfcommSocketToServiceRecord(uuid);
} catch (IOException e) {
return;
}
boolean bConnected = false;
try {
sConnection.connect();
bConnected = true;
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bConnected) {
sIn = sConnection.getInputStream();
sOut = sConnection.getOutputStream();
sOut.write((sVictimPass + "\n").getBytes());
BufferedReader reader = new BufferedReader(new InputStreamReader(sIn));
// here I send and receive some data
sIn.close(); //shotgun debuging
sOut.close(); //shotgun debuging
}
} catch (IOException e) {
e.printStackTrace();
}
try {
Thread.sleep(1000); //shotgun debuging
sConnection.close();
BA.cancelDiscovery(); //shotgun debuging
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t.start();
}