获取java.io.IOException异常:读取失败,插座有可能在BluetoothSocket.connect关闭或超时()有可能、插座、异常、io

2023-09-07 23:01:53 作者:自找的痛,何必喊疼

我试图写一个code,只有连接到我(现在)只配对设备上我的Nexus 7运行Android 4.4奇巧。无论我有多少东西都试过了,我仍然得到这个错误。这是最后的code我都试过了,这似乎是尽我见过的人报告为成功。

任何人都可以点我什么,我做错了吗?

  BluetoothManager bluetoothManager =            (BluetoothManager)getSystemService(Context.BLUETOOTH_SERVICE);BluetoothAdapter适配器= bluetoothManager.getAdapter(); // BluetoothAdapter.getDefaultAdapter();如果(!adapter.isEnabled()){    意图enableBtIntent =新意图(BluetoothAdapter.ACTION_REQUEST_ENABLE);    startActivityForResult(enableBtIntent,REQUEST_ENABLE_BT);}BluetoothDevice类BT = adapter.getBondedDevices()迭代器的next()();BluetoothDevice类实际= adapter.getRemoteDevice(bt.getAddress());字符串str =;为(BluetoothDevice类BD:adapter.getBondedDevices()){    STR + = bd.getName()+\\ n;}STR + =实际;textView.setText(STR);的BluetoothSocket插座= actual.createInsecureRfcommSocketToServiceRecord(MY_UUID);adapter.cancelDiscovery();socket.connect();为PrintWriter了=的新PrintWriter(socket.getOutputStream());的out.print(消息);了out.flush(); 

解决方案

我有完全相同的错误(IOException异常读取失败插座有可能关闭或超时)与ELM327 OBD蓝牙适配器。

  UUID MY_UUID = UUID.fromString(00001101-0000-1000-8000-00805F9B34FB);btDevice = btAdapter.getRemoteDevice(btDeviceAddress);btAdapter.cancelDiscovery();btSocket = btDevice.createRfcommSocketToServiceRecord(MY_UUID);btSocket.connect(); 

该ELM327将允许第一个连接并发送/接收正确的,但只有一次。随后,其随后所有的连接将失败,并抛出IOException。为了得到它重新工作,我不得不从OS取消配对的ELM327,然后我能够连接 - 只有一次!如此循环往复......

在上面的描述后,问题得到了解决 - 创建一个不安全插座

  btSocket = btDevice.createInsecureRfcommSocketToServiceRecord(MY_UUID); 
Java异常控制机制和异常处理原则

I'm trying to write a code that only connect to my (for now) only paired device on my Nexus 7 running Android 4.4 KitKat. No matter how many thing I have tried, I still get this error. This is the last code I have tried, which seems to be doing everything I've seen people report as successful.

Can anybody point me to what I'm doing wrong?

BluetoothManager bluetoothManager =
            (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter adapter = bluetoothManager.getAdapter();//BluetoothAdapter.getDefaultAdapter();
if (!adapter.isEnabled()) {
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
BluetoothDevice bt = adapter.getBondedDevices().iterator().next();
BluetoothDevice actual = adapter.getRemoteDevice(bt.getAddress());
String str = "";
for(BluetoothDevice bd : adapter.getBondedDevices()) {
    str += bd.getName() + "\n";
}
str+= actual;

textView.setText(str);
BluetoothSocket socket = actual.createInsecureRfcommSocketToServiceRecord(MY_UUID);
adapter.cancelDiscovery();
socket.connect();
PrintWriter out = new PrintWriter(socket.getOutputStream());
out.print(message);
out.flush();

解决方案

I had the exactly same error (IOException read failed socket might closed or timeout) with an ELM327 OBD bluetooth adapter.

UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
btDevice = btAdapter.getRemoteDevice(btDeviceAddress);
btAdapter.cancelDiscovery();
btSocket = btDevice.createRfcommSocketToServiceRecord(MY_UUID);
btSocket.connect();

The ELM327 would allow the first connection and send/receive correctly, but only once. Then, all later connections would fail with the IOException. To get it working again, I had to unpair the ELM327 from the OS, and then I'd be able to connect - just once! And the cycle repeats ...

The problem was solved as described in the post above - create an INSECURE socket.

btSocket = btDevice.createInsecureRfcommSocketToServiceRecord(MY_UUID);