Android:getBluetoothService()调用没有BluetoothManagerCallback

omn*_*iyo 7 sockets android bluetooth

我正在尝试将我的Nexus 4与Wii平衡板连接,但我收到此错误:

getBluetoothService() called with no BluetoothManagerCallback
connect(), SocketState: INIT, mPfd: null
Run Code Online (Sandbox Code Playgroud)

所以它没有完成连接.

我的插座:

public final class wSocket
{
    public static BluetoothSocket create(BluetoothDevice dev, int port)
    {
        try {
        /*
         * BluetoothSocket(int type, int fd, boolean auth, boolean encrypt, BluetoothDevice device, int port, ParcelUuid uuid)
         */
            Constructor<BluetoothSocket> construct = BluetoothSocket.class.getDeclaredConstructor(int.class, int.class, boolean.class,
                boolean.class, BluetoothDevice.class, int.class, ParcelUuid.class);

            construct.setAccessible(true);
            return construct.newInstance(3 /* TYPE_L2CAP */, -1, false, false, dev, port, null);
        } catch (Exception ex) {
            return null;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

它给我的错误:

private BluetoothSocket sk;
...
sk = wSocket.create(wm.dev, 0x11);
...
sk.connect();
Run Code Online (Sandbox Code Playgroud)

我检查此链接没有成功,因为我只打开1个套接字: getbluetoothservice()调用没有bluetoothmanagercallback

有什么帮助或想法去探索?

小智 1

getDefaultAdapter()尝试在创建套接字对象之前通过获取BluetoothAdapter 。当上述调用引用 BLuetoothAdater 时,似乎会创建回调服务。有关详细信息: https: //android.googlesource.com/platform/frameworks/base/+/master/core/java/android/bluetooth/BluetoothAdapter.java

当被调用时,哪里mService = managerService.registerAdapter(mManagerCallback);加载了值 getDefaultAdapter

对于套接字 connect(),getBluetoothService() 参数始终为 null,请参见下面的代码:

https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/bluetooth/BluetoothSocket.java

ps 看来谷歌没有直接宣传BluetoothSocket构造函数的使用,并要求使用BluetoothDevice的方法来创建套接字。(来自谷歌网站上的参考)我不知道背后的原因。

  • 仅适用于任何使用 IOIO 设备(为智能手机提供各种外部接口引脚来访问外部信号)的人,这些人发生在这个线程上:我在应用程序开发过程中开始突然出现此错误,即使重新启动它也不会消失我的 Android 智能手机。当我重新启动(关闭然后重新打开)IOIO 板时,错误消失了。 (3认同)