Bluetooth LE 在暂停时断开连接

Bluetooth LE Disconnects on Pause

我希望我的蓝牙设备在应用程序不在主视图中时保持连接。每次我打开主页选项卡或其他应用程序时,它都会断开连接。如何在暂停时保持连接?相关代码如下:

@Override
    protected void onPause() {
        super.onPause();
        //Cancel any scans in progress
        mHandler.removeCallbacks(mStopRunnable);
        mHandler.removeCallbacks(mStartRunnable);
        mBluetoothAdapter.stopLeScan(this);
    }

    private Runnable mStopRunnable = new Runnable() {
        @Override
        public void run() {
            stopScan();
        }
    };

    private Runnable mStartRunnable = new Runnable() {
        @Override
        public void run() {
            startScan();
        }
    };

阅读更多 android 文档,我意识到我混淆了 onStop()onDestroy()。我的 onStop() 中有断开连接功能。删除后,尽管我关闭了 window.

,它仍保持连接状态