编程配对蓝牙设备,而无需用户输入PIN [英] Programmatically pair Bluetooth device without the user entering pin

查看:1308
本文介绍了编程配对蓝牙设备,而无需用户输入PIN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

蓝牙设备,我尝试连接始终相同的引脚code。这应该使人们有可能通过编程设置引脚配对的设备。

在试图寻找如何能够做到这一点,我结束了在$ C $低于C:

  BluetoothDevice类设备=的getDevice();

//为了避免弹出通知:
。de​​vice.getClass()实现getMethod(setPairingConfirmation,boolean.class).invoke(设备,真正的);
。de​​vice.getClass()实现getMethod(cancelPairingUserInput,boolean.class).invoke(设备,真正的);
byte []的引脚= ByteBuffer.allocate(4).putInt(1234).array();
// INT PINN = 1234;

//输入引脚编程方式:
方法毫秒= device.getClass()实现getMethod(setPin,byte []的类。)。
//方法毫秒= device.getClass()实现getMethod(setPasskey,int.class)。
ms.invoke(设备,针);

//联结设备:
方法毫米= device.getClass()实现getMethod(createBond,(等级[])为空)。
mm.invoke(设备,(对象[])NULL);
 

cancelPairingUserInput 给我一个 NoSuchMethodException ,这是奇怪的,因为该方法确实存在于 BluetoothDevice类类。

时的样子 Setpin SetPasskey 并没有做任何事情。该设备只是不会对。它只是对后手动输入PIN。

因此​​,code,它的工作原理只是行是:

  //联结设备:
方法毫米= device.getClass()实现getMethod(createBond,(等级[])为空)。
mm.invoke(设备,(对象[])NULL);
 

logcat的输出:

 十二月9号至27日:34:46.408:错误/应用程序(11671):cancelPairingUserInput [布尔]
        java.lang.NoSuchMethodException:cancelPairingUserInput [布尔]
        在java.lang.Class.getConstructorOrMethod(Class.java:460)
        在java.lang.Class.getMethod(Class.java:915)
        在test.app.bluetooth.model.BluetoothDiscoveryAndPairing.pair(BluetoothDiscoveryAndPairing.java:97)
        在test.app.bluetooth.model.BluetoothDiscoveryAndPairing.access$000(BluetoothDiscoveryAndPairing.java:25)
        在test.app.bluetooth.model.BluetoothDiscoveryAndPairing$1.onReceive(BluetoothDiscoveryAndPairing.java:79)
        在android.app.LoadedApk $ ReceiverDispatcher $ Args.run(LoadedApk.java:756)
        在android.os.Handler.handleCallback(Handler.java:615)
        在android.os.Handler.dispatchMessage(Handler.java:92)
        在android.os.Looper.loop(Looper.java:137)
        在android.app.ActivityThread.main(ActivityThread.java:4921)
        在java.lang.reflect.Method.invokeNative(本机方法)
        在java.lang.reflect.Method.invoke(Method.java:511)
        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1038)
        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
        在dalvik.system.NativeStart.main(本机方法)
 

那我做错了吗?

解决方案

隐藏方法cancelPairingUserInput不会在您的设备存在。不要使用它。

  1. 您应该注册的BroadcastReceiver的android.bluetooth.device.action.PAIRING_REQUEST
  2. 呼叫createBond()
  3. 在等待的BroadcastReceiver触发
  4. 在BroadcastReceiver的,如果动作是android.bluetooth.device.action.PAIRING_REQUEST 调用此方法

 公共无效setBluetoothPairingPin(BluetoothDevice类设备)
{
    byte []的pinBytes = convertPinToBytes(0000);
    尝试 {
          Log.d(TAG,尝试设置密码);
          方法M = device.getClass()实现getMethod(setPin,byte []的类。)。
          m.invoke(设备,pinBytes);
          Log.d(TAG,成功添加PIN。);
          尝试 {
                。de​​vice.getClass()实现getMethod(setPairingConfirmation,boolean.class).invoke(设备,真正的);
                Log.d(TAG,成功对setPairingConfirmation。);
            }赶上(例外五){
                // TODO自动生成的catch块
                Log.e(TAG,e.getMessage());
                e.printStackTrace();
            }
        }赶上(例外五){
          Log.e(TAG,e.getMessage());
          e.printStackTrace();
        }
}
 

它也可以与Android的果冻豆版本(4.1.2)的设备上。

The Bluetooth device I am trying to connect has always the same pincode. This should make it possible to pair the device by setting the pin programmatically.

After trying to search how this could be done, I ended up with the code below:

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 gives me a NoSuchMethodException, which is weird because the method does exist in BluetoothDevice class.

Is looks like Setpin or SetPasskey doesn't do anything. The device just wont pair. It only pairs after manually entering the pin.

So the only line of code that works is:

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

Logcat output:

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)

So what am I doing wrong?

解决方案

The hidden method cancelPairingUserInput does not exist in your device. Don't use it.

  1. You should register BroadcastReceiver for android.bluetooth.device.action.PAIRING_REQUEST
  2. Call createBond()
  3. Wait for BroadcastReceiver to trigger
  4. In BroadcastReceiver if action is android.bluetooth.device.action.PAIRING_REQUEST call this method

public void setBluetoothPairingPin(BluetoothDevice device)
{
    byte[] pinBytes = convertPinToBytes("0000");
    try {
          Log.d(TAG, "Try to set the PIN");
          Method m = device.getClass().getMethod("setPin", byte[].class);
          m.invoke(device, pinBytes);
          Log.d(TAG, "Success to add the PIN.");
          try {
                device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true);
                Log.d(TAG, "Success to setPairingConfirmation.");
            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.e(TAG, e.getMessage());
                e.printStackTrace();
            } 
        } catch (Exception e) {
          Log.e(TAG, e.getMessage());
          e.printStackTrace();
        }
}

It also works on a device with Jelly Bean version (4.1.2) of Android.

这篇关于编程配对蓝牙设备,而无需用户输入PIN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆