蓝牙连接:不安全的连接,安全的连接,安全或不安全的监听,使用什么?

时间:2014-12-07 09:14:46

标签: android bluetooth connection

我正努力在星形拓扑中获得一致的蓝牙连接。我有一部主电话,它是运行API 10的三星Galaxy S4。所有连接到S4上蓝牙服务器插槽的电话都是运行API 10的LG Dynamic Tracfones。

在过去的几天里,我在网上看到了很多关于使用什么类型的连接的相互矛盾的信息。

这是我目前的设置:

主要代码

public void acceptConnection() {

.... (enable bt adapter) ...

    // initializes a Bluetooth server socket
    bluetoothServerSocket = bc.createBluetoothServerSocket();
    //connection made to Master, discovery no longer needed
    bluetoothAdapter.cancelDiscovery();

    BluetoothSocket bluetoothSocket;

    // loops until the thread is interrupted or an exception occurs
    while (!isInterrupted()) {

        try {
            // attempts to accept the slave application's connection
            bluetoothSocket = bluetoothServerSocket.accept();
        } catch (IOException e) {
            // prints out the exception's stack trace
            e.printStackTrace();
            Log.v("Default Thread", "Connection to slave failed.");
            // breaks out of the while loop
            return;
        }

        try {
            ... (enumerate all input and output streams, and all bt sockets) ...
        } catch (IOException e) {
            // prints out the exception's stack trace
            e.printStackTrace();
        }
    }

这是在创建blueToothServerSocket时调用的方法,这是我混淆的一半。我该怎么听适配器?目前,我不安全地做这件事。

public BluetoothServerSocket createBluetoothServerSocket() {

    // gets the name of the application
    String name = "PVCED";
    // gets a common UUID for both the master and slave applications
    UUID uuid = UUID.fromString("23ea856c-49da-11e4-9e35-164230d1df67");

    // initializes an empty Bluetooth server socket
    serverSocket = null;

    try {
        // creates a Bluetooth socket using a common UUID
        serverSocket = bluetoothAdapter.listenUsingInsecureRfcommWithServiceRecord(name, uuid);
    } catch (IOException e) {
        // prints out the exception's stack trace
        e.printStackTrace();
    }

    return serverSocket;
}

SLAVE CODE

这就是我困惑的另一半,我应该如何创建一个套接字?目前我不安全地做这件事。

private BluetoothSocket createBluetoothSocket(Set<BluetoothDevice> pairedDevices) {

    // gets a common UUID for both the master and slave applications
    UUID uuid = UUID.fromString("23ea856c-49da-11e4-9e35-164230d1df67");

    // initialises an empty Bluetooth socket
    BluetoothSocket bluetoothSocket = null;

    // checks to see if there are any paired devices
    if (pairedDevices.size() > 0) {
        // loops through each paired device
        for (BluetoothDevice device : pairedDevices) {
            // checks to see if the name of the paired device is MASTER
            if (device.getName().equals("MASTER")) {
                try {
                    master = device;
                    // creates a Bluetooth socket using a common UUID
                    //bluetoothSocket = master.createRfcommSocketToServiceRecord(uuid);
                    //Method m = master.getClass().getMethod("createRfcommSocketToServiceRecord", new Class[] {int.class});
                    //bluetoothSocket = (BluetoothSocket) m.invoke(master, 1);
                    bluetoothSocket = master.createInsecureRfcommSocketToServiceRecord(uuid);
                } catch(Exception e){
                    Log.v("Connect Exception", e.getMessage());
                }
            }
        }
    }
    //check if we paired succesfully to a master, if not, prompt user to do so.
    if (master == null){
        ... (tell user to pair with master via toast) ...
    }

    return bluetoothSocket;
}

我的logcat经常出现错误,例如&#34; Bad File Descriptor&#34;,&#34;无法启动Service Discovery&#34;或者&#34; Service Discovery失败。&#34;

用于我的方案的最佳连接方案是什么?如果你们需要更多有关我如何启用/禁用bt适配器或关闭bt连接的详细信息,我可以提供更多代码。

0 个答案:

没有答案