Java Android蓝牙在没有断开连接的情况下断开连接
我无法从蓝牙设备(GPS记录器)读取数据,大约30秒后,inputstream无法从记录器获取更多数据。 在timspan中,直到连接断开,所有事情都能完美工作(读写) 记录器蓝牙连接指示灯熄灭,我没有断开连接,如ACTION\u ACL\u DISCONNECTED或ACTION\u ACL\u DISCONNECT\u REQUESTED或其他 当连接断开时,logcat会带来以下错误:Java Android蓝牙在没有断开连接的情况下断开连接,java,android,bluetooth,Java,Android,Bluetooth,我无法从蓝牙设备(GPS记录器)读取数据,大约30秒后,inputstream无法从记录器获取更多数据。 在timspan中,直到连接断开,所有事情都能完美工作(读写) 记录器蓝牙连接指示灯熄灭,我没有断开连接,如ACTION\u ACL\u DISCONNECTED或ACTION\u ACL\u DISCONNECT\u REQUESTED或其他 当连接断开时,logcat会带来以下错误: 09-08 15:24:08.219 1795-3811/com.android.bluetoot
09-08 15:24:08.219 1795-3811/com.android.bluetooth I/bt-btif﹕ btif_dm_search_services_evt: event = 2
09-08 15:24:08.219 1795-3811/com.android.bluetooth D/bt-btif﹕ btif_dm_search_services_evt:(result=0x1, services 0x0)
09-08 15:24:08.219 1795-3811/com.android.bluetooth D/BTIF_STORAGE﹕ prop2cfg(L239): in, bd addr:11:22:33:44:74:0e, prop type:3, len:0
09-08 15:24:08.219 1795-3811/com.android.bluetooth E/BTIF_STORAGE﹕ ## ERROR : prop2cfg(L243): property type:3, len:0 is invalid##
09-08 15:24:08.219 1795-3811/com.android.bluetooth E/﹕ ### ASSERT : external/bluetooth/bluedroid/main/../btif/src/btif_dm.c line 1004 storing remote services failed (1) ###
09-08 15:24:08.219 1795-3811/com.android.bluetooth D/﹕ HAL bt_hal_cbacks->remote_device_properties_cb
09-08 15:24:08.229 1795-3811/com.android.bluetooth E/BluetoothRemoteDevices﹕ devicePropertyChangedCallback: bdDevice: 11:22:33:44:74:0E, value is empty for type: 3
此问题发生在读取字节和连接时间(连接和上次读取字节之间的时间)之后
谢谢经过一天又一小时的努力,我终于找到了解决办法。在第一种情况下,当测试未关闭端口时,我的蓝牙设备出现问题。重新启动并重新配对设备后,设备将正常工作。另一款搭载安卓4.1.2的安卓设备在2秒后断开连接-(啊,啊,啊..解决方案不是把原始的 CreateSecurerCommsocketToServiceRecord 这不适用于特殊Spp设备。解决方案是:
try {
_logger.log(Level.FINEST,"createInsecureRfcommSocket by reflection");
fSocket = (BluetoothSocket)fDevice.getClass().getMethod("createInsecureRfcommSocket", new Class[]{Integer.TYPE}).invoke(fDevice, new Object[]{Integer.valueOf(1)});
} catch (NoSuchMethodException e) {
fSocket = null;
} catch (InvocationTargetException e) {
fSocket = null;
} catch (IllegalAccessException e) {
fSocket = null;
}
if (fSocket == null) {
_logger.log(Level.FINEST,"Connect over createInsecureRfcommSocketToServiceRecord");
fSocket = fDevice.createInsecureRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
}
太糟糕了!!!我在一次幸运的休息中发现了这个。我希望它能帮助任何有同样问题的人
Matze我遇到了一个类似的问题,对我来说,添加了一个设备.getUuids()方法,而不是尝试连接标准UUID(000011101-0000-1000-8000-00805F9B34FB)来修复它。但我不太清楚为什么 下面是我根据谷歌蓝牙聊天示例改编的代码
private class ConnectThread extends Thread {
private final BluetoothSocket mmSocket;
private final BluetoothDevice mmDevice;
private String mSocketType;
public ConnectThread(BluetoothDevice device, boolean secure) {
mmDevice = device;
BluetoothSocket tmp = null;
mSocketType = secure ? "Secure" : "Insecure";
try {
if (secure) {
tmp = device.createRfcommSocketToServiceRecord(UUID_SPP);
} else {
UUID mUUID= device.getUuids()[0].getUuid();
tmp = device.createInsecureRfcommSocketToServiceRecord(mUUID);
}
} catch (IOException e) {
Log.e(TAG, "Socket Type: " + mSocketType + "create() failed", e);
}
mmSocket = tmp;
mState = STATE_CONNECTING;
}