蓝牙配对随机删除

时间:2018-09-25 10:26:11

标签: android bluetooth android-6.0-marshmallow android-bluetooth

我正在维护一个在使用Zebra iMZ220蓝牙打印机的Android 6.0平板电脑上运行的应用程序。该应用程序要求将打印机与平板电脑配对。

打印机有时会从已经与其配对的平板电脑接收新的配对请求,最终结果是断开连接。

我无法确定其确切原因,因为日志中没有错误,并且它似乎是随机发生的。

我发现这些行应该与打印机配对:

Method method = device.getClass().getMethod("createBond", (Class[]) null);
method.invoke(device, (Object[]) null);

按顺序

String printerMac = settings.getString("printerMac","");
if (!stampante.isEmpty()) {
    BluetoothDevice device = bluetoothAdapter.getRemoteDevice(stampante);
    try {
        Method method = device.getClass().getMethod("createBond", (Class[]) null);
        method.invoke(device, (Object[]) null);
    } catch (Exception e) {
        e.printStackTrace();    
    }
}

由于某种原因,它是否可以消除打印机上的粘连?

我无法在Android 8设备上重现此错误。

1 个答案:

答案 0 :(得分:1)

根据我的经验(由于我也正在开发和维护连接到蓝牙打印机的应用程序),尽管确实很少,但有时设备和蓝牙设备之间的配对可能会丢失。这个错误会散布在各种设备和Android版本中。

我最终要做的是接受这种可能性,有时它会发生,并实现广播接收器以通知配对已丢失。那时,我再次以编程方式创建了绑定,并且还连接了打印机。

尝试创建绑定时,将出现一个默认对话框供用户输入打印机使用的PIN码,但是由于我知道PIN码,因此我会以编程方式输入此PIN码,因此对话框仅显示一秒钟,然后消失。

经过数月与这个问题的斗争,这最终成为我能找到的最优雅的解决方案。希望它对您也有帮助。

onCreate中创建过滤器并注册接收者:

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
        filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        filter.addAction(BluetoothDevice.ACTION_FOUND);
        filter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST);
        filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);

        registerReceiver(mReceiver, filter);

要与设备配对时调用的方法:

private void pairDevice(BluetoothDevice device) {
        try {
            //Log.d("AutoPairing", "Start Pairing... with: " + device.getName());
            device.createBond();
            Log.d("AutoPairing", "Pairing finished.");
        } catch (Exception e) {
            Log.e("AutoPairing", e.getMessage());
        }
    }

广播接收器:

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();

                if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {


                }else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {


                }else if (BluetoothDevice.ACTION_FOUND.equals(action)){


                }else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)){


                }else if (action.equals(BluetoothDevice.ACTION_PAIRING_REQUEST)) {

                        try {
                            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                                // If you want to auto_insert the pin, uncomment the following lines
                                //String PinNew = "HereIsThePinThePrinterUses";

                                //device.setPin(PinNew.getBytes());


                        } catch (Exception e) {
                            Log.e("AutoPairing", "Error occurs when trying to auto pair");
                            e.printStackTrace();
                        }

                }else  if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {



                        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

                        if (device.getBondState() == 12) {

                            //  Pairing was succesful. Do what you would normally do after that. Perhaps connect the printer now.

                        } else {
                            //  Pairing was unsuccesful. **This is also what get's triggered when the pairing is getting lost**. Let's pair the device then. 
                             pairDevice(device);                        

                        }  

                }

            }
        };

希望我不会忘记什么。

PM:所有那些空白的if statements都在这里,以备您也要连接打印机。如果您以其他方式执行此操作,则可以将其删除并从过滤器中删除这些操作。