Reputation: 83
This question confused me for a long time. I had seen all kinds of solutions,including IOException: read failed, socket might closed - Bluetooth on Android 4.3 , but all not work.
Main ideas: 1. bond the device; 2. create a socket by uuid; 3. cancelDiscovery and connect.
The log list is as follows:
04-14 11:12:53.044: W/BluetoothAdapter(22212): getBluetoothService() called with no BluetoothManagerCallback
04-14 11:12:53.044: W/bt-l2cap(2445): L2CA_ErtmConnectReq() PSM: 0x0001 BDA: dc2c2600006c p_ertm_info: 0x00000000 allowed:0x0 preferred:0
04-14 11:12:53.044: W/bt-l2cap(2445): L2CAP - st: CLOSED evt: 21
04-14 11:12:53.044: W/bt-l2cap(2445): L2CAP - st: CLOSED evt: 7
04-14 11:12:53.064: W/bt-l2cap(2445): L2CAP - st: W4_L2CAP_CON_RSP evt: 11
04-14 11:12:53.064: W/bt-l2cap(2445): L2CAP - st: CONFIG evt: 24
04-14 11:12:53.074: W/bt-l2cap(2445): L2CAP - st: CONFIG evt: 14
04-14 11:12:53.074: W/bt-l2cap(2445): L2CAP - st: CONFIG evt: 25
04-14 11:12:53.074: W/bt-l2cap(2445): L2CAP - st: CONFIG evt: 15
04-14 11:12:53.264: W/bt-sdp(2445): process_service_search_attr_rsp
04-14 11:12:53.264: W/bt-l2cap(2445): L2CA_DisconnectReq() CID: 0x004a
04-14 11:12:53.274: W/bt-l2cap(2445): L2CAP - st: W4_L2CAP_DISC_RSP evt: 18
04-14 11:12:53.274: E/bt-btif(2445): DISCOVERY_COMP_EVT slot id:91, failed to find channle, status:1, scn:0
04-14 11:12:53.274: W/bt-btif(2445): invalid rfc slot id: 91
04-14 11:12:53.274: W/System.err(22212): java.io.IOException: read failed, socket might closed or timeout, read ret: -1
04-14 11:12:53.274: W/System.err(22212): at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:574)
04-14 11:12:53.274: W/System.err(22212): at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:585)
04-14 11:12:53.274: W/System.err(22212): at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:326)
04-14 11:12:53.274: W/System.err(22212): at com.wormhole.bluetoothdemo.MainActivity$ListItemClickListener.onItemClick(MainActivity.java:152)
04-14 11:12:53.274: W/System.err(22212): at android.widget.AdapterView.performItemClick(AdapterView.java:299)
04-14 11:12:53.274: W/System.err(22212): at android.widget.AbsListView.performItemClick(AbsListView.java:1115)
04-14 11:12:53.284: W/System.err(22212): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2928)
04-14 11:12:53.284: W/System.err(22212): at android.widget.AbsListView$3.run(AbsListView.java:3691)
04-14 11:12:53.284: W/System.err(22212): at android.os.Handler.handleCallback(Handler.java:733)
04-14 11:12:53.284: W/System.err(22212): at android.os.Handler.dispatchMessage(Handler.java:95)
04-14 11:12:53.284: W/System.err(22212): at android.os.Looper.loop(Looper.java:136)
04-14 11:12:53.284: W/System.err(22212): at android.app.ActivityThread.main(ActivityThread.java:5072)
04-14 11:12:53.284: W/System.err(22212): at java.lang.reflect.Method.invokeNative(Native Method)
04-14 11:12:53.294: W/System.err(22212): at java.lang.reflect.Method.invoke(Method.java:515)
04-14 11:12:53.294: W/System.err(22212): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-14 11:12:53.294: W/System.err(22212): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
04-14 11:12:53.294: W/System.err(22212): at dalvik.system.NativeStart.main(Native Method)
Other information: Master device is my phone(android 4.4.4 or android 5.1.1), and slave device is a HID device, UUID is "00001124-0000-1000-8000-00805f9b34fb"
Thanks for all if could help.
Upvotes: 3
Views: 2765
Reputation: 21
My solution
private void connect(BluetoothDevice bluetoothDevice) {
try {
int sdk = Build.VERSION.SDK_INT;
if (sdk >= 10) {
mBluetoothSocket = bluetoothDevice.createInsecureRfcommSocketToServiceRecord(BltContant.SPP_UUID);
} else {
mBluetoothSocket = bluetoothDevice.createRfcommSocketToServiceRecord(BltContant.SPP_UUID);
}
if (mBluetoothSocket != null) {
APP.bluetoothSocket = mBluetoothSocket;
if (bluetoothadapter.isDiscovering()) {
bluetoothadapter.cancelDiscovery();
}
if (!mBluetoothSocket.isConnected()) {
mBluetoothSocket.connect();
}
//EventBus.getDefault().post(new BluRxBean(connectsuccess, bluetoothDevice));
}
} catch (IOException connectException) {
connectException.printStackTrace();
try {
Method m = bluetoothDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class});
mBluetoothSocket = (BluetoothSocket) m.invoke(bluetoothDevice, 1);
mBluetoothSocket.connect();
} catch (Exception e) {
Log.e("BLUE", e.toString());
try {
mBluetoothSocket.close();
} catch (IOException ie) {
ie.printStackTrace();
}
}
}
}
Upvotes: 0
Reputation: 83
In my situation,the bluetooth settings that comes with the system works.
So, I look the source code of the bluetooth settings.
And Copy the libframeworks/base/packages/SettingsLib/src/com/android/settingslib/bluetooth
into my project.
Use reflect to invoke the hide Class and function or variable.
Finally, it works.
I wish this could help.
Upvotes: 2