Android BluetoothGatt - status 133 - register callback

前端 未结 6 695
庸人自扰
庸人自扰 2021-01-30 08:56

First of all I read SOLVED: GATT callback fails to register and took the steps suggested in that post to solve this issue with no success. The recommended fix in there if you ha

6条回答
  •  爱一瞬间的悲伤
    2021-01-30 09:21

    Somehow BluetoothLeScanner.startScan() helps

    private final Handler mMainHandler = new Handler(Looper.getMainLooper());
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        if (newState == BluetoothProfile.STATE_CONNECTED) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && mBluetoothAdapter.isEnabled() && mBleScanner != null) {
                mBleScanner.stopScan(mLeScanCallback);
            }
        } else if (status == 133 && newState == BluetoothProfile.STATE_DISCONNECTED) {
            if (D) Log.e(TAG, "connectGatt status == 133");
            mMainHandler.post(mDisconnectRunnable);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && mBluetoothAdapter.isEnabled()) {
                mBleScanner = mBluetoothAdapter.getBluetoothLeScanner();
                mBleScanner.startScan(null, new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build(),
                        mLeScanCallback);
            }
            mMainHandler.postDelayed(mConnectRunnable, 10000);
        }
    }
    
    private ScanCallback mLeScanCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            super.onScanResult(callbackType, result);
            if (result.getDevice().getAddress().equals(mBluetoothDeviceAddress)) {
                mMainHandler.post(mConnectRunnable);
                if (mBleScanner != null) {
                    mBleScanner.stopScan(mLeScanCallback);
                    mBleScanner = null;
                }
            }
        }
    };
    

提交回复
热议问题