2013-06-25 137 views
6

我想我的Nexus 4采用了Wii的平衡板连接,但我得到这个错误:安卓:getBluetoothService()调用时没有BluetoothManagerCallback

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

任何帮助还是想法探索?

+0

你使用的是什么版本的android?我现在也突然得到这个错误,但从来没有过。 –

+0

我刚开始在Android 4.4上看到这个错误。我在各种设备上使用蓝牙完成了大量工作,不过大部分是2.x和3.x。 –

回答

1

尝试在套接字对象创建之前通过getDefaultAdapter()获取BluetoothAdapter。看起来回调服务是在上述呼叫引用BLuetoothAdater时创建的。详情如下: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/bluetooth/BluetoothAdapter.java

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

套接字连接()的getBluetoothService()的说法总是空,看下面的代码:

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

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

+0

我忘了说我称之为“_adapter = BluetoothAdapter.getDefaultAdapter();”在套接字对象创建之前。 – omniyo

+0

您是否尝试使用BluetoothDevice创建套接字,或者您希望通过隐式BluetoothSocket构造函数实现更好的控制(端口通道等)?而在你测试你的代码的Android产品修订版上? –

+0

@omniyo你能解决这个问题吗? – momo