diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/services/BLEComm.kt b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/services/BLEComm.kt index 8e5937238d..e46aff33d7 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/services/BLEComm.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/pump/danaRS/services/BLEComm.kt @@ -19,15 +19,13 @@ import info.nightscout.androidaps.plugins.pump.danaRS.activities.EnterPinActivit import info.nightscout.androidaps.plugins.pump.danaRS.activities.PairingHelperActivity import info.nightscout.androidaps.plugins.pump.danaRS.comm.DanaRSMessageHashTable import info.nightscout.androidaps.plugins.pump.danaRS.comm.DanaRS_Packet -import info.nightscout.androidaps.plugins.pump.danaRS.comm.DanaRS_Packet_Etc_Keep_Connection -import info.nightscout.androidaps.plugins.pump.danaRS.comm.DanaRS_Packet_General_Get_Pump_Check import info.nightscout.androidaps.plugins.pump.danaRS.encryption.BleEncryption import info.nightscout.androidaps.plugins.pump.danaRS.events.EventDanaRSPairingSuccess import info.nightscout.androidaps.utils.ToastUtils +import info.nightscout.androidaps.utils.extensions.notify +import info.nightscout.androidaps.utils.extensions.waitMillis import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.sharedPreferences.SP -import okhttp3.internal.notify -import okhttp3.internal.waitMillis import java.util.* import java.util.concurrent.ScheduledFuture import javax.inject.Inject @@ -230,7 +228,7 @@ class BLEComm @Inject internal constructor( //aapsLogger.debug("writeCharacteristic:" + DanaRS_Packet.toHexString(data)) bluetoothGatt?.writeCharacteristic(characteristic) }).start() - waitMillis(50) + SystemClock.sleep(50) } private val uartReadBTGattChar: BluetoothGattCharacteristic diff --git a/app/src/main/java/info/nightscout/androidaps/utils/CryptoUtil.kt b/app/src/main/java/info/nightscout/androidaps/utils/CryptoUtil.kt index 745f4449ee..ad271e7344 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/CryptoUtil.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/CryptoUtil.kt @@ -1,5 +1,6 @@ package info.nightscout.androidaps.utils +import info.nightscout.androidaps.utils.extensions.toHex import org.spongycastle.util.encoders.Base64 import java.nio.ByteBuffer import java.security.MessageDigest @@ -13,38 +14,6 @@ import javax.crypto.spec.GCMParameterSpec import javax.crypto.spec.PBEKeySpec import javax.crypto.spec.SecretKeySpec -private val HEX_CHARS = "0123456789abcdef" -private val HEX_CHARS_ARRAY = "0123456789abcdef".toCharArray() - -fun String.hexStringToByteArray() : ByteArray { - - val upperCased = this.toLowerCase() - val result = ByteArray(length / 2) - for (i in 0 until length step 2) { - val firstIndex = HEX_CHARS.indexOf(upperCased[i]); - val secondIndex = HEX_CHARS.indexOf(upperCased[i + 1]); - - val octet = firstIndex.shl(4).or(secondIndex) - result.set(i.shr(1), octet.toByte()) - } - - return result -} - -fun ByteArray.toHex() : String{ - val result = StringBuffer() - - forEach { - val octet = it.toInt() - val firstIndex = (octet and 0xF0).ushr(4) - val secondIndex = octet and 0x0F - result.append(HEX_CHARS_ARRAY[firstIndex]) - result.append(HEX_CHARS_ARRAY[secondIndex]) - } - - return result.toString() -} - object CryptoUtil { private const val IV_LENGTH_BYTE = 12 diff --git a/app/src/main/java/info/nightscout/androidaps/utils/extensions/Concurrency.kt b/app/src/main/java/info/nightscout/androidaps/utils/extensions/Concurrency.kt new file mode 100644 index 0000000000..b958a0ea24 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/utils/extensions/Concurrency.kt @@ -0,0 +1,22 @@ +package info.nightscout.androidaps.utils.extensions + + +@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "NOTHING_TO_INLINE") +inline fun Any.wait() = (this as Object).wait() + +/** + * Lock and wait a duration in milliseconds and nanos. + * Unlike [java.lang.Object.wait] this interprets 0 as "don't wait" instead of "wait forever". + */ +@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN") +fun Any.waitMillis(timeout: Long, nanos: Int = 0) { + if (timeout > 0L || nanos > 0) { + (this as Object).wait(timeout, nanos) + } +} + +@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "NOTHING_TO_INLINE") +inline fun Any.notify() = (this as Object).notify() + +@Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "NOTHING_TO_INLINE") +inline fun Any.notifyAll() = (this as Object).notifyAll()