Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 以编程方式配对蓝牙设备,而无需用户输入pin_Java_Android_Bluetooth - Fatal编程技术网

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

Java 以编程方式配对蓝牙设备,而无需用户输入pin,java,android,bluetooth,Java,Android,Bluetooth,我尝试连接的蓝牙设备始终具有相同的pincode。这样就可以通过编程方式设置管脚来配对设备 在尝试搜索如何实现这一点后,我得到了以下代码: BluetoothDevice device = getDevice(); //To avoid the popup notification: device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true); device.getClass

我尝试连接的蓝牙设备始终具有相同的pincode。这样就可以通过编程方式设置管脚来配对设备

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

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
类中

它看起来像是
Setpin
SetPasskey
不起任何作用。这个设备无法配对。它仅在手动输入pin后配对

因此,唯一有效的代码行是:

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

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)

那么我做错了什么呢?

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

  • 您应该为android.bluetooth.device.action.PAIRING\u请求注册BroadcastReceiver
  • 调用createBond()
  • 等待广播接收器触发
  • 在BroadcastReceiver中,如果操作是android.bluetooth.device.action.PAIRING\u请求 调用此方法
  • 它也适用于Android的Jelly Bean版本(4.1.2)的设备。

    这对我来说很有用:

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

    @DuncanJones我在startpost中添加了stacktrace。这可以在19之前的API级别完成吗?android.bluetooth.device.action.PAIRING_请求仅在API 19中引入。为什么需要setPairingConfirmation()?Android文档说它只适用于配对、变体、密钥确认,而不适用于传统配对。此外,在Android6上,它需要BLUETOOTH_特权,以防任何人无法访问convertPinToBytes。源代码可在此处找到:这不起作用,因为它需要BLUETOOTH_特权权限,这仅在系统应用程序上可用。是否只有尝试配对的设备才需要此代码?我们不需要其他配对设备上的任何代码吗?您不需要其他设备上的任何代码。你应该接受这个连接。(我为连接到移动打印机的android手机编写了此代码。)
        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();
    
    }