gpt4 book ai didi

android - 可以自动接受蓝牙配对吗?

转载 作者:太空宇宙 更新时间:2023-11-03 11:45:34 24 4
gpt4 key购买 nike

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

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

谢谢!

最佳答案

所以,如果有人需要这个在 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();
}
}
}
}

在 list 文件中

<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>

关于android - 可以自动接受蓝牙配对吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7537313/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com