IOException: read failed, socket might closed - Bluetooth on Android 4.3

后端 未结 16 2105
天涯浪人
天涯浪人 2020-11-22 04:08

Currently I am trying to deal with a strange Exception when opening a BluetoothSocket on my Nexus 7 (2012), with Android 4.3 (Build JWR66Y, I guess the second 4.3 update). I

16条回答
  •  广开言路
    2020-11-22 04:35

    Bluetooth devices can operate in both classic and LE mode at the same time. Sometimes they use a different MAC address depending on which way you are connecting. Calling socket.connect() is using Bluetooth Classic, so you have to make sure the device you got when you scanned was really a classic device.

    It's easy to filter for only Classic devices, however:

    if(BluetoothDevice.DEVICE_TYPE_LE == device.getType()){ //socket.connect() }

    Without this check, it's a race condition as to whether a hybrid scan will give you the Classic device or the BLE device first. It may appear as intermittent inability to connect, or as certain devices being able to connect reliably while others seemingly never can.

提交回复
热议问题