蓝牙套接字:读取失败,套接字可能关闭或超时(Bluetooth Socket: read failed, socket might closed or timeout)

所以我收到此错误: W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1

我正在尝试使用三星Galaxy S4(API 21)和S2(API 16)通过蓝牙连接到自定义设备。 有时它有效。 我不知道会发生什么。

我知道还有其他问题的答案,但我觉得我已经看到了所有问题。

我不能使用BluetoothDevice的“createRfCommSocket”方法。

我的UUID是正确的,因为我知道我的自定义设备中的UUID。

我无法以编程方式启用和禁用BluetoothAdapter。

我尝试清除App Cache。

清除蓝牙缓存后,有时可以正常工作。

所以我的连接代码:

private class ConnectAsyncTask extends AsyncTask {

@Override

protected Boolean doInBackground(String... params) {

try {

Thread.sleep(5000);

UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(params[0]);

mBluetoothSocket = mBluetoothDevice

.createRfcommSocketToServiceRecord(uuid);

mBluetoothSocket.connect();

mOutputStream = mBluetoothSocket.getOutputStream();

mInputStream = mBluetoothSocket.getInputStream();

return true;

} catch (Exception e) {

e.printStackTrace();

try {

mBluetoothSocket.close();

} catch (Exception e1) {

e1.printStackTrace();

}

return false;

}

}

@Override

protected void onPostExecute(Boolean aBoolean) {

if (aBoolean) {

connectDeviceCallback.onDeviceConnected(null);

startReceiverThread();

startSendingThread();

} else {

connectDeviceCallback.onError("An error occured");

}

}

}

mBluetoothSocket.connect()发生错误。

So I am getting this error: W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1

I am trying on a Samsung Galaxy S4 (API 21) and S2 (API 16) to connect to a custom device via Bluetooth. Sometimes it works. I have no idea what happens.

I know there are other questions with answers, but I feel like I've seen them all.

I can not use "createRfCommSocket" method from BluetoothDevice.

My UUID is correct, since I know the UUID from my custom device.

I can not programmatically enable and disable the BluetoothAdapter.

I tried clearing the App Cache.

It sometimes works after clearing the Bluetooth Cache.

So my code for connecting:

private class ConnectAsyncTask extends AsyncTask {

@Override

protected Boolean doInBackground(String... params) {

try {

Thread.sleep(5000);

UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

mBluetoothDevice = mBluetoothAdapter.getRemoteDevice(params[0]);

mBluetoothSocket = mBluetoothDevice

.createRfcommSocketToServiceRecord(uuid);

mBluetoothSocket.connect();

mOutputStream = mBluetoothSocket.getOutputStream();

mInputStream = mBluetoothSocket.getInputStream();

return true;

} catch (Exception e) {

e.printStackTrace();

try {

mBluetoothSocket.close();

} catch (Exception e1) {

e1.printStackTrace();

}

return false;

}

}

@Override

protected void onPostExecute(Boolean aBoolean) {

if (aBoolean) {

connectDeviceCallback.onDeviceConnected(null);

startReceiverThread();

startSendingThread();

} else {

connectDeviceCallback.onError("An error occured");

}

}

}

Error occurs on mBluetoothSocket.connect().

原文:https://stackoverflow.com/questions/33145494

更新时间:2019-10-25 07:10

最满意答案

我的手机离设备太远,无法正确连接。

My phone was too far away from the device to connect correctly.

2015-10-16

相关问答

我终于找到了解决办法。 魔术隐藏在BluetoothDevice类的引擎盖下(请参阅https://github.com/android/platform_frameworks_base/blob/android-4.3_r2/core/java/android/bluetooth/BluetoothDevice.java#L1037 )。 现在,当我收到这个例外时,我实例化了一个后备的BluetoothSocket ,类似于下面的源代码。 你可以看到,通过反射调用隐藏的方法createRfcom

...

当然, gen_tcp:recv/3 ,超时设置为infinity不会关闭套接字:)请参阅官方文档 。 编辑: 从文档: 此函数以被动模式从套接字接收数据包。 查看setopts/2的文档,了解被动和主动模式之间的区别。 尤其是: 如果值为false(被动模式),则进程必须通过调用gen_tcp:recv / 2,3显式接收传入数据。 您的进程一次只能执行一项操作 - 侦听来自其他进程的关闭消息或等待TCP数据包。 你可以尝试使用gen_tcp:controlling_process/2但我不知道

...

您正在与其他设备不支持的UUID配对的问题。 以下是解决此问题的示例代码。 请记住,这个解决方案可能需要一些稳健性,这只是概念的证明。 人们说FetchUuidsWithSdp不是很稳定,所以你可能想注册广播动作以获得UUID或类似的东西。 现在的代码。 代替 BluetoothSocket btSocket = btDevice.CreateRfcommSocketToServiceRecord(UUID.FromString("00001101-0000-1000-8000-00805F9B3

...

好吧,经过几个小时后,我找到了解决方案。 显然,原始开发人员在JDBC连接字符串中添加了一个socket timeout参数 - 只需删除参数即可,因为默认值为0或无限超时。 之前: String connectionStr = "jdbc:as400://" + systemInfo.getIPAddress() + ":1527" + ";naming=system;socket timeout=30000;thread used=false;errors=full;prompt=false;

...

我在以下问题中找到了答案: - IOException:读取失败,套接字可能关闭 - Android 4.3上的蓝牙 实际上我只能在后退时使用反射,即在连接失败时在捕获内部。 I found the answer in the following question:- IOException: read failed, socket might closed - Bluetooth on Android 4.3 Actually I had to use reflection only on th

...

设置mail.smtp.timeout属性。 请参阅com.sun.mail.smtp包的javadocs 。 I resolve my problem by changing to the newest version of JavaMail (to JavaMail 1.5). I write about it there: http://openejb.979440.n4.nabble.com/Which-version-of-JavaMail-td4665285.html thank's

...

内容长度标题与实际内容长度不匹配,并且正在等待更多数据。 The content-length header didn't match the actual content length and it was waiting for more data.

尝试使用, createInsecureRfcommSocketToServiceRecord(MY_UUID) 代替 createRfcommSocketToServiceRecord(MY_UUID) 这应该可以解决问题。 分享您的调试结果,如果这不能解决问题。 另外,不要生成随机的UUID,请尝试下面的。 UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"); Try to use , createIns

...

实际上,通过为timestamp添加单独的字段和select()超时来检查所有活动的轮询队列,从而使每服务器超时工作。 Actually, got per-server timeaouts working by adding a separate field for timestamp and select() timeout for checking all active polling queue.

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐