Nexus 5无法连接到外围设备

时间:2014-04-01 06:55:44

标签: android bluetooth nexus-5

我已经构建了一个Android应用程序,可以通过蓝牙(经典蓝牙)使用BluetoothSocket.connect()连接到外围设备,并与它进行一些交互。该应用程序适用于三星Galaxy S3(4.3)和注3(4.3),但在Nexus 5上,该应用程序无法连接到该设备。

我有2个Nexus 5,而且两个都无法连接,是什么让我觉得Nexus 5肯定有问题,而不是我。但是当我使用设备制造商支持的应用程序连接到设备时,即使使用Nexus 5,也可以建立连接。

所以我猜,经典蓝牙上的Nexus 5一定有问题,这使得建立连接变得不容易,并且可能有一些方法可以解决它。

抱歉我的英语不好。感谢您提供有关如何解决此问题的任何建议。

以下是我的logcat信息的3个块:

三星Galaxy S3的logcat信息(工作):

04-01 17:03:33.340 D/BloodPressureService﹕ connect to: 8C:DE:52:2C:68:6D
04-01 17:03:33.360 D/BluetoothUtils﹕ isSocketAllowedBySecurityPolicy start : device null
04-01 17:03:33.360 W/BluetoothAdapter﹕ getBluetoothService() called with no BluetoothManagerCallback
04-01 17:03:33.360 D/BloodPressureService﹕ setState() 0 -> 1
04-01 17:03:33.365 I/BloodPressureMonitorFragment﹕ MESSAGE_STATE_CHANGE: 1
04-01 17:03:35.095 D/BloodPressureService﹕ connected
04-01 17:03:35.095 D/BloodPressureService$ConnectedThread﹕ create ConnectedThread
04-01 17:03:35.105 I/BloodPressureService$ConnectedThread﹕ BEGIN mConnectedThread
04-01 17:03:35.110 D/BloodPressureService﹕ setState() 1 -> 2
04-01 17:03:35.125 I/BloodPressureMonitorFragment﹕ MESSAGE_STATE_CHANGE: 2

第一次使用Nexus 5的logcat信息(工作。不知道为什么,我之前从未见过。但只有一次):

04-01 17:14:37.577 D/BloodPressureService﹕ connect to: 8C:DE:52:2C:68:6D
04-01 17:14:37.577 D/BloodPressureService﹕ setState() 0 -> 1
04-01 17:14:37.577 I/BloodPressureMonitorFragment﹕ MESSAGE_STATE_CHANGE: 1
04-01 17:14:37.587 W/BluetoothAdapter﹕ getBluetoothService() called with no BluetoothManagerCallback
04-01 17:14:37.587 D/BluetoothSocket﹕ connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[47]}
04-01 17:14:39.207 D/BloodPressureService﹕ connected
04-01 17:14:39.207 D/BloodPressureService$ConnectedThread﹕ create ConnectedThread
04-01 17:14:39.207 D/BloodPressureService﹕ setState() 1 -> 2
04-01 17:14:39.207 I/BloodPressureService$ConnectedThread﹕ BEGIN mConnectedThread
04-01 17:14:39.217 I/BloodPressureMonitorFragment﹕ MESSAGE_STATE_CHANGE: 2

第二次使用Nexus 5的logcat信息(不起作用。再也不用了):

04-01 17:14:59.977 D/BloodPressureService﹕ connect to: 8C:DE:52:2C:68:6D
04-01 17:14:59.977 D/BloodPressureService﹕ setState() 0 -> 1
04-01 17:14:59.977 I/BloodPressureMonitorFragment﹕ MESSAGE_STATE_CHANGE: 1
04-01 17:14:59.977 W/BluetoothAdapter﹕ getBluetoothService() called with no BluetoothManagerCallback
04-01 17:14:59.987 D/BluetoothSocket﹕ connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[56]}

1 个答案:

答案 0 :(得分:1)

蓝牙堆栈与Nexus 5有一些变化,这是有史以来最烦人的事情。当您打开套接字时,您可以尝试使用不安全的套接字,看看是否有帮助。还要确保所有现有套接字都已关闭且为null: -

adapter= BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device = adapter.getRemoteDevice(mAddress);
BluetoothSocket socket = device.createInsecureRfcommSocketToServiceRecord(UUID);
socket.connect();

此时你应该尝试检索输出流: -

 out = socket.getOutputStream();
 in = socket.getInputStream();

还要确保完全关闭插座: -

        if (in != null) { 
            in.close();
            in = null;
        }
        if (out != null) { 
            out.close();
            out = null;
        }
        if (socket != null) { 
            socket.close();
            socket = null;
        }