Android:getBluetoothService()调用没有BluetoothManagerCallback

时间:2013-06-25 18:07:08

标签: android sockets bluetooth

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

getBluetoothService() called with no BluetoothManagerCallback
connect(), SocketState: INIT, mPfd: null

所以它没有完成连接。

我的插座:

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;
        }
    }
}

它给我的错误:

private BluetoothSocket sk;
...
sk = wSocket.create(wm.dev, 0x11);
...
sk.connect();

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

要探索的任何帮助或想法?

1 个答案:

答案 0 :(得分:1)

尝试在套接字对象创建之前通过getDefaultAdapter()获取BluetoothAdapter。当上面提到的调用引用BLuetoothAdater时,似乎创建了回调服务。详情如下: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/bluetooth/BluetoothAdapter.java

mService = managerService.registerAdapter(mManagerCallback);在调用getDefaultAdapter时加载值。

对于socket connect(),getBluetoothService()参数始终为null,请参阅下面的代码:

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

P.S。似乎google没有直接宣传BluetoothSocket构造函数的使用,并要求使用BluetoothDevice的方法来创建套接字。(来自google网站上的参考资料)背后的原因我不知道。