Android蓝牙对和连接

时间:2015-02-03 03:44:19

标签: android multithreading bluetooth

您好我必须开发一个应用程序,所以我有一个设备(服务器)有3个客户端。 我做了所有验证,打开蓝牙,找到设备,所有工作都很棒。但是,当我要连接设备时,我不知道会发生什么。

当我点击要连接的设备时,我正在使用下一个代码。我只在母设备上安装了我的应用程序。

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // TODO Auto-generated method stub
    try {
        if(btADapter.isDiscovering()){
            btADapter.cancelDiscovery();
        }
        if(listAdapter.getItem(position).contains("Paired")){
            BluetoothDevice selectedDevice = devices.get(position);
            ConnectThread connect = new ConnectThread(selectedDevice);
            connect.start();
        }
        else{
            BluetoothDevice selectedDevice = devices.get(position);
            ConnectThread connect = new ConnectThread(selectedDevice);
            connect.start();
            //pairDevice(devices.get(position));
            //Toast.makeText(getApplicationContext(), "device is not paired", Toast.LENGTH_LONG).show();
        }

    } catch (Exception e) {
        System.out.println(e);
    }
}

我在这里有一个问题,如果没有配对会发生什么?如果我尝试连接它自动配对它们?

我的UUID是:&#34; 00001101-0000-1000-8000-00805F9B34FB&#34; 然后我的连接代码:

private class ConnectThread extends Thread {

    private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;

    public ConnectThread(BluetoothDevice device) {
        // Use a temporary object that is later assigned to mmSocket,
        // because mmSocket is final
        BluetoothSocket tmp = null;
        mmDevice = device;

        // Get a BluetoothSocket to connect with the given BluetoothDevice
        try {
            // MY_UUID is the app's UUID string, also used by the server
            // code
            tmp = device.createRfcommSocketToServiceRecord(MY_UUID);
        } catch (IOException e) {
        }
        mmSocket = tmp;
    }

    public void run() {
        // Cancel discovery because it will slow down the connection
        btADapter.cancelDiscovery();

        try {
            // Connect the device through the socket. This will block
            // until it succeeds or throws an exception
            mmSocket.connect();
        } catch (IOException connectException) {
            System.out.println(connectException);
            // Unable to connect; close the socket and get out
            try {
                mmSocket.close();
            } catch (IOException closeException) {
            }
            return;
        }

        // Do work to manage the connection (in a separate thread)

        mHandler.obtainMessage(SUCCESS_CONNECT, mmSocket).sendToTarget();
    }

    /** Will cancel an in-progress connection, and close the socket */
    public void cancel() {
        try {
            mmSocket.close();
        } catch (IOException e) {
        }
    }
}

当我做mmSocket.connect();应用程序崩溃并返回:java.io.IOException:服务发现失败,我不知道该怎么做。

我需要帮助。 感谢。

修改

在寻找更多答案之后,我在这个问题(Android Bluetooth Connection - Service Discovery Failed)中找到了Sandeep Maram的答案并完美地连接起来。 这是代码:

BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
Method m = device.getClass().getMethod("createInsecureRfcommSocket", new Class[] {int.class});
socket = (BluetoothSocket) m.invoke(device, 1);
bluetoothAdapter.cancelDiscovery();
socket.connect();

2 个答案:

答案 0 :(得分:0)

当您尝试连接时,它会向您尝试连接的设备发送特定的整数代码,它必须接受传入连接并允许您(母设备)在它们之间共享文件,一旦您的设备是配对它将不再要求你再次配对它,当你选择与之前配对的特定设备时,它将启动连接,你可以随时共享数据,我建议你使用Synchornise,因为你正在做多线程,以便只有一个线程能够读写时间,你的设备中没有死锁或饥饿!

答案 1 :(得分:0)

致电

btADapter.cancelDiscovery();

停止发现可能需要一些时间。您可以尝试在

之间设置延迟
btADapter.cancelDiscovery();

mmSocket.connect();

如果有帮助,请告诉我。