package com.polidea.rxandroidble2.internal.util;

import android.annotation.TargetApi;
import android.app.PendingIntent;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanFilter;
import android.bluetooth.le.ScanSettings;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.polidea.rxandroidble2.internal.RxBleLog;
import java.util.List;
import java.util.Set;

import bleshadow.javax.inject.Inject;

public class RxBleAdapterWrapper {

    private final BluetoothAdapter bluetoothAdapter;

    @Inject
    public RxBleAdapterWrapper(@Nullable BluetoothAdapter bluetoothAdapter) {
        this.bluetoothAdapter = bluetoothAdapter;
    }

    public BluetoothDevice getRemoteDevice(String macAddress) {
        return bluetoothAdapter.getRemoteDevice(macAddress);
    }

    public boolean hasBluetoothAdapter() {
        return bluetoothAdapter != null;
    }

    public boolean isBluetoothEnabled() {
        return bluetoothAdapter != null && bluetoothAdapter.isEnabled();
    }

    @SuppressWarnings("deprecation")
    public boolean startLegacyLeScan(BluetoothAdapter.LeScanCallback leScanCallback) {
        return bluetoothAdapter.startLeScan(leScanCallback);
    }

    @SuppressWarnings("deprecation")
    public void stopLegacyLeScan(BluetoothAdapter.LeScanCallback leScanCallback) {
        bluetoothAdapter.stopLeScan(leScanCallback);
    }

    @TargetApi(21 /* Build.VERSION_CODES.LOLLIPOP */)
    public void startLeScan(List<ScanFilter> scanFilters, ScanSettings scanSettings, ScanCallback scanCallback) {
        bluetoothAdapter.getBluetoothLeScanner().startScan(scanFilters, scanSettings, scanCallback);
    }

    @RequiresApi(26 /* Build.VERSION_CODES.O */)
    public int startLeScan(List<ScanFilter> scanFilters, ScanSettings scanSettings, PendingIntent callbackIntent) {
        return bluetoothAdapter.getBluetoothLeScanner().startScan(scanFilters, scanSettings, callbackIntent);
    }

    @RequiresApi(26 /* Build.VERSION_CODES.O */)
    public void stopLeScan(PendingIntent callbackIntent) {
        bluetoothAdapter.getBluetoothLeScanner().stopScan(callbackIntent);
    }

    @TargetApi(21 /* Build.VERSION_CODES.LOLLIPOP */)
    public void stopLeScan(ScanCallback scanCallback) {
        if (!bluetoothAdapter.isEnabled()) {
            // this situation seems to be a problem since API 29
            RxBleLog.v(
                    "BluetoothAdapter is disabled, calling BluetoothLeScanner.stopScan(ScanCallback) may cause IllegalStateException"
            );
            // if stopping the scan is not possible due to BluetoothAdapter turned off then it is probably stopped anyway
            return;
        }
        final BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
        if (bluetoothLeScanner == null) {
            RxBleLog.w(
                    "Cannot call BluetoothLeScanner.stopScan(ScanCallback) on 'null' reference; BluetoothAdapter.isEnabled() == %b",
                    bluetoothAdapter.isEnabled()
            );
            // if stopping the scan is not possible due to BluetoothLeScanner not accessible then it is probably stopped anyway
            // this should not happen since the check for BluetoothAdapter.isEnabled() has been added above. This situation was only
            // observed when the adapter was disabled
            return;
        }
        bluetoothLeScanner.stopScan(scanCallback);
    }

    public Set<BluetoothDevice> getBondedDevices() {
        return bluetoothAdapter.getBondedDevices();
    }
}

@@ -883,24 +883,99 @@
 cAddress) %7B%0A
+        if (bluetoothAdapter == null) %7B%0A            return null;%0A        %7D%0A
         retu
@@ -1023,16 +1023,16 @@
 dress);%0A
-
     %7D%0A%0A 
@@ -1359,32 +1359,108 @@
 ScanCallback) %7B%0A
+        if (bluetoothAdapter == null) %7B%0A            return false;%0A        %7D%0A
         return b
@@ -1623,32 +1623,102 @@
 ScanCallback) %7B%0A
+        if (bluetoothAdapter == null) %7B%0A            return;%0A        %7D%0A
         bluetoot
@@ -1921,32 +1921,102 @@
 scanCallback) %7B%0A
+        if (bluetoothAdapter == null) %7B%0A            return;%0A        %7D%0A
         bluetoot
@@ -2264,32 +2264,142 @@
 llbackIntent) %7B%0A
+        if (bluetoothAdapter == null) %7B%0A            return ScanCallback.SCAN_FAILED_INTERNAL_ERROR;%0A        %7D%0A
         return b
@@ -2599,32 +2599,102 @@
 llbackIntent) %7B%0A
+        if (bluetoothAdapter == null) %7B%0A            return;%0A        %7D%0A
         bluetoot
@@ -2861,32 +2861,102 @@
 scanCallback) %7B%0A
+        if (bluetoothAdapter == null) %7B%0A            return;%0A        %7D%0A
         if (!blu
@@ -4128,32 +4128,32 @@
 llback);%0A    %7D%0A%0A
-
     public Set%3CB
@@ -4185,24 +4185,99 @@
 Devices() %7B%0A
+        if (bluetoothAdapter == null) %7B%0A            return null;%0A        %7D%0A
         retu

Add checks for null bluetoothAdaptor