首次尝试时无法建立Android蓝牙连接

时间:2016-07-12 12:38:24

标签: android sockets bluetooth connection ioexception

我正在尝试连接到我的Android应用程序中的蓝牙设备,但我遇到了问题。 似乎我在第一次尝试时永远无法连接到蓝牙设备。

我在BluetoothConnectThread中有以下代码:

public class BluetoothConnectThread extends Thread {
    private BluetoothSocket mmSocket;
    private BluetoothDevice mmDevice;
    private Context context;
    private BluetoothManager manager;

    public BluetoothConnectThread(BluetoothDevice mmDevice, UUID uuid, Context context, BluetoothManager manager) {
        this.context = context;
        this.manager = manager;
        this.mmDevice = mmDevice;
        this.uuid = uuid;
    }

    public void run() {
        try {
            System.out.println("Try to connect");
            mmSocket = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(mmDevice, Integer.valueOf(1));
            mmSocket.connect();
        } catch (Exception connectException) {
            connectException.printStackTrace();
            try {
                mmSocket.close();
                System.out.println("Couldn't establish Bluetooth connection! (1)");
            } catch (IOException closeException) {
                closeException.printStackTrace();
                System.out.println("Couldn't establish Bluetooth connection! (2)");
            }

            try {
                System.out.println("Try to connect again");
                mmSocket = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(mmDevice, Integer.valueOf(1));
                mmSocket.connect();
            } catch (Exception connectException2) {
                connectException.printStackTrace();
                try {
                    mmSocket.close();
                    System.out.println("Couldn't establish Bluetooth connection! (3)");
                } catch (IOException closeException) {
                    closeException.printStackTrace();
                    System.out.println("Couldn't establish Bluetooth connection! (4)");
                }
            }
        }

        if(mmSocket.isConnected()) {
            if(mmSocket.isConnected()) {
                System.out.println("Connected");
    //Do something with the connected socket

调用run方法时收到以下日志:

07-12 14:17:10.906 9941-10518/com.example.niekdewit.test I/System.out: Try to connect
07-12 14:17:10.910 9941-10518/com.example.niekdewit.test W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test W/System.err:     at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:573)
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test W/System.err:     at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:550)
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test W/System.err:     at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:325)
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test W/System.err:     at com.example.niekdewit.test.BluetoothConnectThread.run(BluetoothConnectThread.java:33)
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test I/System.out: Couldn't establish Bluetooth connection! (1)
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test I/System.out: Try to connect again
07-12 14:17:13.025 9941-10518/com.example.niekdewit.test W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
07-12 14:17:13.147 9941-9941/com.example.niekdewit.test I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@2e01eda7 time:762005544
07-12 14:17:16.503 9941-9941/com.example.niekdewit.test I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@2e01eda7 time:762008901
07-12 14:17:17.279 9941-10518/com.example.niekdewit.test I/System.out: Connected

我尝试更换2行

mmSocket = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(mmDevice, Integer.valueOf(1));

 mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);

但这也不起作用,但它会生成不同的日志。使用这种方法我也无法在第二次尝试时连接。

07-12 14:27:15.968 15135-15622/com.example.niekdewit.test I/System.out: Try to connect
07-12 14:27:15.969 15135-15622/com.example.niekdewit.test W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
07-12 14:27:18.205 15135-15622/com.example.niekdewit.test W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
07-12 14:27:18.205 15135-15622/com.example.niekdewit.test W/System.err:     at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:573)
07-12 14:27:18.205 15135-15622/com.example.niekdewit.test W/System.err:     at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:550)
07-12 14:27:18.205 15135-15622/com.example.niekdewit.test W/System.err:     at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:325)
07-12 14:27:18.205 15135-15622/com.example.niekdewit.test W/System.err:     at com.example.niekdewit.test.BluetoothConnectThread.run(BluetoothConnectThread.java:36)
07-12 14:27:18.205 15135-15622/com.example.niekdewit.test I/System.out: Couldn't establish Bluetooth connection! (1)
07-12 14:27:18.206 15135-15622/com.example.niekdewit.test I/System.out: Try to connect again
07-12 14:27:18.206 15135-15622/com.example.niekdewit.test W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
07-12 14:27:18.319 15135-15135/com.example.niekdewit.test I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@2e01eda7 time:762610717
07-12 14:27:20.669 15135-15622/com.example.niekdewit.test W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
07-12 14:27:20.669 15135-15622/com.example.niekdewit.test W/System.err:     at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:573)
07-12 14:27:20.669 15135-15622/com.example.niekdewit.test W/System.err:     at android.bluetooth.BluetoothSocket.waitSocketSignal(BluetoothSocket.java:550)
07-12 14:27:20.669 15135-15622/com.example.niekdewit.test W/System.err:     at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:325)
07-12 14:27:20.669 15135-15622/com.example.niekdewit.test W/System.err:     at com.example.niekdewit.test.BluetoothConnectThread.run(BluetoothConnectThread.java:36)
07-12 14:27:20.669 15135-15622/com.example.niekdewit.test I/System.out: Couldn't establish Bluetooth connection! (3)
07-12 14:27:20.854 15135-15135/com.example.niekdewit.test I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@2e01eda7 time:762613252

当我第一次尝试连接此行时

 mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);

尝试第二次尝试(“尝试再次连接”)与此行

mmSocket = (BluetoothSocket) mmDevice.getClass().getMethod("createRfcommSocket", new Class[]{int.class}).invoke(mmDevice, Integer.valueOf(1));

然后它生成与我附加的第一个日志相同的日志

我不知道发生了什么事? 我希望你们中的一个人能够看到我的代码出了什么问题,或者指出我正确的方向。

编辑: 当设备已配对时,可以使用两种方法在第一次尝试时连接。

EDIT2:

mmSocket = mmDevice.createInsecureRfcommSocketToServiceRecord(uuid);

也不起作用

1 个答案:

答案 0 :(得分:0)

我会尝试改变

usedRange

到这个

TextView

这就是我总是连接到设备的方式