3

createInsecureRfcommSocketToServiceRecord() API を使用した Android 2.3.3 BluetoothChat の例では、PIN コードが提示されていなくても、ユーザーはペアリング要求を受け入れるように求められます。

ユーザーの介入なしで Bluetooth ペアリング要求を自動化する方法はありますか? それとも、セキュリティ上の懸念により、これは決して不可能ですか? 2日間オンラインで探していましたが、あまり見つけられなかったので、知っている人は投稿してください.

ありがとう!

4

3 に答える 3

6

だから、誰かがアンドロイド4.4.2でこれが機能することに対する答えが必要な場合、私はこのキューズメントを持っていました

 IntentFilter filter = new IntentFilter(
                "android.bluetooth.device.action.PAIRING_REQUEST");


        /*
         * Registering a new BTBroadcast receiver from the Main Activity context
         * with pairing request event
         */
        registerReceiver(
                new PairingRequest(), filter);

およびレシーバーのコード

  public static class PairingRequest extends BroadcastReceiver {
        public PairingRequest() {
            super();
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("android.bluetooth.device.action.PAIRING_REQUEST")) {
                try {
                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    int pin=intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY", 0);
                    //the pin in case you need to accept for an specific pin
                    Log.d("PIN", " " + intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY",0));
                    //maybe you look for a name or address
                    Log.d("Bonded", device.getName());
                    byte[] pinBytes;
                    pinBytes = (""+pin).getBytes("UTF-8");
                    device.setPin(pinBytes);
                    //setPairing confirmation if neeeded
                    device.setPairingConfirmation(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

そしてマニフェストファイルで

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

そしてブロードキャストレシーバー

 <receiver android:name=".MainActivity$PairingRequest">
                <intent-filter>
                    <action android:name="android.bluetooth.device.action.PAIRING_REQUEST" />
                    <action android:name="android.bluetooth.device.action.PAIRING_CANCEL" />
                </intent-filter>
</receiver>
于 2015-05-21T00:25:05.853 に答える
3

標準 API ではありません。MAC アドレスがまだペアリング データベースにない場合は、常にプロンプ​​トが表示されます。ルート化されたデバイスがあり、Bluetooth サービスの DBus エンドポイントへのパブリックな読み取り/書き込みアクセス権を持っている場合、それを回避できると言われていますが、実際に実装されているのを見たことがありません。

于 2011-09-24T07:04:03.953 に答える
3

私は同じ問題に遭遇しました。次のコードが役立つことを願っています:

<receiver android:name=".broadcast.PairingRequest"> <intent-filter> <action android:name="android.bluetooth.device.action.PAIRING_REQUEST" /> <action android:name="android.bluetooth.device.action.PAIRING_CANCEL" /> </intent-filter></receiver>

次に、BluetoothDevice クラスが必要です。

public class PairingRequest extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent){
if (intent.getAction().equals("ACTION_PAIRING_REQUEST")) {
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        byte[] pinBytes = BluetoothDevice.convertPinToBytes("1234");
        device.setPin(pinBytes);
    }
 }
}
于 2011-10-04T20:02:03.083 に答える