如何以编程方式将蓝牙设备与Android配对

如何以编程方式将蓝牙设备与Android配对,android,bluetooth,android-bluetooth,Android,Bluetooth,Android Bluetooth,我正在开发一个应用程序,其中我想连接蓝牙设备的主要问题是我不希望用户输入所需的pin,而应用程序应该自己完成…我没有任何连接相关的问题…只想插入和完成pin认证过程的应用程序本身 我发现下面的代码,我肯定它是工作,但不知道如何在这个代码中添加pin private void pairDevice(BluetoothDevice device) { try { Log.d("pairDevice()", "Start Pairing...");

我正在开发一个应用程序,其中我想连接蓝牙设备的主要问题是我不希望用户输入所需的pin,而应用程序应该自己完成…我没有任何连接相关的问题…只想插入和完成pin认证过程的应用程序本身

我发现下面的代码,我肯定它是工作,但不知道如何在这个代码中添加pin

private void pairDevice(BluetoothDevice device) {
        try {
            Log.d("pairDevice()", "Start Pairing...");
            Method m = device.getClass().getMethod("createBond", (Class[]) null);
            m.invoke(device, (Object[]) null);
            Log.d("pairDevice()", "Pairing finished.");
        } catch (Exception e) {
            Log.e("pairDevice()", e.getMessage());
        }
    }
有人知道如何在上述代码或任何类似代码中输入pin来解决问题吗。。 谢谢

这似乎给了你答案,输入pin和所有。它包括在收到消息时发送.setPin()

请尝试以下代码:

public void pairDevice(BluetoothDevice device)
{
    String ACTION_PAIRING_REQUEST = "android.bluetooth.device.action.PAIRING_REQUEST";
    Intent intent = new Intent(ACTION_PAIRING_REQUEST);
    String EXTRA_DEVICE = "android.bluetooth.device.extra.DEVICE";
    intent.putExtra(EXTRA_DEVICE, device);
    String EXTRA_PAIRING_VARIANT = "android.bluetooth.device.extra.PAIRING_VARIANT";
    int PAIRING_VARIANT_PIN = 0;
    intent.putExtra(EXTRA_PAIRING_VARIANT, PAIRING_VARIANT_PIN);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
}

Intent intent = new Intent(BluetoothDevice.ACTION_PAIRING_REQUEST);
intent.putExtra(EXTRA_DEVICE, device);
int PAIRING_VARIANT_PIN = 272;
intent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, PAIRING_VARIANT_PIN);
sendBroadcast(intent);

Intent intent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS);
startActivityForResult(intent, REQUEST_PAIR_DEVICE);
我希望这有帮助

参考资料:

试试这个

BluetoothDevice device = intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE");
device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true);
device.getClass().getMethod("cancelPairingUserInput", boolean.class).invoke(device);

所以,我有一个问题,如果有人需要在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" />

还有广播接收机

  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();
                }
            }
        }
    }
 <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>

如何设置pin码已经在上面给出了答案(这对我很有帮助)。然而,我在下面分享了我的简单代码,它适用于Android 6:

BluetoothAdapter mBTA = BluetoothAdapter.getDefaultAdapter();
if (mBTA.isDiscovering()) mBTA.cancelDiscovery();
mBTA.startDiscovery();
...

/** In a broadcast receiver: */

if (BluetoothDevice.ACTION_FOUND.equals(action)) { // One device found.

    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    Log.d(TAG, "Start Pairing... with: " + device.getName());
    device.createBond();
}

// If you want to auto-input the pin#:
else if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)){

                    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                    device.setPin("1234".getBytes());
}

bluetoothDevice.createBond方法,可用于切割

为了检查配对状态,您必须注册广播接收器 BluetoothDevice.ACTION\u BOND\u STATE\u已更改


在receiver类中,您可以检查blueToothDevice.getBondState注册blueToothDevice.ACTION\u PAIRING\u REQUESTreceiver onCreate()

在接收器上,使用
setPin()
设置pin,然后调用
abortBroadcast()

不要忘记在
onDestroy()上注销接收器

如果它不适合您,请尝试设置接收器的高优先级

val pairingRequestFilter = IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST)          
pairingRequestFilter.priority = IntentFilter.SYSTEM_HIGH_PRIORITY - 1
            registerReceiver(pairingReceiver, pairingRequestFilter)
您还可以向Bluetooth设备注册接收器。操作\u绑定\u状态\u已更改
以读取配对状态

val filter = IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED)
        registerReceiver(receiver, filter)

也许这对你有帮助。干杯,@ManolescuSebastian——我想建立安全连接……试试我的答案。我希望它对你有用我在这篇文章中回答了这个问题:在这里检查我的答案,请帮助我…我想要一个已经启用BT的设备列表,在点击特定的蓝牙设备后,它与我们的设备配对我得到第一种方法,但是你建议我们如何处理其他两段代码?这个解决方案对android pie也有效吗?因为我仍然得到确认对话框不,我不认为这个解决方案是针对安卓4.4.2和安卓5的,不确定安卓6和更高版本@TEJASPANDYA此解决方案是否在未经Bluetooth_Priveldge许可的情况下工作?不支持此设备。setPairingConfirmation(true);line是否需要上述许可才能工作?@Harrish它应该可以工作,但这取决于设备,有些设备需要发送配对确认,有些设备假设一旦连接,就可以配对,而且此解决方案适用于android 4.4.2和android 5,因此,您可能应该为android 6及以上版本执行类似的操作。setPairingConfirmation方法需要BLUETOOTH_Priviledge权限,这对第三方应用程序不可用。但是有了abortBroadcast,我的Pixel 4 XL就开始工作了!这个问题的最佳答案。这是我的像素4 XL与安卓10的工作!如果我不调用abortBroadcast,则会弹出对话框,并在2秒钟后再次关闭。
override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(pairingReceiver)
    }
val pairingRequestFilter = IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST)          
pairingRequestFilter.priority = IntentFilter.SYSTEM_HIGH_PRIORITY - 1
            registerReceiver(pairingReceiver, pairingRequestFilter)
val filter = IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED)
        registerReceiver(receiver, filter)