【问题标题】:Android bluetooth connection cannot be established on the first try第一次尝试无法建立安卓蓝牙连接
【发布时间】:2016-11-14 17:04:18
【问题描述】:

我正在尝试在我的 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);

也不行

【问题讨论】:

    标签: android sockets bluetooth connection ioexception


    【解决方案1】:

    我会尝试改变

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

    到这里

        // BluetoothDevice device= BluetoothAdapter.getDefaultAdapter().getRemoteDevice("00:1C:4D:02:A6:55");
        // You already have a device so capture the method then invoke a socket
        Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
        socket = (BluetoothSocket)m.invoke(device, Integer.valueOf(1));
        socket.connect();
        // Do some cool stuff
    

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

    【讨论】:

    • 感谢您的回答,但这并没有解决我的问题。输出日志与我附加到我的帖子的第一个日志相同:(
    • 您定义了蓝牙管理器,但您不使用它。我可能会看一下,因为您收到 W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback 的错误
    • 蓝牙管理器是我自己编写的一个类,它充当活动的接口以建立蓝牙连接等。我在线程类中使用它来告诉管理器已建立连接:跨度>
    • if(mmSocket.isConnected()) { System.out.println("Connected"); // 获取一个可以用来发布到主线程的处理程序 Handler mainHandler = new Handler(context.getMainLooper()); Runnable myRunnable = new Runnable() { @Override public void run() { manager.ConnectionEstablishedEvent(mmSocket); } }; mainHandler.post(myRunnable); }(由于字数限制,这是一条新评论)
    • 我已经很多年没有接触过安卓了,所以也许其他人可以帮忙。不过,我会再次查看文档。 developer.android.com/guide/topics/connectivity/bluetooth.html
    猜你喜欢
    • 1970-01-01
    • 2014-08-31
    • 2015-02-11
    • 1970-01-01
    • 2013-07-19
    • 2011-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多