蓝牙连接失败“java.io.IOException:读取失败,套接字可能关闭或超时,读取 ret:-1”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25698585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bluetooth Connection failed "java.io.IOException: read failed, socket might closed or timeout, read ret: -1"
提问by Java_begins
I am trying to connect devices through my app installed in Nexus 5.I want to make an app like rainbow contactsin android. In my app I aim to connect to another device through Bluetooth and transfer set of contacts or files. I followed this question, but the workaround mentioned there is not working for me Hereis my full code. This is the code snippet from my application where I have tried to get the socket and make connections.
我正在尝试通过安装在 Nexus 5 中的应用程序连接设备。我想在 android 中制作一个类似彩虹联系人的应用程序。在我的应用程序中,我的目标是通过蓝牙连接到另一台设备并传输一组联系人或文件。我关注了这个问题,但是那里提到的解决方法对我不起作用 这是我的完整代码。这是我尝试获取套接字并建立连接的应用程序的代码片段。
I am able to get through pairing device dialog ,but when i trying to paired then error is raised
我能够通过配对设备对话框,但是当我尝试配对时会引发错误
//to create socket
if (secure) {
bluetoothSocket = device.createRfcommSocketToServiceRecord(uuid);
} else {
bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);
}
//connection establishment
try {
bluetoothSocket.connect();
success = true;
break;
} catch (IOException e) {
//try the fallback
try {
Class<?> clazz = tmp.getRemoteDevice().getClass();
Class<?>[] paramTypes = new Class<?>[] {Integer.TYPE};
Method m = clazz.getMethod("createRfcommSocket", paramTypes);
Object[] params = new Object[] {Integer.valueOf(1)};
bluetoothSocket = (BluetoothSocket) m.invoke(tmp.getRemoteDevice(), params);
Thread.sleep(500);
bluetoothSocket.connect();
success = true;
break;
} catch (FallbackException e1) {
Log.w("BT", "Could not initialize FallbackBluetoothSocket classes.", e);
} catch (InterruptedException e1) {
Log.w("BT", e1.getMessage(), e1);
} catch (IOException e1) {
Log.w("BT", "Fallback failed. Cancelling.", e1);
}
}
Error I am getting
我得到的错误
09-06 13:44:57.247 27860-27860/com.example.gauravdubey.myapplication I/BT﹕ Attempting to connect to Protocol: 00001101-0000-1000-8000-00805f9b34fb
09-06 13:44:57.247 27860-27860/com.example.gauravdubey.myapplication W/BluetoothAdapter﹕ getBluetoothService() called with no BluetoothManagerCallback
09-06 13:44:57.247 27860-27860/com.example.gauravdubey.myapplication D/BluetoothSocket﹕ connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[56]}
09-06 13:44:58.667 27860-27860/com.example.gauravdubey.myapplication W/BluetoothAdapter﹕ getBluetoothService() called with no BluetoothManagerCallback
09-06 13:44:58.667 27860-27860/com.example.gauravdubey.myapplication D/BluetoothSocket﹕ connect(), SocketState: INIT, mPfd: {ParcelFileDescriptor: FileDescriptor[59]}
09-06 13:45:03.267 27860-27860/com.example.gauravdubey.myapplication W/BT﹕ Fallback failed. Cancelling.
java.io.IOException: read failed, socket might closed or timeout, read ret: -1
at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:505)
at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:482)
at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:324)
at com.example.gauravdubey.myapplication.BluetoothConnector$FallbackBluetoothSocket.connect(BluetoothConnector.java:198)
at com.example.gauravdubey.myapplication.BluetoothConnector.connect(BluetoothConnector.java:62)
at com.example.gauravdubey.myapplication.ConnectThread.run(ConnectThread.java:101)
at com.example.gauravdubey.myapplication.MainActivity.onItemClick(MainActivity.java:288)
at android.widget.AdapterView.performItemClick(AdapterView.java:299)
at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2911)
at android.widget.AbsListView.run(AbsListView.java:3645)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
09-06 13:45:03.267 27860-27860/com.example.gauravdubey.myapplication V/connectThread﹕ Could not connect to device: B0:D0:9C:8B:A4:47
09-06 13:45:03.267 27860-27860/com.example.gauravdubey.myapplication I/Choreographer﹕ Skipped 361 frames! The application may be doing too much work on its main thread.
So what i am doing wrong ? any help would be appreciated
那么我做错了什么?任何帮助,将不胜感激
采纳答案by twister_void
In the constructor of BluetoothConnector.java, a list of uuidCandidatesis passed.
在BluetoothConnector.java的构造函数中,传递了一个uuidCandidates列表。
public BluetoothConnector(BluetoothDevice device, boolean secure, BluetoothAdapter adapter,
List<UUID> uuidCandidates)
{
this.device = device;
this.secure = secure;
this.adapter = adapter;
this.uuidCandidates = uuidCandidates;
if (this.uuidCandidates == null || this.uuidCandidates.isEmpty()) {
this.uuidCandidates = new ArrayList<UUID>();
this.uuidCandidates.add(UUID.fromString("fa87c0d0-afac-11de-8a39-0800200c9a66"));
}
}
Have you passed it null?
你有没有通过它?
If yes,then try calling device.fetchUuidsWithSdp()
for the Bluetooth device you want to connect and receive BluetoothDevice.ACTION_UUID
intent in your receiver.As this will fetch a list of uuids supported for the device.
如果是,则尝试调用device.fetchUuidsWithSdp()
您要连接的蓝牙设备并BluetoothDevice.ACTION_UUID
在接收器中接收意图。因为这将获取该设备支持的 uuid 列表。
回答by kalup
Within the try
("//try the fallback") use this:
在try
("//try the fallback") 中使用:
socket =(BluetoothSocket) device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(device,1);
socket.connect();
Another tip may be to use a different UUID, it may solve the problem.
另一个提示可能是使用不同的 UUID,它可能会解决问题。
I don't know if you tryed it yet, but similarly to what has been done in the bluetooth chat example, just before creating your ConnectThread, call this function:
我不知道您是否尝试过,但与蓝牙聊天示例中所做的类似,就在创建 ConnectThread 之前,调用此函数:
public synchronized void connect(BluetoothDevice device) {
// Cancel any thread attempting to make a connection
if (mState == STATE_CONNECTING) {
if (mConnectThread != null) {
mConnectThread.cancel();
mConnectThread = null;
}
}
// Cancel any thread currently running a connection
if (mConnectedThread != null) {
mConnectedThread.cancel();
mConnectedThread = null;
}
// Start the thread to connect with the given device
mConnectThread = new ConnectThread(device);
mConnectThread.start();
setState(STATE_CONNECTING);
}
回答by WU9I
Dirty cache. Find where on the device to clear app cache, then reboot the thing. You may have a device flow control interrupter (firewall, app wall, security software) that intercepts the port and re-issues it-- this is the problem. Depending upon where it instantiates at startup, it's now different than what your app wants. Therefore, rebuild the table by snarfing the cache and rebooting to build the cache/redirect tables back. I know this sounds wickedly ephemeral, but it worked for me.
脏缓存。在设备上找到清除应用程序缓存的位置,然后重新启动设备。您可能有一个设备流控制中断器(防火墙、应用程序墙、安全软件)来拦截端口并重新发出它——这就是问题所在。根据它在启动时实例化的位置,它现在与您的应用程序想要的不同。因此,通过捕获缓存并重新启动以重新构建缓存/重定向表来重建表。我知道这听起来非常短暂,但它对我有用。