2013-06-25 136 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