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
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;
}
}
}
};