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

Android bluetooth socket.connect失败;抛出java IO异常,android,io,bluetooth,java-io,Android,Io,Bluetooth,Java Io,我使用的代码来自Android开发者页面上的蓝牙教程。 我的应用程序尝试连接到BT称重秤并记录重量信息 我的程序在尝试执行BluetoothSocket.connect()时抛出错误; 该程序作为蓝牙客户机/从机运行,以便从BT设备接收数据。 用户在屏幕上看到配对设备的列表,并单击特定设备以连接到该设备 因此,当用户单击特定设备时,应用程序会尝试连接到该设备,因为该设备已预先配对 应用程序获得BluetoothSocket,但无法执行connect() 我曾尝试与其他蓝牙设备建立连接,但我总是一

我使用的代码来自Android开发者页面上的蓝牙教程。 我的应用程序尝试连接到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();
        }
可以找到应用程序的完整代码

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

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连接提供的任何/所有帮助


多谢各位

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

在连接到除移动设备以外的蓝牙设备时遇到相同问题。更改UUID值对我很有用。早些时候,我使用UUID值
但那是针对移动蓝牙设备的,然后我将UUID改为00001101-0000-1000-8000-00805F9B34FB,它可以工作了?如果您使用的是当前环境中太旧(或太新!)的内容,则可能会产生一些错误。@RachelKeslensky我的应用程序使用API级别15(ICS),但我的设备运行的是Jelly Bean。因此,我的设备运行的是一个高级版本的操作系统,在我看来这应该不是一个问题。我确实使用了正确的UUID。事实上,这是我检查的第一件事,以便将其设置正确。它使用SPP,我使用的UUID是:00001101-0000-1000-8000-00805F9B34FB(从我的整个代码中可以看到)