简体   繁体   English

如何在Android中断开BLE设备

[英]How to disconnect a ble device in android

I am trying to disconnect a Bluetooth Low Energy (BLE) device.My code works fine in lollipop and lower versions and connecting again to the BLE device also works fine. 我正在尝试断开蓝牙低功耗(BLE)设备。我的代码在棒棒糖及更低版本中工作正常,再次连接到BLE设备也工作正常。 However, when I disconnect in nougat it disconnects the BLE device, but connecting again fails and shows the error "app is not responding". 但是,当我在牛轧糖中断开连接时,它将断开BLE设备的连接,但是再次连接失败并显示错误“应用程序未响应”。 This is my code. 这是我的代码。 I am trying to disconnect onBackPressed() method. 我试图断开onBackPressed()方法。

 public void onBackPressed() {

        AlertDialog.Builder adb = new AlertDialog.Builder(ActivityName.this);
        adb.setTitle("DISCONNECT");
        adb.setCancelable(true);
        adb.setMessage("Do you want to disconnect");
        adb.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mBTLE_Service.disconnect();
                dialog.dismiss();
            }
        });
        adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        AlertDialog alertDialog = adb.create();
        alertDialog.show();
    }

Try mBTLE_Service.close(); 尝试mBTLE_Service.close(); instead of mBTLE_Service.disconnect(); 而不是mBTLE_Service.disconnect();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM