diff --git a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/device/OrangeLinkImpl.kt b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/device/OrangeLinkImpl.kt index 1568503a4e..b54cfb52ba 100644 --- a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/device/OrangeLinkImpl.kt +++ b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/ble/device/OrangeLinkImpl.kt @@ -1,7 +1,6 @@ package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.device import android.bluetooth.BluetoothAdapter -import android.bluetooth.BluetoothAdapter.LeScanCallback import android.bluetooth.BluetoothGattCharacteristic import android.bluetooth.le.BluetoothLeScanner import android.bluetooth.le.ScanCallback @@ -9,6 +8,7 @@ import android.bluetooth.le.ScanFilter import android.bluetooth.le.ScanResult import android.bluetooth.le.ScanSettings import android.os.Handler +import android.os.HandlerThread import android.os.Message import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag @@ -19,7 +19,6 @@ import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.ble.operation import info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service.RileyLinkServiceData import info.nightscout.androidaps.plugins.pump.common.utils.ByteUtil import info.nightscout.androidaps.utils.sharedPreferences.SP -import java.lang.Exception import java.util.* import javax.inject.Inject import javax.inject.Singleton @@ -34,7 +33,7 @@ class OrangeLinkImpl @Inject constructor( lateinit var rileyLinkBLE: RileyLinkBLE fun onCharacteristicChanged(characteristic: BluetoothGattCharacteristic) { - if (characteristic.uuid.toString().equals(GattAttributes.CHARA_NOTIFICATION_ORANGE)) { + if (characteristic.uuid.toString() == GattAttributes.CHARA_NOTIFICATION_ORANGE) { val data = characteristic.value val first = 0xff and data[0].toInt() aapsLogger.info(LTag.PUMPBTCOMM, @@ -44,7 +43,7 @@ class OrangeLinkImpl @Inject constructor( rileyLinkServiceData.versionOrangeFirmware = fv rileyLinkServiceData.versionOrangeHardware = hv - aapsLogger.info(LTag.PUMPBTCOMM, "OrangeLink: Firmware: ${fv}, Hardware: ${hv}") + aapsLogger.info(LTag.PUMPBTCOMM, "OrangeLink: Firmware: ${fv}, Hardware: $hv") } } @@ -56,7 +55,7 @@ class OrangeLinkImpl @Inject constructor( } /** - * We are checking if this is special Orange (with ORANGE_NOTIFICTION_SERVICE) + * We are checking if this is special Orange (with ORANGE_NOTIFICATION_SERVICE) */ fun checkIsOrange(uuidService: UUID) { if (GattAttributes.isOrange(uuidService)) { @@ -99,7 +98,7 @@ class OrangeLinkImpl @Inject constructor( fun startScan() { try { stopScan() - val bluetoothAdapter = rileyLinkBLE.getBluetoothAdapter() + val bluetoothAdapter = rileyLinkBLE.bluetoothAdapter aapsLogger.debug(LTag.PUMPBTCOMM, "startScan") handler.sendEmptyMessageDelayed(TIME_OUT_WHAT, TIME_OUT.toLong()) val bluetoothLeScanner: BluetoothLeScanner = bluetoothAdapter.bluetoothLeScanner @@ -114,7 +113,7 @@ class OrangeLinkImpl @Inject constructor( } } - var scanCallback: ScanCallback = object : ScanCallback() { + private val scanCallback: ScanCallback = object : ScanCallback() { override fun onScanResult(callbackType: Int, result: ScanResult) { super.onScanResult(callbackType, result) //val name = result.device.name @@ -136,7 +135,7 @@ class OrangeLinkImpl @Inject constructor( } } - +/* private val mLeScanCallback = LeScanCallback { device, _, _ -> if (rileyLinkServiceData.rileyLinkAddress.equals(device.address)) { stopScan() @@ -144,11 +143,8 @@ class OrangeLinkImpl @Inject constructor( rileyLinkBLE.connectGattInternal() } } - - val TIME_OUT = 90 * 1000 - val TIME_OUT_WHAT = 0x12 - - var handler: Handler = object : Handler() { +*/ + private val handler: Handler = object : Handler(HandlerThread(OrangeLinkImpl::class.java.simpleName + "Handler").also { it.start() }.looper) { override fun handleMessage(msg: Message) { super.handleMessage(msg) when (msg.what) { @@ -160,10 +156,10 @@ class OrangeLinkImpl @Inject constructor( fun stopScan() { handler.removeMessages(TIME_OUT_WHAT) - val bluetoothAdapter = rileyLinkBLE.getBluetoothAdapter() ?: return + val bluetoothAdapter = rileyLinkBLE.bluetoothAdapter ?: return try { - val bluetoothLeScanner: BluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner() + val bluetoothLeScanner: BluetoothLeScanner = bluetoothAdapter.bluetoothLeScanner if (isBluetoothAvailable()) { bluetoothLeScanner.stopScan(scanCallback) @@ -185,11 +181,15 @@ class OrangeLinkImpl @Inject constructor( } } - fun isBluetoothAvailable(): Boolean { - val bluetoothAdapter = rileyLinkBLE.getBluetoothAdapter() + private fun isBluetoothAvailable(): Boolean { + val bluetoothAdapter = rileyLinkBLE.bluetoothAdapter return bluetoothAdapter != null && - bluetoothAdapter.isEnabled() && - bluetoothAdapter.getState() == BluetoothAdapter.STATE_ON + bluetoothAdapter.isEnabled && + bluetoothAdapter.state == BluetoothAdapter.STATE_ON } + companion object { + const val TIME_OUT = 90 * 1000 + const val TIME_OUT_WHAT = 0x12 + } } \ No newline at end of file diff --git a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkServiceData.java b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkServiceData.java index 0159bd945e..2e09c92253 100644 --- a/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkServiceData.java +++ b/rileylink/src/main/java/info/nightscout/androidaps/plugins/pump/common/hw/rileylink/service/RileyLinkServiceData.java @@ -2,6 +2,7 @@ package info.nightscout.androidaps.plugins.pump.common.hw.rileylink.service; import java.util.Locale; +import javax.annotation.Nullable; import javax.inject.Inject; import javax.inject.Singleton; @@ -37,8 +38,8 @@ public class RileyLinkServiceData { private long lastServiceStateChange = 0L; public RileyLinkFirmwareVersion firmwareVersion; // here we have "compatibility level" version public RileyLinkTargetFrequency rileyLinkTargetFrequency; - public String rileyLinkAddress; - public String rileyLinkName; + @Nullable public String rileyLinkAddress; + @Nullable public String rileyLinkName; public Integer batteryLevel; public boolean showBatteryLevel = false; long lastTuneUpTime = 0L;