是否有可能在Android蓝牙和笔记本电脑蓝牙设备之间建立不安全的连接?

时间:2014-06-16 06:07:22

标签: java android sockets bluetooth rfcomm

我正在尝试通过蓝牙套接字连接将Android设备连接到包含蓝牙的笔记本电脑或台式机。

我创建了一个Android应用程序(Client),它试图连接正在运行java应用程序(Server)的笔记本电脑蓝牙设备。

我担心的是,是否可以使用蓝牙套接字连接不安全地连接设备(没有引脚验证)?

如果可能,请建议我解决方案。

如果没有,有没有办法以编程方式自动配对这两个设备?

提前致谢!!!

2 个答案:

答案 0 :(得分:1)

通过引用java api的蓝牙技术,我得到了两个Android和笔记本电脑蓝牙设备之间不安全连接的解决方案。

我使用过SPP客户端服务器机制。

我的服务器是java。 在java中将某些参数添加到URL。 使authentication = false;授权= FALSE;加密= FALSE; 打开此URL以进行连接接受。

//Create a UUID for SPP
    UUID uuid=new UUID("0f2b61c18be240e6ab90e735818da0a7", false);
    System.out.println("\n"+uuid.toString());

    //Create the servicve url
    String url="btspp://localhost:"+uuid.toString()+";"+"name=remoteNotifier;authenticate=false;authorize=false;encrypt=false";

    //open server url
    StreamConnectionNotifier streamConnNotifier = (StreamConnectionNotifier)Connector.open(url);


    //Create a UUID for SPP
    UUID uuid=new UUID("0f2b61c18be240e6ab90e735818da0a7", false);

    System.out.println("\n"+uuid.toString());

    //Create the servicve url
    String url="btspp://localhost:"+uuid.toString()+";"+"name=remoteNotifier;authenticate=false;authorize=false;encrypt=false";

    //open server url
    StreamConnectionNotifier streamConnNotifier = (StreamConnectionNotifier)Connector.open(url);

现在在客户端: 上面的Android API 10包含不安全的连接方法。 “createInsecureRfcommSocketToServiceRecord(UUID)” 所以使用这种方法进行连接。它不会弹出配对请求,也不会尝试连接已经运行Java服务器的远程蓝牙设备。

代码:

// Set up a pointer to the remote node using it's address.
    BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);

    // Two things are needed to make a connection:
    // A MAC address, which we got above.
    // A Service ID or UUID.  In this case we are using the
    // UUID for SPP.
    try {
        //          btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
        btSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e) {
        AlertBox("Fatal Error", "In onResume() and socket create failed: " + e.getMessage() + ".");
    }
    // Discovery is resource intensive.  Make sure it isn't going on
    // when you attempt to connect and pass your message.
    mBluetoothAdapter.cancelDiscovery();

    // Establish the connection.  This will block until it connects.
    try {
        btSocket.connect();
        out.append("\n...Connection established and data link opened...");
    } catch (IOException e) {
        try {
            btSocket.close();
            e.printStackTrace();
        } catch (IOException e2) {
            e2.printStackTrace();
            AlertBox("Fatal Error", "In onResume() and unable to close socket during connection failure" + e2.getMessage() + ".");
        }
    }

    // Create a data stream so we can talk to server.
    out.append("\n...Sending message to server...");

    try {
        outStream = btSocket.getOutputStream();
    } catch (IOException e) {
        AlertBox("Fatal Error", "In onResume() and output stream creation failed:" + e.getMessage() + ".");
    }

    //      Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.android_logo);
    //      byte[] msgBuffer = getBytesFromBitmap(bitmap);

    String message = "Hello from Android.\n";
    byte[] msgBuffer = message.getBytes();
    try {
        outStream.write(msgBuffer);
    } catch (IOException e) {
        String msg = "In onResume() and an exception occurred during write: " + e.getMessage();
        if (address.equals("00:00:00:00:00:00")) {
            msg = msg + ".\n\nUpdate your server address from 00:00:00:00:00:00 to the correct address on line 37 in the java code";
            msg = msg +  ".\n\nCheck that the SPP UUID: " + MY_UUID.toString() + " exists on server.\n\n";
        }
        // AlertBox("Fatal Error", msg);      
    }

我只提供了所需的代码。 对于连接,两个设备的UUID应该相同。

在客户端的“地址”字段提供服务器蓝牙MAC地址。

我们能够不安全地与远程蓝牙设备通信(没有配对)。

但是这段代码依赖于设备...

某些设备能够非常有效地进行通信。 像联想笔记本电脑,用于Java服务器的PC的外部蓝牙设备 和 Android设备DELL场地7,索尼,LG手机客户端。 经过测试和正常工作。

但在戴尔笔记本电脑,Micromaxx,xolo mobile它不起作用。 我不知道为什么它不起作用,如果有人知道请给出解决方案。

答案 1 :(得分:0)

对于蓝牙2.1及更高版本的设备,必须提供安全性。 如果您只是想避免密钥输入/显示,您可以将笔记本电脑和Android设备上的安全要求设置为“不需要MITM保护”。 这样设备就会自动配对,但链接在中间攻击中容易受到攻击。