Android 套接字与 RFCOMM 蓝牙服务器的不安全连接失败

问题描述

我的目标是连接到在 Raspberry Pi 上运行的 RFCOMM 蓝牙服务器。我需要它在没有密码的情况下工作,因为这个功能必须是完全自动的,没有用户的交互。该项目是在 Flutter 中开发的,但对于这些特定的事情,我决定使用原生。

安卓端:

  • 我使用 Java 的反射创建了一个像这样的不安全的 RFCOMM 套接字
           try {
               socket = (BluetoothSocket) BluetoothDevice.class.getMethod("createInsecureRfcommSocket",int.class).invoke(remoteDevice,channel);
               Log.e(TAG,"socket = " + socket);
           } catch (IllegalAccessException e) {
               e.printStackTrace();
           } catch (InvocationTargetException e) {
               e.printStackTrace();
           } catch (NoSuchMethodException e) {
               e.printStackTrace();
           }
  • 然后我像这样使用套接字连接到蓝牙服务器
            if (socket != null) {
                int attempt = 1;
                if (bluetoothAdapter.isDiscovering())
                    bluetoothAdapter.cancelDiscovery();
                while (attempt <= MAX_CONNECTION_ATTEMPTS && !socket.isConnected()) {
                    try {
                        Log.d(TAG,"Socket connect() attempt:" + attempt);
                        socket.connect();
                    } catch (IOException e) {
                        Log.e(TAG,"Socket connect() failed!");
                        Log.d(TAG,"Socket.isConnected() -> " + socket.isConnected());
                        e.printStackTrace();
                        if (attempt < MAX_CONNECTION_ATTEMPTS)
                            SystemClock.sleep(BLUETOOTH_MSECONDS_SLEEP); // sleep before retry to connect
                    }
                    attempt++;
                }
                if(socket.isConnected()) {
                    try {
                        rfcommChannel = new RfcommChannel(socket.getInputStream(),socket.getOutputStream());
                    } catch (IOException e) {
                        closeConnection();
                    }
                }
            }

树莓派输出:

Agent registered
[CHG] Controller B(:27:EB:47:CF:Be Pairable: yes
[NEW] Device 5C:51:81:6A:18:0E Galaxy J7
[CHG] Device 5C:51:81:6A:18:0E Modalias: bluetooth:v0075p0100d0201
[CHG] Device 5C:51:81:6A:18:0E UUIDs: 00001105-0000-1000-8000-00805f9b34fb
<similar data>
[CHG] Device 5C:51:81:6A:18:0E UUIDs: 00001801-0000-1000-8000-00805f9b34fb
[CHG] Device 5C:51:81:6A:18:0E Services resolved: yes
[CHG] Device 5C:51:81:6A:18:0E Paired: yes
[CHG] Device 5C:51:81:6A:18:0E Services resolved: no
[CHG] Device 5C:51:81:6A:18:0E Paired: no
[CHG] Device 5C:51:81:6A:18:0E Connected: no
[DEL] Device 5C:51:81:6A:18:0E Galaxy J7
NEW] Device 5C:51:81:6A:18:0E Galaxy J7
[CHG] Device 5C:51:81:6A:18:0E Modalias: bluetooth:v0075p0100d0201
[CHG] Device 5C:51:81:6A:18:0E UUIDs: 00001105-0000-1000-8000-00805f9b34fb
<similar data>
[CHG] Device 5C:51:81:6A:18:0E UUIDs: 00001801-0000-1000-8000-00805f9b34fb
[CHG] Device 5C:51:81:6A:18:0E Services resolved: yes
[CHG] Device 5C:51:81:6A:18:0E Paired: yes
[CHG] Device 5C:51:81:6A:18:0E Services resolved: no
[CHG] Device 5C:51:81:6A:18:0E Paired: no
[CHG] Device 5C:51:81:6A:18:0E Center code hereonnected: no
[DEL] Device 5C:51:81:6A:18:0E Galaxy J7

你能帮忙找出连接有什么问题吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...