3

我正在使用从 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 连接。

谢谢你。

4

2 回答 2

3

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

于 2012-12-05T17:16:29.390 回答
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 并且它可以工作

于 2017-10-01T15:22:19.323 回答