3

在带有 createInsecureRfcommSocketToServiceRecord() API 的 Android 2.3.3 BluetoothChat 示例中,即使没有提供 PIN 码,仍会提示用户接受配对请求。

有没有一种无需用户干预即可自动执行蓝牙配对请求的方法?还是出于安全考虑,这永远不可能?我已经在网上找了2天了,并没有真正找到太多,所以如果有人知道,请发帖。

谢谢!

4

3 回答 3

6

所以,我有这个提示,如果有人需要在 android 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 地址不在配对数据库中,则始终会出现提示。有人告诉我,如果您的设备已植根并且对蓝牙服务的 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 回答