首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >以编程方式配对蓝牙设备,而无需用户输入pin

以编程方式配对蓝牙设备,而无需用户输入pin
EN

Stack Overflow用户
提问于 2013-09-27 18:01:23
回答 1查看 30.9K关注 0票数 20

我尝试连接的蓝牙设备始终具有相同的PIN码。这应该可以通过以编程方式设置引脚来配对器件。

在尝试搜索如何做到这一点后,我得到了以下代码:

代码语言:javascript
复制
BluetoothDevice device = getDevice();

//To avoid the popup notification:
device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true);
device.getClass().getMethod("cancelPairingUserInput", boolean.class).invoke(device, true);
byte[] pin = ByteBuffer.allocate(4).putInt(1234).array();
//int pinn = 1234;

//Entering pin programmatically:  
Method ms = device.getClass().getMethod("setPin", byte[].class);
//Method ms = device.getClass().getMethod("setPasskey", int.class);
ms.invoke(device, pin);

//Bonding the device:
Method mm = device.getClass().getMethod("createBond", (Class[]) null);
mm.invoke(device, (Object[]) null);

cancelPairingUserInput给了我一个NoSuchMethodException,这很奇怪,因为这个方法确实存在于BluetoothDevice类中。

看起来SetpinSetPasskey什么都不做。这个设备就是不能配对。它仅在手动输入引脚后才会配对。

因此,唯一可以工作的代码行是:

代码语言:javascript
复制
//Bonding the device:
Method mm = device.getClass().getMethod("createBond", (Class[]) null);
mm.invoke(device, (Object[]) null);

Logcat输出:

代码语言:javascript
复制
09-27 12:34:46.408: ERROR/App(11671): cancelPairingUserInput [boolean]
        java.lang.NoSuchMethodException: cancelPairingUserInput [boolean]
        at java.lang.Class.getConstructorOrMethod(Class.java:460)
        at java.lang.Class.getMethod(Class.java:915)
        at test.app.bluetooth.model.BluetoothDiscoveryAndPairing.pair(BluetoothDiscoveryAndPairing.java:97)
        at test.app.bluetooth.model.BluetoothDiscoveryAndPairing.access$000(BluetoothDiscoveryAndPairing.java:25)
        at test.app.bluetooth.model.BluetoothDiscoveryAndPairing$1.onReceive(BluetoothDiscoveryAndPairing.java:79)
        at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:756)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4921)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
        at dalvik.system.NativeStart.main(Native Method)

那么我到底做错了什么呢?

EN

回答 1

Stack Overflow用户

发布于 2015-09-04 00:36:03

这对我很有效:

代码语言:javascript
复制
    IntentFilter filter2 = new IntentFilter(
            "android.bluetooth.device.action.PAIRING_REQUEST");
    mActivity.registerReceiver(
            pairingRequest, filter2);

private final BroadcastReceiver pairingRequest = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals("android.bluetooth.device.action.PAIRING_REQUEST")) {
            mBluetoothDevice = needed;
                try {
                    byte[] pin = (byte[]) BluetoothDevice.class.getMethod("convertPinToBytes", String.class).invoke(BluetoothDevice.class, "1234");
                    Method m = mBluetoothDevice.getClass().getMethod("setPin", byte[].class);
                    m.invoke(mBluetoothDevice, pin);
                    mBluetoothDevice.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(mBluetoothDevice, true);
}
    catch(Exception e)
{

    e.printStackTrace();

}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19047995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档