Android 蓝牙套接字:读取失败,套接字可能已关闭或超时
所以我得到了这个错误:Android 蓝牙套接字:读取失败,套接字可能已关闭或超时,android,sockets,bluetooth,Android,Sockets,Bluetooth,所以我得到了这个错误:W/System.err:java.io.IOException:read失败,套接字可能关闭或超时,read ret:-1 我正在尝试使用三星Galaxy S4(API 21)和S2(API 16)通过蓝牙连接到自定义设备。有时它是有效的。我不知道会发生什么 我知道还有其他问题有答案,但我觉得我已经看到了所有的问题 我无法使用BluetoothDevice中的“createRfCommSocket”方法 我的UUID是正确的,因为我从自定义设备知道UUID 我无法以编程
W/System.err:java.io.IOException:read失败,套接字可能关闭或超时,read ret:-1
我正在尝试使用三星Galaxy S4(API 21)和S2(API 16)通过蓝牙连接到自定义设备。有时它是有效的。我不知道会发生什么
我知道还有其他问题有答案,但我觉得我已经看到了所有的问题
- 我无法使用BluetoothDevice中的“createRfCommSocket”方法李>
- 我的UUID是正确的,因为我从自定义设备知道UUID
- 我无法以编程方式启用和禁用BluetoothAdapter
- 我试图清除应用缓存
- 它有时在清除蓝牙缓存后工作
private class ConnectAsyncTask extends AsyncTask<String, Void, Boolean> {
@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");
}
}
}
私有类ConnectAsyncTask扩展了AsyncTask{
@凌驾
受保护的布尔doInBackground(字符串…参数){
试一试{
睡眠(5000);
UUID UUID=UUID.fromString(“000011101-0000-1000-8000-00805F9B34FB”);
mBluetoothDevice=mBluetoothAdapter.getRemoteDevice(参数[0]);
mBluetoothSocket=mBluetoothDevice
.createrFComSocketToServiceRecord(uuid);
mBluetoothSocket.connect();
mOutputStream=mBluetoothSocket.getOutputStream();
mInputStream=mBluetoothSocket.getInputStream();
返回true;
}捕获(例外e){
e、 printStackTrace();
试一试{
mBluetoothSocket.close();
}捕获(异常e1){
e1.printStackTrace();
}
返回false;
}
}
@凌驾
受保护的void onPostExecute(布尔aBoolean){
if(阿布奥兰语){
connectDeviceCallback.onDeviceConnected(空);
startReceiverThread();
startSendingRead();
}否则{
connectDeviceCallback.onError(“发生错误”);
}
}
}
在
mBluetoothSocket.connect()
上发生错误。我的手机离设备太远,无法正确连接 谷歌真的需要修正这条愚蠢的消息“套接字关闭”是与“套接字超时”完全不同的条件,它们都不同于“连接关闭”、“连接超时”和“连接被拒绝”。