问题
In the Android 2.3.3 BluetoothChat example with with createInsecureRfcommSocketToServiceRecord() API, users are still prompted to accept the pairing request, even though no PIN code is presented.
Is there a way to automate Bluetooth pairing request without user intervention? Or is this never possible due to security concerns? I have been looking online for 2 days now and haven't really found much, so if anybody knows, please post.
Thanks!
回答1:
Not with the standard API, no: if the MAC address is not already in the pairing database there will always be the prompt. I'm told that if you have a device that has been rooted and have public read/write access to the bluetooth service's DBus endpoint you can work around that but I've never seen that actually implemented.
回答2:
So, I had this cuestion, if some one needs the answer to this working in 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);
and the code for the Receiver
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();
}
}
}
}
and in the manifest file
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
and the broadcastReceiver
<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>
回答3:
i came across the same problem, i hope the following code will help: firsly we need:
<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>
secondly we need the BluetoothDevice class, and:
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);
}
}
}
来源:https://stackoverflow.com/questions/7537313/auto-accept-bluetooth-pairing-possible