簡體   English   中英

在BT適配器關閉時強制停止LE掃描會出現java.lang.IllegalStateException(IN NEXUS 5和Android 6)

[英]Force stopping LE Scan while BT Adapter is OFF thows java.lang.IllegalStateException (IN NEXUS 5 and Android 6)

禁用藍牙時遇到此錯誤。 用谷歌搜索,但沒有找到解決方案。 這是我的廣播接收器,當藍牙狀態更改時調用。

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            final String action = intent.getAction();
            Log.e(LOG_TAG, "Broadcast receiver - onReceive");
            if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
                h.removeCallbacks(runnable);
                final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
                switch (state) {
                    case BluetoothAdapter.STATE_OFF:
                        Log.i(LOG_TAG, "***** BLE-Bluetooth is disabled");
                        Toast.makeText(getApplicationContext(), "Bluetooth is disabled", Toast.LENGTH_LONG).show();
                        Log.i(TAG, "***** mBeaconScanner "+ mBeaconScanner);
                        if(null != mBeaconScanner)
                            mBeaconScanner.scanLeDevice(false);
                        break;
                    case BluetoothAdapter.STATE_ON:
                        Log.i(LOG_TAG, "***** BLE-Bluetooth is enabled");
                        Toast.makeText(getApplicationContext(), "Bluetooth is enabled", Toast.LENGTH_LONG).show();
                        if(null != mBeaconScanner)
                            mBeaconScanner.scanLeDevice(true);
                        break;
                }
            }
        }
    };

在上面的代碼中,我試圖在禁用藍牙的同時停止ble掃描。 在上面的代碼方法中,“ mBeaconScanner.scanLeDevice(false);” 重定向到:

Log.i(TAG, "***** Stopping BLE Scan for Android version " + Build.VERSION.SDK_INT);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

                if (bluetoothLeScanner != null)
                {

                        bluetoothLeScanner.stopScan(mScanCallback);

                }
            } else {
                mBluetoothAdapter.stopLeScan(mLeScanCallback);
            }

在上面的代碼片段中,調用bluetoothLeScanner.stopScan(mScanCallback)方法時出現異常。

注意:此問題並非在所有設備上都可以重現,但與聯系無關,對我來說也不是很常見。

如果有任何解決方案,請建議我。

我可以在這里建議的唯一解決方案是注釋此代碼:-

//mBeaconScanner.scanLeDevice(false);

BLE掃描的工作方式在無線電級別上非常復雜。 在達到正確的scan window睡眠時間等因素時,要考慮很多因素(例如功率,效率等)。
在這種特殊情況下,當您將BT適配器本身關閉時,就無線電而言,嘗試停止掃描是無效的操作。 很高興Android FW拋出此異常! 只要擺脫掉這段代碼,就可以進行一些內部應用程序特定的狀態轉換(如果需要的話),或者只是登錄並完成case BluetoothAdapter.STATE_OFF:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM