简体   繁体   English

Android:逐个通过蓝牙连接设备

[英]Android: connect to devices via Bluetooth one by one

I use a modification of Android BluetoothChat to connect to devices one by one: 我使用Android BluetoothChat的修改来逐个连接到设备:

  1. try to connect to device 尝试连接到设备
  2. if connection was failed then connect to another device 如果连接失败,则连接到另一台设备
  3. if connected then try read data from device and then connect to another device etc. 如果已连接,则尝试从设备读取数据,然后连接到其他设备等。

But I get an exception Service discovery failed on Samsung Galaxy i-9000 (Android 2.3) on a line mmSocket.connect(). 但我得到一个异常服务发现在三星Galaxy i-9000(Android 2.3)上失败了mmSocket.connect()。

The problem is that when I'm using my application on HTC Desire (Android 2.2) it works fine. 问题是,当我在HTC Desire(Android 2.2)上使用我的应用程序时,它运行正常。

But when I added Thread.sleep(...) before socket connection, socket connect always successfully on Samsung and HTC. 但是当我在套接字连接之前添加Thread.sleep(...)时,socket连接总是在Samsung和HTC上成功连接。

Is it problem with Android version or with the code? Android版本或代码有问题吗?

Here is the code: 这是代码:

private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
    private final BluetoothDevice mmDevice;
    private boolean mIsCanceled = false;

    public ConnectThread(BluetoothDevice device) {
        mmDevice = device;
        BluetoothSocket tmp = null;

        if(LOG_ENABLED) Log.d(TAG, "create mConnectThread " + mmDevice.getAddress());

        // Get a BluetoothSocket for a connection with the
        // given BluetoothDevice

        try
        {
            tmp = device.createRfcommSocketToServiceRecord(SerialPortServiceClass_UUID);
            if(LOG_ENABLED) Log.d(TAG, "create socket");
        }
        catch (IOException e) {
            if(LOG_ENABLED) Log.e(TAG, "create() failed", e);
        }

        mmSocket = tmp;
    }

    public void run() {
        if(LOG_ENABLED) Log.d(TAG, "run mConnectThread " + mmDevice.getAddress());

        // Always cancel discovery because it will slow down a connection
        mAdapter.cancelDiscovery();

        /*
        With this code commented "mmSocket.connect()" throws an exception
        Service discovery failed on Samsung Galaxy S
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/

        if(mIsCanceled) return;

        // Make a connection to the BluetoothSocket
        try {
            // This is a blocking call and will only return on a
            // successful connection or an exception
            if(LOG_ENABLED) Log.d(TAG, "socket.connect() " + mmDevice.getAddress());
            mmSocket.connect();
        } catch (Exception e) {

            if(LOG_ENABLED) Log.e(TAG, e.getMessage() + " " + mmDevice.getAddress(), e);

            try {
                mmSocket.close();
                if(LOG_ENABLED) Log.d(TAG, "mConnectThread mmSocket.close() " + mmDevice.getAddress());
            } catch (IOException e1) {

                if(LOG_ENABLED) Log.e(TAG, "mConnectThread mmSocket.close() error" + mmDevice.getAddress());
                if(LOG_ENABLED) Log.e(TAG, e1.getMessage() + " " + mmDevice.getAddress(), e1);
            }

            connectionFailed(mmDevice.getAddress());

            return;
        }

        // Reset the ConnectThread because we're done
        synchronized (CBluetoothAdapter.this) {
            mConnectThread = null;
        }

        // Start the connected thread
        connected(mmSocket, mmDevice);
    }

    public void cancel() {
        try {
            mmSocket.close();
            if(LOG_ENABLED) Log.d(TAG, "mConnectThread socket close() " + mmDevice.getAddress());
        } catch (IOException e) {
            if(LOG_ENABLED) Log.e(TAG, "mConnectThread close() of connect socket failed " + mmDevice.getAddress(), e);
        }

        mIsCanceled = true;
    }
}

And this is from LogCat: 这是来自LogCat:

10-04 12:54:57.035: D/CBluetoothAdapter(27291): Start connect 00:0A:84:02:77:6F
10-04 12:54:57.039: D/CBluetoothAdapter(27291): Connecting to device 00:0A:84:02:77:6F
10-04 12:54:57.039: D/CBluetoothAdapter(27291): connect to: 00:0A:84:02:77:6F
10-04 12:54:57.039: D/CBluetoothAdapter(27291): create mConnectThread 00:0A:84:02:77:6F
10-04 12:54:57.187: D/CBluetoothAdapter(27291): create socket
10-04 12:54:57.187: D/CBluetoothAdapter(27291): State changed: CONNECTING
10-04 12:54:57.191: D/CBluetoothAdapter(27291): run mConnectThread 00:0A:84:02:77:6F
10-04 12:54:57.203: D/CBluetoothAdapter(27291): socket.connect() 00:0A:84:02:77:6F
10-04 12:55:03.234: E/CBluetoothAdapter(27291): Service discovery failed 00:0A:84:02:77:6F
10-04 12:55:03.234: E/CBluetoothAdapter(27291): java.io.IOException: Service discovery failed
10-04 12:55:03.234: E/CBluetoothAdapter(27291):     at     android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:406)
10-04 12:55:03.234: E/CBluetoothAdapter(27291):     at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:217)
10-04 12:55:03.234: E/CBluetoothAdapter(27291):     at     scatemobile.adapters.CBluetoothAdapter$ConnectThread.run(CBluetoothAdapter.java:473)
10-04 12:55:03.253: D/CBluetoothAdapter(27291): mConnectThread mmSocket.close() 00:0A:84:02:77:6F
10-04 12:55:03.253: D/CBluetoothAdapter(27291): Connection failed 00:0A:84:02:77:6F
10-04 12:55:03.253: D/CBluetoothAdapter(27291): Disconnect 00:0A:84:02:77:6F
10-04 12:55:03.253: D/CBluetoothAdapter(27291): mConnectThread socket close() 00:0A:84:02:77:6F
10-04 12:55:03.253: D/CBluetoothAdapter(27291): State changed: DISABLED
10-04 12:55:03.253: D/CBluetoothAdapter(27291): Start connect 00:1D:28:92:A2:D1
10-04 12:55:03.257: D/CBluetoothAdapter(27291): Connecting to device 00:1D:28:92:A2:D1
10-04 12:55:03.257: D/CBluetoothAdapter(27291): connect to: 00:1D:28:92:A2:D1
10-04 12:55:03.257: D/CBluetoothAdapter(27291): create mConnectThread 00:1D:28:92:A2:D1
10-04 12:55:03.257: D/CBluetoothAdapter(27291): create socket
10-04 12:55:03.261: D/CBluetoothAdapter(27291): Send msg MSG_CONNECTION_FAILED 00:0A:84:02:77:6F     1079579976
10-04 12:55:03.273: D/CBluetoothAdapter(27291): State changed: CONNECTING
10-04 12:55:03.273: D/CBluetoothAdapter(27291): run mConnectThread 00:1D:28:92:A2:D1
10-04 12:55:03.289: D/CBluetoothAdapter(27291): socket.connect() 00:1D:28:92:A2:D1
10-04 12:55:09.304: E/CBluetoothAdapter(27291): Service discovery failed 00:1D:28:92:A2:D1
10-04 12:55:09.304: E/CBluetoothAdapter(27291): java.io.IOException: Service discovery failed
10-04 12:55:09.304: E/CBluetoothAdapter(27291):     at     android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:406)
10-04 12:55:09.304: E/CBluetoothAdapter(27291):     at     android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:217)
10-04 12:55:09.304: E/CBluetoothAdapter(27291):     at scatemobile.adapters.CBluetoothAdapter$ConnectThread.run(CBluetoothAdapter.java:473)
10-04 12:55:09.312: D/CBluetoothAdapter(27291): mConnectThread mmSocket.close() 00:1D:28:92:A2:D1
10-04 12:55:09.312: D/CBluetoothAdapter(27291): Connection failed 00:1D:28:92:A2:D1
10-04 12:55:09.316: D/CBluetoothAdapter(27291): Disconnect 00:1D:28:92:A2:D1
10-04 12:55:09.316: D/CBluetoothAdapter(27291): mConnectThread socket close() 00:1D:28:92:A2:D1
10-04 12:55:09.316: D/CBluetoothAdapter(27291): State changed: DISABLED
10-04 12:55:09.316: D/CBluetoothAdapter(27291): Send msg MSG_CONNECTION_FAILED 00:1D:28:92:A2:D1     1079579976
10-04 12:55:09.320: D/CBluetoothAdapter(27291): Start connect 00:0A:84:02:77:70
10-04 12:55:09.324: D/CBluetoothAdapter(27291): Connecting to device 00:0A:84:02:77:70
10-04 12:55:09.324: D/CBluetoothAdapter(27291): connect to: 00:0A:84:02:77:70
10-04 12:55:09.324: D/CBluetoothAdapter(27291): create mConnectThread 00:0A:84:02:77:70
10-04 12:55:09.328: D/CBluetoothAdapter(27291): create socket
10-04 12:55:09.335: D/CBluetoothAdapter(27291): State changed: CONNECTING
10-04 12:55:09.339: D/CBluetoothAdapter(27291): run mConnectThread 00:0A:84:02:77:70
10-04 12:55:09.343: D/CBluetoothAdapter(27291): socket.connect() 00:0A:84:02:77:70
10-04 12:55:15.359: E/CBluetoothAdapter(27291): Service discovery failed 00:0A:84:02:77:70
10-04 12:55:15.359: E/CBluetoothAdapter(27291): java.io.IOException: Service discovery failed
10-04 12:55:15.359: E/CBluetoothAdapter(27291):     at     android.bluetooth.BluetoothSocket$SdpHelper.doSdp(BluetoothSocket.java:406)
10-04 12:55:15.359: E/CBluetoothAdapter(27291):     at     android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:217)
10-04 12:55:15.359: E/CBluetoothAdapter(27291):     at     scatemobile.adapters.CBluetoothAdapter$ConnectThread.run(CBluetoothAdapter.java:473)
10-04 12:55:15.367: D/CBluetoothAdapter(27291): mConnectThread mmSocket.close() 00:0A:84:02:77:70
10-04 12:55:15.367: D/CBluetoothAdapter(27291): Connection failed 00:0A:84:02:77:70
10-04 12:55:15.367: D/CBluetoothAdapter(27291): Disconnect 00:0A:84:02:77:70
10-04 12:55:15.367: D/CBluetoothAdapter(27291): mConnectThread socket close() 00:0A:84:02:77:70
10-04 12:55:15.367: D/CBluetoothAdapter(27291): State changed: DISABLED
10-04 12:55:15.375: D/CBluetoothAdapter(27291): Start connect 00:0A:84:02:5F:3A
10-04 12:55:15.375: D/CBluetoothAdapter(27291): Send msg MSG_CONNECTION_FAILED 00:0A:84:02:77:70 1079579976

I changed my code. 我改变了我的代码。 Now I use reflection on creating BluetoothSocket: 现在我使用反射来创建BluetoothSocket:

Method m;

m = device.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
tmp = (BluetoothSocket)m.invoke(device, 1);

And it works for me! 它对我有用! Problem solved. 问题解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM