我想配对我的Android手机和SPP蓝牙没有针脚和确认对话.我有这个代码,注册BroadcastReceiver:
IntentFilter filter = new IntentFilter(ACTION_PAIRING_REQUEST);
registerReceiver(mPairReceiver, filter);
pairDevice(device);
Run Code Online (Sandbox Code Playgroud)
pairDevice方法:
private void pairDevice(BluetoothDevice device) {
try {
Method method = device.getClass().getMethod("createBond", (Class[]) null);
method.invoke(device, (Object[]) null);
} catch (Exception e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
BroadcastReceiver:
private final BroadcastReceiver mPairReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_PAIRING_REQUEST.equals(action)) {
System.out.println("ACTION_PAIRING_REQUEST");
setBluetoothPairingPin(device);
}
}
};
Run Code Online (Sandbox Code Playgroud)
setBluetoothPairingPin方法:
public void setBluetoothPairingPin(BluetoothDevice device)
{
byte[] pinBytes = convertPinToBytes("0000");
try {
Log.d(TAG, …Run Code Online (Sandbox Code Playgroud)