关于重新连接的A​​ndroid蓝牙createInsecureRfcommSocketToServiceRecord

时间:2014-08-23 08:06:03

标签: android bluetooth rfcomm insecure-connection

当我第一次尝试连接时,服务器端设备在范围内 - 一切正常!

但是,如果设备无法访问(服务发现失败),我的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();
}

0 个答案:

没有答案