4

以下のブロードキャストレシーバーを使用してボンドリクエストを取得し、ユーザーポップアップリクエストなしでボンディングします。Android - プログラムでBLEボンディングがすべてのCoolPadで動作しない注3

private static BroadcastReceiver pairingBroadcastReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(intent.getAction())) { 
       BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
       int type = intent.getIntExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, BluetoothDevice.ERROR); 

       Toast.makeText(context, "broadcast type "+type, Toast.LENGTH_LONG).show(); 

       if (type == BluetoothDevice.PAIRING_VARIANT_PIN) { 
        if(devicePin != null) { 
         CommonStuff.Log("bond pin "+devicePin); 
         Toast.makeText(context, "broadcast bond pin "+devicePin, Toast.LENGTH_LONG).show(); 
         byte[] pin = new byte[10]; 
         try { 
          pin = (byte[]) BluetoothDevice.class.getMethod("convertPinToBytes", String.class) 
            .invoke(BluetoothDevice.class, devicePin); 
         } catch (IllegalAccessException e) { 

         } catch (InvocationTargetException e) { 

         } catch (NoSuchMethodException e) { 

         } 
         /*for (int i=0; i< pin.length; i++) { 
          CommonStuff.Log("byte bond pin "+pin[i]); 
         }*/ 
         device.setPin(pin); 
         /*if(type == BluetoothDevice.PAIRING_VARIANT_PASSKEY_CONFIRMATION || type == 1) { 
          device.setPairingConfirmation(true); 
         }*/ 
         abortBroadcast(); 
        } 
       } 
      } else if(BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(intent.getAction())) { 
       BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
       int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.BOND_NONE); 

       if(state == BluetoothDevice.BOND_BONDING) 
        return; 

        if(state == BluetoothDevice.BOND_BONDED) { 
         Toast.makeText(context, "Bond success", Toast.LENGTH_LONG).show(); 
        } else { 
         Toast.makeText(context, "Bond failed", Toast.LENGTH_LONG).show(); 
        } 
      } 
     } 
    }; 

devicePinがグローバルに初期化されているものとします。これはほとんどの電話機で機能します。しかしクールパッドノート3では、BluetoothDevice.EXTRA_PAIRING_VARIANTの値は1です。そして、setPin()を使用してこのデバイスを接続しようとすると、EXTRA_PAIRING_VARIANTという値は考慮されず、結合は失敗します。

誰もがこの同じ問題に直面しましたか?これを整理するのを手伝ってください。 1として

答えて

2

BluetoothDevice.EXTRA_PAIRING_VARIANTは、パスキーを入力することができる唯一のユーザです。しかし、プログラムでは、のsetpasskeyというプライベートメソッドを使用してパスキーを設定できます。上記のコードでのタイプのために他のかのケースのように、コードの下に

追加

else if(type == 1) { 
    int pin = Integer.parseInt(devicePin); 
    try { 
     device.getClass().getMethod("setPasskey", int.class) 
       .invoke(device, pin); 
     abortBroadcast(); 
    } catch (IllegalAccessException e) { 

    } catch (InvocationTargetException e) { 

    } catch (NoSuchMethodException e) { 

    } 
} 
関連する問題