Android蓝牙socket.connect失败;抛出java IO异常

时间:2012-11-30 22:41:05

标签: android io bluetooth java-io

我正在Android Developer页面上使用从蓝牙教程派生的代码。 我的应用程序尝试连接到BT秤,并记录重量信息。

我的程序在尝试执行BluetoothSocket.connect()时会抛出错误。 该程序作为蓝牙客户端/从属设备运行,以便从BT设备接收数据。 用户在屏幕上看到已配对设备的列表,然后单击特定设备以连接到该设备。

因此,当用户点击特定设备时,该应用会尝试连接到该设备,因为该设备已预先配对。

该应用获取BluetoothSocket但无法执行connect()

我试图与其他蓝牙设备建立连接,但我总是一遍又一遍地得到同样的错误。

以下是connectThread的java代码:

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;
            Log.i(tag, "construct");
            // 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) { 
                Log.i(tag, "get socket failed");

            }
            mmSocket = tmp;
        }

        public void run() {
            // Cancel discovery because it will slow down the connection
            btAdapter.cancelDiscovery();
            Log.i(tag, "connect - run");
            try {
                // Connect the device through the socket. This will block
                // until it succeeds or throws an exception
                mmSocket.connect();
                Log.i(tag, "connect - succeeded");
            } catch (IOException connectException) {    Log.i(tag, "connect failed");
                // 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();
        }

可以在 here

找到该应用的完整代码

以下是我在运行应用程序时获得的日志:

01-01 01:46:00.359: I/ActivityManager(1553): Displayed com.test.bluetooth/.Main_Activity: +579ms
01-01 01:46:00.953: D/BluetoothService(1553): updateDeviceServiceChannelCache(00:10:C6:2E:CB:C3)
01-01 01:46:00.968: D/BluetoothService(1553): updateDeviceServiceChannelCache(00:19:15:66:C4:2D)
01-01 01:46:00.984: D/BluetoothService(1553): updateDeviceServiceChannelCache(00:1A:88:00:01:25)
01-01 01:46:01.000: D/BluetoothService(1553):   uuid(system): 00001105-0000-1000-8000-00805f9b34fb 1
01-01 01:46:04.421: I/debugging(2400): construct
01-01 01:46:04.421: D/BluetoothEventLoop(1553): Property Changed: Discovering : false
01-01 01:46:04.421: I/debugging(2400): in click listener
01-01 01:46:04.421: E/BluetoothService.cpp(1553): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session)
01-01 01:46:04.429: E/BluetoothService.cpp(1553): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session)
01-01 01:46:04.437: I/debugging(2400): connect - run
01-01 01:46:04.437: V/BluetoothDiscoveryReceiver(1864): Received: android.bluetooth.adapter.action.DISCOVERY_FINISHED
01-01 01:46:04.445: E/BluetoothEventLoop.cpp(1553): onCreateDeviceResult: D-Bus error: org.bluez.Error.AlreadyExists (Already Exists)
01-01 01:46:05.484: D/BluetoothEventLoop(1553): Device property changed: 00:10:C6:2E:CB:C3 property: Connected value: true
01-01 01:46:06.554: D/BluetoothService(1553): updateDeviceServiceChannelCache(00:10:C6:2E:CB:C3)
01-01 01:46:06.562: D/BluetoothService(1553):   uuid(application): 00001101-0000-1000-8000-00805f9b34fb 1
01-01 01:46:06.562: D/BluetoothService(1553): Making callback for 00001101-0000-1000-8000-00805f9b34fb with result 1
01-01 01:46:06.562: I/debugging(2400): connect failed
01-01 01:46:06.562: W/System.err(2400): java.io.IOException: Invalid argument
01-01 01:46:06.562: W/System.err(2400):     at android.bluetooth.BluetoothSocket.connectNative(Native Method)
01-01 01:46:06.562: W/System.err(2400):     at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:216)
01-01 01:46:06.562: W/System.err(2400):     at com.test.bluetooth.Main_Activity$ConnectThread.run(Main_Activity.java:326)
01-01 01:46:10.132: D/BluetoothEventLoop(1553): Device property changed: 00:10:C6:2E:CB:C3 property: Connected value: false
01-01 01:47:05.914: D/SurfaceFlinger(1311): About to give-up screen, flinger = 0x41876af0

非常感谢任何/所有帮助以建立基本的BT连接。

谢谢。

2 个答案:

答案 0 :(得分:3)

检查以确保您使用的UUID适合您尝试连接的设备类型。

答案 1 :(得分:0)

连接到移动设备以外的蓝牙设备时出现同样的问题。更改UUID值对我有用。早些时候我在https://github.com/googlesamples/android-BluetoothChat/blob/master/Application/src/main/java/com/example/android/bluetoothchat/BluetoothChatService.java使用了UUID值提及 但那是移动蓝牙设备然后我用00001101-0000-1000-8000-00805F9B34FB更改了我的UUID并且它可以工作