Lil*_*ily 3 android bluetooth pairing
在带有createInsecureRfcommSocketToServiceRecord()API的Android 2.3.3 BluetoothChat示例中,即使未提供PIN码,仍然会提示用户接受配对请求。
有没有一种方法可以自动进行蓝牙配对请求,而无需用户干预?还是出于安全考虑这永远不可能?我已经在网上找了两天了,还没有发现太多东西,所以如果有人知道,请发表。
谢谢!
所以,我有这个提示,如果有人需要在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);
Run Code Online (Sandbox Code Playgroud)
以及接收方的代码
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();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
并在清单文件中
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
Run Code Online (Sandbox Code Playgroud)
和广播接收器
<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>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14762 次 |
| 最近记录: |