.1458 2022-10-09 10:55 采纳率: 50%
浏览 376

android bt传统蓝牙连接失败 getBluetoothService() called with no BluetoothManagerCallback

问题遇到的现象和发生背景

我在做 android bluetooth spp ota 的app项目 现在经典蓝牙的连接这里出了问题

用代码块功能插入代码,请勿粘贴截图

private class ClientThread extends Thread {
private final BluetoothDevice mDevice;
private BluetoothSocket mSocket;

    @SuppressLint("MissingPermission")
    public ClientThread(BluetoothDevice device) {
        mDevice = device;
        try {
            //mSocket =(BluetoothSocket) device.getClass().getMethod("createRfcommSocketToServiceRecord", UUID.class).invoke(device,2);
            mSocket = device.createInsecureRfcommSocketToServiceRecord(SerialPortServiceClass_UUID);
        } catch ( IOException e) {
            Log.e("ClientThread", "create failed", e);
            mSocket = null;
        }
    }

    @SuppressLint("MissingPermission")
    public void run() {
        Log.i("ClientThread", "BEGIN mConnectThread");
        new Thread(() -> {
            //先退出扫描再进行连接
            if ( mBluetoothAdapter.isDiscovering()) {
                mBluetoothAdapter.cancelDiscovery();
            }
            try {
                mSocket.connect();
                Log.d("service","Bluetooth one running(connected)");
            } catch (IOException e) {
                e.printStackTrace();
                try {
                    mSocket.close();
                }catch (IOException e1){
                    e1.printStackTrace();
                }
            }
        }).start();
    }

}

运行结果及报错内容

2022-10-09 10:37:46.908 9899-11377/com.savitech.btota I/ClientThread: BEGIN mConnectThread
2022-10-09 10:37:46.910 9899-11378/com.savitech.btota W/BluetoothAdapter: getBluetoothService() called with no BluetoothManagerCallback
2022-10-09 10:37:47.747 9899-11378/com.savitech.btota W/System.err: java.io.IOException: read failed, socket might closed or timeout, read ret: -1
2022-10-09 10:37:47.747 9899-11378/com.savitech.btota W/System.err: at android.bluetooth.BluetoothSocket.readAll(BluetoothSocket.java:772)
2022-10-09 10:37:47.747 9899-11378/com.savitech.btota W/System.err: at android.bluetooth.BluetoothSocket.readInt(BluetoothSocket.java:786)
2022-10-09 10:37:47.747 9899-11378/com.savitech.btota W/System.err: at android.bluetooth.BluetoothSocket.connect(BluetoothSocket.java:404)
2022-10-09 10:37:47.747 9899-11378/com.savitech.btota W/System.err: at com.savitech.btota.MainActivity$ClientThread.lambda$run$0$com-savitech-btota-MainActivity$ClientThread(MainActivity.java:179)
2022-10-09 10:37:47.747 9899-11378/com.savitech.btota W/System.err: at com.savitech.btota.MainActivity$ClientThread$$ExternalSyntheticLambda0.run(Unknown Source:2)
2022-10-09 10:37:47.747 9899-11378/com.savitech.btota W/System.err: at java.lang.Thread.run(Thread.java:919)
2022-10-09 10:37:47.747 9899-11378/com.savitech.btota D/BluetoothSocket: close() this: android.bluetooth.BluetoothSocket@701412e, channel: -1, mSocketIS: android.net.LocalSocketImpl$SocketInputStream@4f8f4cf, mSocketOS: android.net.LocalSocketImpl$SocketOutputStream@fb17e5cmSocket: android.net.LocalSocket@a3b0565 impl:android.net.LocalSocketImpl@7e4423a fd:java.io.FileDescriptor@b3a0eeb, mSocketState: INIT

我想要达到的结果

想要其能正常连接上蓝牙设备

  • 写回答

1条回答 默认 最新

  • TheRisingMoon 2022-10-10 08:54
    关注

    可以贴一下蓝牙服务端创建的代码看看。还有确认下使用的蓝牙UUID的值,服务端和客户端是不是一致的。

    评论

报告相同问题?

问题事件

  • 创建了问题 10月9日

悬赏问题

  • ¥35 navicat将excel中的数据导入mysql出错
  • ¥15 rt-thread线程切换的问题
  • ¥20 python忆阻器数字识别
  • ¥15 高通uboot 打印ubi init err 22
  • ¥20 PDF元数据中的XMP媒体管理属性
  • ¥15 R语言中lasso回归报错
  • ¥15 网站突然不能访问了,上午还好好的
  • ¥15 有没有dl可以帮弄”我去图书馆”秒选道具和积分
  • ¥15 semrush,SEO,内嵌网站,api
  • ¥15 Stata:为什么reghdfe后的因变量没有被发现识别啊