2013-09-27 166 views
17

我試圖連接的藍牙設備始終具有相同的PIN碼。這應該可以通過以編程方式設置引腳來配對設備。以編程方式配對藍牙設備,而無需用戶輸入PIN碼

試圖尋找如何可以這樣做之後,我結束了下面的代碼:

BluetoothDevice device = getDevice(); 

//To avoid the popup notification: 
device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true); 
device.getClass().getMethod("cancelPairingUserInput", boolean.class).invoke(device, true); 
byte[] pin = ByteBuffer.allocate(4).putInt(1234).array(); 
//int pinn = 1234; 

//Entering pin programmatically: 
Method ms = device.getClass().getMethod("setPin", byte[].class); 
//Method ms = device.getClass().getMethod("setPasskey", int.class); 
ms.invoke(device, pin); 

//Bonding the device: 
Method mm = device.getClass().getMethod("createBond", (Class[]) null); 
mm.invoke(device, (Object[]) null); 

cancelPairingUserInput給了我一個NoSuchMethodException,因爲該方法在BluetoothDevice類存在這是奇怪的。

看起來像SetpinSetPasskey不會做任何事情。該設備不會配對。它只能在手動輸入引腳後配對。

所以,工程代碼的唯一路線是:

//Bonding the device: 
Method mm = device.getClass().getMethod("createBond", (Class[]) null); 
mm.invoke(device, (Object[]) null); 

logcat的輸出:

09-27 12:34:46.408: ERROR/App(11671): cancelPairingUserInput [boolean] 
     java.lang.NoSuchMethodException: cancelPairingUserInput [boolean] 
     at java.lang.Class.getConstructorOrMethod(Class.java:460) 
     at java.lang.Class.getMethod(Class.java:915) 
     at test.app.bluetooth.model.BluetoothDiscoveryAndPairing.pair(BluetoothDiscoveryAndPairing.java:97) 
     at test.app.bluetooth.model.BluetoothDiscoveryAndPairing.access$000(BluetoothDiscoveryAndPairing.java:25) 
     at test.app.bluetooth.model.BluetoothDiscoveryAndPairing$1.onReceive(BluetoothDiscoveryAndPairing.java:79) 
     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:756) 
     at android.os.Handler.handleCallback(Handler.java:615) 
     at android.os.Handler.dispatchMessage(Handler.java:92) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:4921) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:511) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 
     at dalvik.system.NativeStart.main(Native Method) 

那我做錯了嗎?

+0

@DuncanJones我加堆棧跟蹤到我的startpost。 – user1816451

回答

11

隱藏方法cancelPairingUserInput在您的設備中不存在。不要使用它。

  1. 您應該註冊的BroadcastReceiver的android.bluetooth.device.action.PAIRING_REQUEST
  2. 呼叫createBond()
  3. 等待廣播接收器來觸發
  4. 在廣播接收器,如果動作是android.bluetooth.device.action .PAIRING_REQUEST 調用此方法
public void setBluetoothPairingPin(BluetoothDevice device) 
{ 
    byte[] pinBytes = convertPinToBytes("0000"); 
    try { 
      Log.d(TAG, "Try to set the PIN"); 
      Method m = device.getClass().getMethod("setPin", byte[].class); 
      m.invoke(device, pinBytes); 
      Log.d(TAG, "Success to add the PIN."); 
      try { 
       device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true); 
       Log.d(TAG, "Success to setPairingConfirmation."); 
      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       Log.e(TAG, e.getMessage()); 
       e.printStackTrace(); 
      } 
     } catch (Exception e) { 
      Log.e(TAG, e.getMessage()); 
      e.printStackTrace(); 
     } 
} 

它也適用於帶有Jelly Bean版本(4.1.2)的Android設備。

+2

這可以在19年前的API級別完成嗎? android.bluetooth.device.action.PAIRING_REQUEST僅在API 19上引入 – FOliveira

+0

爲什麼需要setPairingConfirmation()? Android文檔說它只適用於PAIRING_VARIANT_PASSKEY_CONFIRMATION,而不是傳統配對。此外,在Android6上,它需要BLUETOOTH_PRIVILEGED –

2

這是爲我工作:

IntentFilter filter2 = new IntentFilter(
      "android.bluetooth.device.action.PAIRING_REQUEST"); 
    mActivity.registerReceiver(
      pairingRequest, filter2); 

private final BroadcastReceiver pairingRequest = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 

     if (intent.getAction().equals("android.bluetooth.device.action.PAIRING_REQUEST")) { 
      mBluetoothDevice = needed; 
       try { 
        byte[] pin = (byte[]) BluetoothDevice.class.getMethod("convertPinToBytes", String.class).invoke(BluetoothDevice.class, "1234"); 
        Method m = mBluetoothDevice.getClass().getMethod("setPin", byte[].class); 
        m.invoke(mBluetoothDevice, pin); 
        mBluetoothDevice.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(mBluetoothDevice, true); 
} 
    catch(Exception e) 
{ 

    e.printStackTrace(); 

} 
+0

這個代碼是否只需要配對的設備?我們不需要其他正在配對的設備上的任何代碼? –

+0

您不需要任何代碼到其他設備。你應該接受連接。 (我爲連接到移動打印機的android手機編寫了此代碼。) –

相關問題