From 6b2c9fb48a8fa3577ce1c0cf71d35aaf57a37f7d Mon Sep 17 00:00:00 2001 From: Arie Trouw Date: Sat, 29 Dec 2018 11:00:00 -0800 Subject: [PATCH 1/2] Base Changes --- .../java/network/xyo/ble/CallByVersion.kt | 2 +- .../xyo/ble/devices/XY2BluetoothDevice.kt | 2 +- .../xyo/ble/devices/XY3BluetoothDevice.kt | 22 ++-- .../xyo/ble/devices/XY4BluetoothDevice.kt | 28 ++--- .../xyo/ble/devices/XYBluetoothDevice.kt | 14 +-- .../ble/devices/XYFinderBluetoothDevice.kt | 20 +-- .../xyo/ble/devices/XYGpsBluetoothDevice.kt | 8 +- .../ble/devices/XYIBeaconBluetoothDevice.kt | 2 +- .../java/network/xyo/ble/firmware/OtaFile.kt | 9 +- .../network/xyo/ble/firmware/OtaUpdate.kt | 36 +++--- .../network/xyo/ble/gatt/XYBluetoothError.kt | 6 +- .../network/xyo/ble/gatt/XYBluetoothGatt.kt | 116 +++++++++--------- .../xyo/ble/gatt/XYBluetoothGattClient.kt | 60 ++++----- .../ble/gatt/server/XYBluetoothGattServer.kt | 24 ++-- .../xyo/ble/scanner/XYScanResultManual.kt | 4 +- .../network/xyo/ble/scanner/XYSmartScan.kt | 16 +-- .../xyo/ble/scanner/XYSmartScanModern.kt | 18 +-- .../java/network/xyo/ble/services/Service.kt | 2 +- sdk-android-sample/build.gradle | 1 + .../network/xyo/ble/sample/XYApplication.kt | 4 +- .../sample/activities/XYOBleSampleActivity.kt | 32 +++-- .../activities/XYOFinderDeviceActivity.kt | 12 +- .../fragments/FirmwareUpdateFragment.kt | 20 ++- .../xyo/ble/sample/fragments/InfoFragment.kt | 35 +++--- .../sample/fragments/XYAppBaseFragmentt.kt | 7 +- .../xyo/ble/sample/views/XYBLEStatsView.kt | 8 +- .../xyo/ble/sample/views/XYDeviceItemView.kt | 6 +- 27 files changed, 262 insertions(+), 252 deletions(-) diff --git a/sdk-android-library/src/main/java/network/xyo/ble/CallByVersion.kt b/sdk-android-library/src/main/java/network/xyo/ble/CallByVersion.kt index f26db1ed..dee8c927 100644 --- a/sdk-android-library/src/main/java/network/xyo/ble/CallByVersion.kt +++ b/sdk-android-library/src/main/java/network/xyo/ble/CallByVersion.kt @@ -28,6 +28,6 @@ class CallByVersion : XYBase() { return } } - logError("No Call for OS Version Found", true) + log.error("No Call for OS Version Found", true) } } \ No newline at end of file diff --git a/sdk-android-library/src/main/java/network/xyo/ble/devices/XY2BluetoothDevice.kt b/sdk-android-library/src/main/java/network/xyo/ble/devices/XY2BluetoothDevice.kt index 2f49bbfc..a687e83a 100644 --- a/sdk-android-library/src/main/java/network/xyo/ble/devices/XY2BluetoothDevice.kt +++ b/sdk-android-library/src/main/java/network/xyo/ble/devices/XY2BluetoothDevice.kt @@ -27,7 +27,7 @@ open class XY2BluetoothDevice(context: Context, scanResult: XYScanResult, hash: val sensorService = SensorService(this) override fun find(): Deferred> { - logInfo("find") + log.info("find") return controlService.buzzerSelect.set(2) } diff --git a/sdk-android-library/src/main/java/network/xyo/ble/devices/XY3BluetoothDevice.kt b/sdk-android-library/src/main/java/network/xyo/ble/devices/XY3BluetoothDevice.kt index a27cd960..b4093af2 100644 --- a/sdk-android-library/src/main/java/network/xyo/ble/devices/XY3BluetoothDevice.kt +++ b/sdk-android-library/src/main/java/network/xyo/ble/devices/XY3BluetoothDevice.kt @@ -43,7 +43,7 @@ open class XY3BluetoothDevice(context: Context, scanResult: XYScanResult, hash: private val buttonListener = object : XYBluetoothGattCallback() { override fun onCharacteristicChanged(gatt: BluetoothGatt?, characteristic: BluetoothGattCharacteristic?) { - logInfo("onCharacteristicChanged") + log.info("onCharacteristicChanged") super.onCharacteristicChanged(gatt, characteristic) if (characteristic?.uuid == controlService.button.uuid) { reportButtonPressed(buttonPressFromInt(characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0))) @@ -64,27 +64,27 @@ open class XY3BluetoothDevice(context: Context, scanResult: XYScanResult, hash: override val prefix = "xy:ibeacon" override fun find(): Deferred> { - logInfo("find") + log.info("find") return controlService.buzzerSelect.set(2) } override fun lock(): Deferred> { - logInfo("lock") + log.info("lock") return basicConfigService.lock.set(DEFAULT_LOCK_CODE) } override fun unlock(): Deferred> { - logInfo("unlock") + log.info("unlock") return basicConfigService.unlock.set(DEFAULT_LOCK_CODE) } override fun stayAwake(): Deferred> { - logInfo("stayAwake") + log.info("stayAwake") return extendedConfigService.registration.set(1) } override fun fallAsleep(): Deferred> { - logInfo("fallAsleep") + log.info("fallAsleep") return extendedConfigService.registration.set(0) } @@ -93,7 +93,7 @@ open class XY3BluetoothDevice(context: Context, scanResult: XYScanResult, hash: if (scanResult != null) { if (pressFromScanResult(scanResult)) { if (now - lastButtonPressTime > BUTTON_ADVERTISEMENT_LENGTH) { - logInfo("onDetect: pressFromScanResult: first") + log.info("onDetect: pressFromScanResult: first") reportButtonPressed(ButtonPress.Single) lastButtonPressTime = now } @@ -102,9 +102,9 @@ open class XY3BluetoothDevice(context: Context, scanResult: XYScanResult, hash: } private fun enableButtonNotifyIfConnected() { - logInfo("enableButtonNotifyIfConnected") + log.info("enableButtonNotifyIfConnected") if (connectionState == ConnectionState.Connected) { - logInfo("enableButtonNotifyIfConnected: Connected") + log.info("enableButtonNotifyIfConnected: Connected") controlService.button.enableNotify(true) } } @@ -187,13 +187,13 @@ open class XY3BluetoothDevice(context: Context, scanResult: XYScanResult, hash: } fun reportGlobalButtonPressed(device: XY3BluetoothDevice, state: ButtonPress) { - logInfo("reportButtonPressed (Global)") + log.info("reportButtonPressed (Global)") GlobalScope.launch { synchronized(globalListeners) { for (listener in globalListeners) { val xyFinderListener = listener.value as? XYFinderBluetoothDevice.Listener if (xyFinderListener != null) { - logInfo("reportButtonPressed: $xyFinderListener") + log.info("reportButtonPressed: $xyFinderListener") GlobalScope.launch { when (state) { ButtonPress.Single -> xyFinderListener.buttonSinglePressed(device) diff --git a/sdk-android-library/src/main/java/network/xyo/ble/devices/XY4BluetoothDevice.kt b/sdk-android-library/src/main/java/network/xyo/ble/devices/XY4BluetoothDevice.kt index 76a0b574..27e4a704 100644 --- a/sdk-android-library/src/main/java/network/xyo/ble/devices/XY4BluetoothDevice.kt +++ b/sdk-android-library/src/main/java/network/xyo/ble/devices/XY4BluetoothDevice.kt @@ -45,7 +45,7 @@ open class XY4BluetoothDevice(context: Context, scanResult: XYScanResult, hash: private val buttonListener = object : XYBluetoothGattCallback() { override fun onCharacteristicChanged(gatt: BluetoothGatt?, characteristic: BluetoothGattCharacteristic?) { - logInfo("onCharacteristicChanged") + log.info("onCharacteristicChanged") super.onCharacteristicChanged(gatt, characteristic) if (characteristic?.uuid == primary.buttonState.uuid) { reportButtonPressed(buttonPressFromInt(characteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0))) @@ -62,32 +62,32 @@ open class XY4BluetoothDevice(context: Context, scanResult: XYScanResult, hash: override val prefix = "xy:ibeacon" override fun find(): Deferred> { - logInfo("find") + log.info("find") return primary.buzzer.set(11) } override fun lock(): Deferred> { - logInfo("lock") + log.info("lock") return primary.lock.set(DefaultLockCode) } override fun unlock(): Deferred> { - logInfo("unlock") + log.info("unlock") return primary.unlock.set(DefaultLockCode) } override fun stayAwake(): Deferred> { - logInfo("stayAwake") + log.info("stayAwake") return primary.stayAwake.set(1) } override fun fallAsleep(): Deferred> { - logInfo("fallAsleep") + log.info("fallAsleep") return primary.stayAwake.set(0) } override fun batteryLevel(): Deferred> { - logInfo("batteryLevel") + log.info("batteryLevel") return batteryService.level.get() } @@ -95,9 +95,9 @@ open class XY4BluetoothDevice(context: Context, scanResult: XYScanResult, hash: super.onDetect(scanResult) if (scanResult != null) { if (pressFromScanResult(scanResult)) { - logInfo("onDetect: pressFromScanResult: true") + log.info("onDetect: pressFromScanResult: true") if (now - lastButtonPressTime > BUTTON_ADVERTISEMENT_LENGTH) { - logInfo("onDetect: pressFromScanResult: first") + log.info("onDetect: pressFromScanResult: first") reportButtonPressed(ButtonPress.Single) lastButtonPressTime = now } @@ -128,15 +128,15 @@ open class XY4BluetoothDevice(context: Context, scanResult: XYScanResult, hash: private fun enableButtonNotifyIfConnected() { - logInfo("enableButtonNotifyIfConnected") + log.info("enableButtonNotifyIfConnected") if (connectionState == ConnectionState.Connected) { - logInfo("enableButtonNotifyIfConnected: Connected") + log.info("enableButtonNotifyIfConnected: Connected") primary.buttonState.enableNotify(true) } } override fun onConnectionStateChange(newState: Int) { - logInfo("onConnectionStateChange") + log.info("onConnectionStateChange") super.onConnectionStateChange(newState) enableButtonNotifyIfConnected() } @@ -198,13 +198,13 @@ open class XY4BluetoothDevice(context: Context, scanResult: XYScanResult, hash: } fun reportGlobalButtonPressed(device: XY4BluetoothDevice, state: ButtonPress) { - logInfo("reportButtonPressed (Global)") + log.info("reportButtonPressed (Global)") GlobalScope.launch { synchronized(globalListeners) { for (listener in globalListeners) { val xyFinderListener = listener.value as? XYFinderBluetoothDevice.Listener if (xyFinderListener != null) { - logInfo("reportButtonPressed: $xyFinderListener") + log.info("reportButtonPressed: $xyFinderListener") GlobalScope.launch { when (state) { ButtonPress.Single -> xyFinderListener.buttonSinglePressed(device) diff --git a/sdk-android-library/src/main/java/network/xyo/ble/devices/XYBluetoothDevice.kt b/sdk-android-library/src/main/java/network/xyo/ble/devices/XYBluetoothDevice.kt index 082200a6..abacc37e 100644 --- a/sdk-android-library/src/main/java/network/xyo/ble/devices/XYBluetoothDevice.kt +++ b/sdk-android-library/src/main/java/network/xyo/ble/devices/XYBluetoothDevice.kt @@ -82,7 +82,7 @@ open class XYBluetoothDevice(context: Context, device: BluetoothDevice?, private checkingForExit = true GlobalScope.launch { while (checkingForExit) { - //logInfo("checkForExit: $id : $rssi : $now : $outOfRangeDelay : $lastAdTime : $lastAccessTime") + //log.info("checkForExit: $id : $rssi : $now : $outOfRangeDelay : $lastAdTime : $lastAccessTime") delay(outOfRangeDelay) //check if something else has already marked it as exited @@ -108,7 +108,7 @@ open class XYBluetoothDevice(context: Context, device: BluetoothDevice?, private } internal open fun onEnter() { - logInfo("onEnter: $address") + log.info("onEnter: $address") enterCount++ lastAdTime = now synchronized(listeners) { @@ -122,7 +122,7 @@ open class XYBluetoothDevice(context: Context, device: BluetoothDevice?, private } internal open fun onExit() { - logInfo("onExit: $address") + log.info("onExit: $address") exitCount++ synchronized(listeners) { for ((_, listener) in listeners) { @@ -161,11 +161,11 @@ open class XYBluetoothDevice(context: Context, device: BluetoothDevice?, private } override fun onConnectionStateChange(newState: Int) { - logInfo("onConnectionStateChange: $id : $newState: $listeners.size") + log.info("onConnectionStateChange: $id : $newState: $listeners.size") synchronized(listeners) { for ((tag, listener) in listeners) { GlobalScope.launch { - logInfo("connectionStateChanged: $tag : $newState") + log.info("connectionStateChanged: $tag : $newState") listener.connectionStateChanged(this@XYBluetoothDevice, newState) if (newState == BluetoothGatt.STATE_CONNECTED) { lastAccessTime = now @@ -191,7 +191,7 @@ open class XYBluetoothDevice(context: Context, device: BluetoothDevice?, private } fun addListener(key: String, listener: Listener) { - logInfo("addListener:$key:$listener") + log.info("addListener:$key:$listener") GlobalScope.launch { synchronized(listeners) { listeners.put(key, listener) @@ -200,7 +200,7 @@ open class XYBluetoothDevice(context: Context, device: BluetoothDevice?, private } fun removeListener(key: String) { - logInfo("removeListener:$key") + log.info("removeListener:$key") GlobalScope.launch { synchronized(listeners) { listeners.remove(key) diff --git a/sdk-android-library/src/main/java/network/xyo/ble/devices/XYFinderBluetoothDevice.kt b/sdk-android-library/src/main/java/network/xyo/ble/devices/XYFinderBluetoothDevice.kt index 37d7452f..10046e58 100644 --- a/sdk-android-library/src/main/java/network/xyo/ble/devices/XYFinderBluetoothDevice.kt +++ b/sdk-android-library/src/main/java/network/xyo/ble/devices/XYFinderBluetoothDevice.kt @@ -127,7 +127,7 @@ open class XYFinderBluetoothDevice(context: Context, scanResult: XYScanResult, h //signal the user to where it is, usually make it beep open fun find(): Deferred> { - logError(UnsupportedOperationException().toString(), true) + log.error(UnsupportedOperationException().toString(), true) return asyncBle { return@asyncBle XYBluetoothResult(XYBluetoothError("Not Implemented")) } @@ -135,49 +135,49 @@ open class XYFinderBluetoothDevice(context: Context, scanResult: XYScanResult, h //turn off finding, if supported open fun stopFind(): Deferred> { - logError(UnsupportedOperationException(), true) + log.error(UnsupportedOperationException(), true) return asyncBle { return@asyncBle XYBluetoothResult(XYBluetoothError("Not Implemented")) } } open fun lock(): Deferred> { - logError(UnsupportedOperationException(), true) + log.error(UnsupportedOperationException(), true) return asyncBle { return@asyncBle XYBluetoothResult(XYBluetoothError("Not Implemented")) } } open fun unlock(): Deferred> { - logError(UnsupportedOperationException(), true) + log.error(UnsupportedOperationException(), true) return asyncBle { return@asyncBle XYBluetoothResult(XYBluetoothError("Not Implemented")) } } open fun stayAwake(): Deferred> { - logError(UnsupportedOperationException(), true) + log.error(UnsupportedOperationException(), true) return asyncBle { return@asyncBle XYBluetoothResult(XYBluetoothError("Not Implemented")) } } open fun fallAsleep(): Deferred> { - logError(UnsupportedOperationException(), true) + log.error(UnsupportedOperationException(), true) return asyncBle { return@asyncBle XYBluetoothResult(XYBluetoothError("Not Implemented")) } } open fun restart(): Deferred> { - logError(UnsupportedOperationException(), true) + log.error(UnsupportedOperationException(), true) return asyncBle { return@asyncBle XYBluetoothResult(XYBluetoothError("Not Implemented")) } } open fun batteryLevel(): Deferred> { - logError(UnsupportedOperationException(), true) + log.error(UnsupportedOperationException(), true) return asyncBle { return@asyncBle XYBluetoothResult(XYBluetoothError("Not Implemented")) } @@ -210,13 +210,13 @@ open class XYFinderBluetoothDevice(context: Context, scanResult: XYScanResult, h } internal open fun reportButtonPressed(state: ButtonPress) { - logInfo("reportButtonPressed") + log.info("reportButtonPressed") GlobalScope.launch { synchronized(listeners) { for (listener in listeners) { val xyFinderListener = listener.value as? Listener if (xyFinderListener != null) { - logInfo("reportButtonPressed: $xyFinderListener") + log.info("reportButtonPressed: $xyFinderListener") GlobalScope.launch { when (state) { ButtonPress.Single -> xyFinderListener.buttonSinglePressed(this@XYFinderBluetoothDevice) diff --git a/sdk-android-library/src/main/java/network/xyo/ble/devices/XYGpsBluetoothDevice.kt b/sdk-android-library/src/main/java/network/xyo/ble/devices/XYGpsBluetoothDevice.kt index 4158cc2b..b3fabe83 100644 --- a/sdk-android-library/src/main/java/network/xyo/ble/devices/XYGpsBluetoothDevice.kt +++ b/sdk-android-library/src/main/java/network/xyo/ble/devices/XYGpsBluetoothDevice.kt @@ -52,7 +52,7 @@ open class XYGpsBluetoothDevice(context: Context, scanResult: XYScanResult, hash override val prefix = "xy:gps" override fun find(): Deferred> { - logInfo("find") + log.info("find") return controlService.buzzerSelect.set(1) } @@ -62,7 +62,7 @@ open class XYGpsBluetoothDevice(context: Context, scanResult: XYScanResult, hash } // fun reportButtonPressed(state: Int) { -// logInfo("reportButtonPressed") +// log.info("reportButtonPressed") // synchronized(listeners) { // for (listener in listeners) { // val xy3Listener = listener as? Listener @@ -115,13 +115,13 @@ open class XYGpsBluetoothDevice(context: Context, scanResult: XYScanResult, hash } fun reportGlobalButtonPressed(device: XYGpsBluetoothDevice, state: ButtonPress) { - logInfo("reportButtonPressed (Global)") + log.info("reportButtonPressed (Global)") GlobalScope.launch { synchronized(globalListeners) { for (listener in globalListeners) { val xyFinderListener = listener.value as? XYFinderBluetoothDevice.Listener if (xyFinderListener != null) { - logInfo("reportButtonPressed: $xyFinderListener") + log.info("reportButtonPressed: $xyFinderListener") GlobalScope.launch { when (state) { ButtonPress.Single -> xyFinderListener.buttonSinglePressed(device) diff --git a/sdk-android-library/src/main/java/network/xyo/ble/devices/XYIBeaconBluetoothDevice.kt b/sdk-android-library/src/main/java/network/xyo/ble/devices/XYIBeaconBluetoothDevice.kt index 4a071a73..494a01ec 100644 --- a/sdk-android-library/src/main/java/network/xyo/ble/devices/XYIBeaconBluetoothDevice.kt +++ b/sdk-android-library/src/main/java/network/xyo/ble/devices/XYIBeaconBluetoothDevice.kt @@ -99,7 +99,7 @@ open class XYIBeaconBluetoothDevice(context: Context, scanResult: XYScanResult?, UUID(high, low) } catch (ex: BufferUnderflowException) { // can throw a BufferUnderflowException if the beacon sends an invalid value for UUID. - logError("refreshGatt catch $ex", true) + log.error("refreshGatt catch $ex", true) return null } } else { diff --git a/sdk-android-library/src/main/java/network/xyo/ble/firmware/OtaFile.kt b/sdk-android-library/src/main/java/network/xyo/ble/firmware/OtaFile.kt index 361d54f9..f6782f4c 100644 --- a/sdk-android-library/src/main/java/network/xyo/ble/firmware/OtaFile.kt +++ b/sdk-android-library/src/main/java/network/xyo/ble/firmware/OtaFile.kt @@ -9,8 +9,7 @@ import java.io.InputStream import java.util.* import kotlin.experimental.xor -class OtaFile @Throws(IOException::class) -private constructor(private val inputStream: InputStream?) { +class OtaFile(private val inputStream: InputStream?): XYBase() { private var bytes: ByteArray? = null private var blocks: Array>? = null @@ -66,7 +65,7 @@ private constructor(private val inputStream: InputStream?) { totalChunkCount = 0 blocks = fileEmptyArray(numberOfBlocks) - XYBase.logInfo("OtaFile", "initBlocksSuota numberOfBlocks: $numberOfBlocks") + log.info("OtaFile", "initBlocksSuota numberOfBlocks: $numberOfBlocks") var byteOffset = 0 // Loop through all the bytes and split them into pieces the size of the default chunk size @@ -90,7 +89,7 @@ private constructor(private val inputStream: InputStream?) { chunkSize = blockSize.rem(fileChunkSize) } - //XYBase.logInfo("OtaFile", "total bytes: " + bytes!!.size + ", offset: " + byteOffset + ", block: " + i + ", chunk: " + (chunkNumber + 1) + ", blockSize: " + blockSize + ", chunkSize: " + chunkSize) + //XYBase.log.info("OtaFile", "total bytes: " + bytes!!.size + ", offset: " + byteOffset + ", block: " + i + ", chunk: " + (chunkNumber + 1) + ", blockSize: " + blockSize + ", chunkSize: " + chunkSize) val chunk = Arrays.copyOfRange(bytes!!, byteOffset, byteOffset + chunkSize) blocks!![i][chunkNumber] = chunk byteOffset += chunkSize @@ -123,7 +122,7 @@ private constructor(private val inputStream: InputStream?) { val intVal = byteValue.toInt() crcCode = crcCode xor intVal.toByte() } - //XYBase.logInfo("OtaFile", String.format("Firmware CRC: %#04x", crc_code and 0xff.toByte())) + //XYBase.log.info("OtaFile", String.format("Firmware CRC: %#04x", crc_code and 0xff.toByte())) return crcCode } diff --git a/sdk-android-library/src/main/java/network/xyo/ble/firmware/OtaUpdate.kt b/sdk-android-library/src/main/java/network/xyo/ble/firmware/OtaUpdate.kt index 682b5306..978d5a76 100644 --- a/sdk-android-library/src/main/java/network/xyo/ble/firmware/OtaUpdate.kt +++ b/sdk-android-library/src/main/java/network/xyo/ble/firmware/OtaUpdate.kt @@ -5,9 +5,9 @@ import network.xyo.ble.devices.XY4BluetoothDevice import network.xyo.ble.devices.XYBluetoothDevice import network.xyo.ble.gatt.XYBluetoothResult import network.xyo.ble.gatt.asyncBle -import network.xyo.core.XYBase.Companion.logInfo +import network.xyo.core.XYBase -class OtaUpdate(var device: XY4BluetoothDevice, private val otaFile: OtaFile?) { +class OtaUpdate(var device: XY4BluetoothDevice, private val otaFile: OtaFile?): XYBase() { private val listeners = HashMap() private var updateJob: Job? = null @@ -118,7 +118,7 @@ class OtaUpdate(var device: XY4BluetoothDevice, private val otaFile: OtaFile?) { hasError = true failUpdate(error.message.toString()) updateJob?.cancelAndJoin() - logInfo(TAG, "startUpdate - MemDev ERROR: $error") + log.info( "startUpdate - MemDev ERROR: $error") } //STEP 2 - GpioMap @@ -127,7 +127,7 @@ class OtaUpdate(var device: XY4BluetoothDevice, private val otaFile: OtaFile?) { hasError = true failUpdate(error.message.toString()) updateJob?.cancelAndJoin() - logInfo(TAG, "startUpdate - GPIO ERROR: $error") + log.info( "startUpdate - GPIO ERROR: $error") } //STEP 3 - Set patch length for the first and last block @@ -136,7 +136,7 @@ class OtaUpdate(var device: XY4BluetoothDevice, private val otaFile: OtaFile?) { hasError = true failUpdate(error.message.toString()) updateJob?.cancelAndJoin() - logInfo(TAG, "startUpdate - patch ERROR: $error") + log.info( "startUpdate - patch ERROR: $error") } //STEP 4 - send blocks @@ -147,26 +147,26 @@ class OtaUpdate(var device: XY4BluetoothDevice, private val otaFile: OtaFile?) { hasError = true failUpdate(error.message.toString()) updateJob?.cancelAndJoin() - logInfo(TAG, "startUpdate - sendBlock ERROR: $error") + log.info( "startUpdate - sendBlock ERROR: $error") } if (lastBlock) { if (!lastBlockReady && otaFile?.numberOfBytes?.rem(otaFile.fileBlockSize) != 0) { - logInfo(TAG, "startUpdate LAST BLOCK - SET PATCH LEN: $lastBlock") + log.info( "startUpdate LAST BLOCK - SET PATCH LEN: $lastBlock") val finalPatchResult = setPatchLength().await() finalPatchResult.error?.let { error -> hasError = true failUpdate(error.message.toString()) updateJob?.cancelAndJoin() - logInfo(TAG, "startUpdate - finalPatchResult ERROR: $error") + log.info( "startUpdate - finalPatchResult ERROR: $error") } } } } - logInfo(TAG, "startUpdate done sending blocks") + log.info( "startUpdate done sending blocks") //SEND END SIGNAL val endResult = sendEndSignal().await() @@ -174,13 +174,13 @@ class OtaUpdate(var device: XY4BluetoothDevice, private val otaFile: OtaFile?) { hasError = true failUpdate(error.message.toString()) updateJob?.cancelAndJoin() - logInfo(TAG, "startUpdate - endSignal Result ERROR: $error") + log.info( "startUpdate - endSignal Result ERROR: $error") } //REBOOT val reboot = sendReboot().await() reboot.error?.let { error -> - logInfo(TAG, "startUpdate - reboot ERROR: $error") + log.info( "startUpdate - reboot ERROR: $error") } passUpdate() @@ -222,7 +222,7 @@ class OtaUpdate(var device: XY4BluetoothDevice, private val otaFile: OtaFile?) { private fun setMemDev(): Deferred> { return asyncBle { val memType = MEMORY_TYPE_EXTERNAL_SPI shl 24 or _imageBank - logInfo(TAG, "setMemDev: " + String.format("%#010x", memType)) + log.info( "setMemDev: " + String.format("%#010x", memType)) val result = device.spotaService.SPOTA_MEM_DEV.set(memType).await() return@asyncBle XYBluetoothResult(result.value, result.error) @@ -249,7 +249,7 @@ class OtaUpdate(var device: XY4BluetoothDevice, private val otaFile: OtaFile?) { lastBlockReady = true } - logInfo(TAG, "setPatchLength blockSize: $blockSize - ${String.format("%#06x", blockSize)}") + log.info( "setPatchLength blockSize: $blockSize - ${String.format("%#06x", blockSize)}") val result = device.spotaService.SPOTA_PATCH_LEN.set(blockSize!!).await() @@ -270,12 +270,12 @@ class OtaUpdate(var device: XY4BluetoothDevice, private val otaFile: OtaFile?) { val chunk = block[i] val msg = "Sending block " + (blockCounter + 1) + ", chunk " + (i + 1) + " of " + block.size + ", size " + chunk.size - logInfo(TAG, msg) + log.info( msg) if (lastChunk) { - logInfo(TAG, "sendBlock... lastChunk") + log.info( "sendBlock... lastChunk") if (!lastBlock) { blockCounter++ } else { @@ -294,10 +294,10 @@ class OtaUpdate(var device: XY4BluetoothDevice, private val otaFile: OtaFile?) { private fun sendEndSignal(): Deferred> { - logInfo(TAG, "sendEndSignal...") + log.info( "sendEndSignal...") return asyncBle { val result = device.spotaService.SPOTA_MEM_DEV.set(END_SIGNAL).await() - logInfo(TAG, "sendEndSignal result: $result") + log.info( "sendEndSignal result: $result") endSignalSent = true return@asyncBle XYBluetoothResult(result.value, result.error) } @@ -305,7 +305,7 @@ class OtaUpdate(var device: XY4BluetoothDevice, private val otaFile: OtaFile?) { //DONE private fun sendReboot(): Deferred> { - logInfo(TAG, "sendReboot...") + log.info( "sendReboot...") return asyncBle { val result = device.spotaService.SPOTA_MEM_DEV.set(REBOOT_SIGNAL).await() return@asyncBle XYBluetoothResult(result.value, result.error) diff --git a/sdk-android-library/src/main/java/network/xyo/ble/gatt/XYBluetoothError.kt b/sdk-android-library/src/main/java/network/xyo/ble/gatt/XYBluetoothError.kt index dccf461b..df0b734d 100644 --- a/sdk-android-library/src/main/java/network/xyo/ble/gatt/XYBluetoothError.kt +++ b/sdk-android-library/src/main/java/network/xyo/ble/gatt/XYBluetoothError.kt @@ -1,12 +1,10 @@ package network.xyo.ble.gatt - import network.xyo.core.XYBase - open class XYBluetoothError(message: String) : Error(message) { init { - XYBase.logError(tag, message, false) + log.error(message, false) } val tag: String @@ -18,4 +16,6 @@ open class XYBluetoothError(message: String) : Error(message) { override fun toString(): String { return "$tag: ${super.toString()}" } + + companion object: XYBase() } \ No newline at end of file diff --git a/sdk-android-library/src/main/java/network/xyo/ble/gatt/XYBluetoothGatt.kt b/sdk-android-library/src/main/java/network/xyo/ble/gatt/XYBluetoothGatt.kt index ada4085b..0aaac3b1 100644 --- a/sdk-android-library/src/main/java/network/xyo/ble/gatt/XYBluetoothGatt.kt +++ b/sdk-android-library/src/main/java/network/xyo/ble/gatt/XYBluetoothGatt.kt @@ -110,7 +110,7 @@ open class XYBluetoothGatt protected constructor( } fun refreshGatt() = asyncBle { - logInfo("refreshGatt") + log.info("refreshGatt") var result = false var error: XYBluetoothError? = null @@ -120,12 +120,12 @@ open class XYBluetoothGatt protected constructor( } else { try { val localMethod = BluetoothGatt::class.java.getMethod("refresh") - logInfo("refreshGatt found method $localMethod") + log.info("refreshGatt found method $localMethod") result = (localMethod.invoke(gatt) as Boolean) } catch (ex: NoSuchMethodException) { //null receiver error = XYBluetoothError("refreshGatt: Failed to refresh gatt") - logError("refreshGatt catch $ex", true) + log.error("refreshGatt catch $ex", true) //method not found } } @@ -133,7 +133,7 @@ open class XYBluetoothGatt protected constructor( } fun connectGatt() = asyncBle { - logInfo("connectGatt") + log.info("connectGatt") var error: XYBluetoothError? = null var value: Boolean? = null @@ -171,7 +171,7 @@ open class XYBluetoothGatt protected constructor( } fun connect() = asyncBle { - logInfo("connect") + log.info("connect") var error: XYBluetoothError? = null var value: Boolean? = null var gatt = this@XYBluetoothGatt.gatt @@ -191,7 +191,7 @@ open class XYBluetoothGatt protected constructor( val listenerName = "connect$nowNano" value = suspendCancellableCoroutine { cont -> var resumed = false - logInfo("connect: suspendCancellableCoroutine") + log.info("connect: suspendCancellableCoroutine") val listener = object : XYBluetoothGattCallback() { override fun onConnectionStateChange(gatt: BluetoothGatt?, status: Int, newState: Int) { super.onConnectionStateChange(gatt, status, newState) @@ -200,18 +200,18 @@ open class XYBluetoothGatt protected constructor( resumed = true when { status == BluetoothGatt.GATT_FAILURE -> { - logInfo("connect:failure: $status : $newState") + log.info("connect:failure: $status : $newState") error = XYBluetoothError("connect: connection failed(status): $status : $newState") removeGattListener(listenerName) cont.tryResumeSilent(null) } newState == BluetoothGatt.STATE_CONNECTED -> { - logInfo("connect:connected") + log.info("connect:connected") removeGattListener(listenerName) cont.tryResumeSilent(true) } - newState == BluetoothGatt.STATE_CONNECTING -> logInfo("connect:connecting") + newState == BluetoothGatt.STATE_CONNECTING -> log.info("connect:connecting") else -> { error = XYBluetoothError("connect: connection failed unknown(state): $status : $newState") @@ -225,15 +225,15 @@ open class XYBluetoothGatt protected constructor( addGattListener(listenerName, listener) if (connectionState == ConnectionState.Connected) { - logInfo("asyncConnect:already connected") + log.info("asyncConnect:already connected") removeGattListener(listenerName) resumed = true cont.tryResumeSilent(true) } else if (connectionState == ConnectionState.Connecting) { - logInfo("connect:connecting") + log.info("connect:connecting") //dont call connect since already in progress } else if (!gatt.connect()) { - logInfo("connect: failed to start connect") + log.info("connect: failed to start connect") error = XYBluetoothError("connect: gatt.connect failed to start") removeGattListener(listenerName) resumed = true @@ -247,12 +247,12 @@ open class XYBluetoothGatt protected constructor( while (!resumed) { delay(500) lastAccessTime = now //prevent cleanup for cleaningup before the timeout - logInfo("connect: waiting...") + log.info("connect: waiting...") } } } catch (ex: TimeoutCancellationException) { if (!resumed) { - logInfo("connect: timeout - cancelling") + log.info("connect: timeout - cancelling") removeGattListener(listenerName) close() resumed = true @@ -267,7 +267,7 @@ open class XYBluetoothGatt protected constructor( } fun disconnect() = asyncBle { - logInfo("disconnect") + log.info("disconnect") var error: XYBluetoothError? = null val gatt = this@XYBluetoothGatt.gatt @@ -307,14 +307,14 @@ open class XYBluetoothGatt protected constructor( when (connectionState) { ConnectionState.Disconnected -> { - logInfo("asyncDisconnect:already disconnected") + log.info("asyncDisconnect:already disconnected") removeGattListener(listenerName) cont.tryResumeSilent(true) } - ConnectionState.Disconnecting -> logInfo("asyncDisconnect:disconnecting") + ConnectionState.Disconnecting -> log.info("asyncDisconnect:disconnecting") //dont call connect since already in progress else -> { - logInfo("asyncDisconnect:starting disconnect") + log.info("asyncDisconnect:starting disconnect") gatt.disconnect() } } @@ -324,13 +324,13 @@ open class XYBluetoothGatt protected constructor( } protected fun close() = asyncBle { - logInfo("close") + log.info("close") val gatt = gatt ?: return@asyncBle XYBluetoothResult(true) if (connectionState != ConnectionState.Disconnected) { disconnect().await() } gatt.close() - logInfo("close: Closed") + log.info("close: Closed") removeGattListener("default") this@XYBluetoothGatt.gatt = null return@asyncBle XYBluetoothResult(true) @@ -347,7 +347,7 @@ open class XYBluetoothGatt protected constructor( value = gatt.services } else { value = suspendCancellableCoroutine { cont -> - logInfo("discover") + log.info("discover") val listenerName = "discover$nowNano" val listener = object : XYBluetoothGattCallback() { override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) { @@ -404,7 +404,7 @@ open class XYBluetoothGatt protected constructor( //this can only be called after a successful discover protected fun findCharacteristic(service: UUID, characteristic: UUID) = asyncBle { - logInfo("findCharacteristic") + log.info("findCharacteristic") var error: XYBluetoothError? = null var value: BluetoothGattCharacteristic? = null @@ -418,12 +418,12 @@ open class XYBluetoothGatt protected constructor( error = XYBluetoothError("Services Not Discovered Yet") cont.tryResumeSilent(null) } else { - logInfo("findCharacteristic") + log.info("findCharacteristic") val foundService = gatt.getService(service) - logInfo("findCharacteristic:service:$foundService") + log.info("findCharacteristic:service:$foundService") if (foundService != null) { val foundCharacteristic = foundService.getCharacteristic(characteristic) - logInfo("findCharacteristic:characteristic:$foundCharacteristic") + log.info("findCharacteristic:characteristic:$foundCharacteristic") cont.tryResumeSilent(foundCharacteristic) } else { error = XYBluetoothError("findCharacteristic: Characteristic not Found!") @@ -432,12 +432,12 @@ open class XYBluetoothGatt protected constructor( } } } - logInfo("findCharacteristic: Returning: $value") + log.info("findCharacteristic: Returning: $value") return@asyncBle XYBluetoothResult(value, error) } protected fun writeCharacteristic(characteristicToWrite: BluetoothGattCharacteristic) = queueBle { - logInfo("writeCharacteristic") + log.info("writeCharacteristic") var error: XYBluetoothError? = null var value: ByteArray? = null @@ -451,7 +451,7 @@ open class XYBluetoothGatt protected constructor( value = suspendCancellableCoroutine { cont -> val listener = object : XYBluetoothGattCallback() { override fun onCharacteristicWrite(gatt: BluetoothGatt?, characteristic: BluetoothGattCharacteristic?, status: Int) { - logInfo("onCharacteristicWrite: $status") + log.info("onCharacteristicWrite: $status") super.onCharacteristicWrite(gatt, characteristic, status) if (!resumed) { //since it is always possible to have a rogue callback, make sure it is the one we are looking for @@ -471,7 +471,7 @@ open class XYBluetoothGatt protected constructor( } override fun onConnectionStateChange(gatt: BluetoothGatt?, status: Int, newState: Int) { - logInfo("onCharacteristicWrite") + log.info("onCharacteristicWrite") super.onConnectionStateChange(gatt, status, newState) if (!resumed && newState != BluetoothGatt.STATE_CONNECTED) { error = XYBluetoothError("writeCharacteristic: connection dropped") @@ -512,12 +512,12 @@ open class XYBluetoothGatt protected constructor( // This function throws [CancellationException] if the coroutine is cancelled or completed while suspended. // It seems that the proper fix for this is to actually cancel it if it is cancelled and actually throw and error // if it is resumed twice - logError(ex, true) + log.error(ex, true) } } protected fun setCharacteristicNotify(characteristicToWrite: BluetoothGattCharacteristic, notify: Boolean): XYBluetoothResult { - logInfo("setCharacteristicNotify") + log.info("setCharacteristicNotify") var error: XYBluetoothError? = null var value: Boolean? = null @@ -534,7 +534,7 @@ open class XYBluetoothGatt protected constructor( protected fun writeDescriptor(descriptorToWrite: BluetoothGattDescriptor): Deferred> { return queueBle { - logInfo("writeDescriptor") + log.info("writeDescriptor") var error: XYBluetoothError? = null var value: ByteArray? = null @@ -548,7 +548,7 @@ open class XYBluetoothGatt protected constructor( var resumed = false val listener = object : XYBluetoothGattCallback() { override fun onDescriptorWrite(gatt: BluetoothGatt?, descriptor: BluetoothGattDescriptor?, status: Int) { - logInfo("onDescriptorWrite: $status") + log.info("onDescriptorWrite: $status") super.onDescriptorWrite(gatt, descriptor, status) if (!resumed) { //since it is always possible to have a rogue callback, make sure it is the one we are looking for @@ -568,7 +568,7 @@ open class XYBluetoothGatt protected constructor( } override fun onConnectionStateChange(gatt: BluetoothGatt?, status: Int, newState: Int) { - logInfo("onConnectionStateChange") + log.info("onConnectionStateChange") super.onConnectionStateChange(gatt, status, newState) if (!resumed) { if (newState != BluetoothGatt.STATE_CONNECTED) { @@ -601,7 +601,7 @@ open class XYBluetoothGatt protected constructor( protected fun readCharacteristic(characteristicToRead: BluetoothGattCharacteristic): Deferred> { return queueBle { - logInfo("readCharacteristic") + log.info("readCharacteristic") var error: XYBluetoothError? = null var value: BluetoothGattCharacteristic? = null @@ -671,7 +671,7 @@ open class XYBluetoothGatt protected constructor( //if null is passed back, the sdk was unable to create the safe session fun connectionWithResult(closure: suspend () -> XYBluetoothResult): Deferred> { return asyncBle { - logInfo("connection") + log.info("connection") var value: T? = null var error: XYBluetoothError? = null references++ @@ -696,7 +696,7 @@ open class XYBluetoothGatt protected constructor( fun connection(closure: suspend () -> Unit): Deferred> { return asyncBle { - logInfo("connection") + log.info("connection") val value: Unit? = null var error: XYBluetoothError? = null references++ @@ -720,7 +720,7 @@ open class XYBluetoothGatt protected constructor( @TargetApi(Build.VERSION_CODES.KITKAT) private fun connectGatt19(device: BluetoothDevice, autoConnect: Boolean): BluetoothGatt? { - logInfo("connectGatt19") + log.info("connectGatt19") return device.connectGatt(context, autoConnect, centralCallback) } @@ -728,7 +728,7 @@ open class XYBluetoothGatt protected constructor( private fun connectGatt23(device: BluetoothDevice, autoConnect: Boolean, transport: Int?): BluetoothGatt? { - logInfo("connectGatt23") + log.info("connectGatt23") return if (transport == null) { device.connectGatt(context, autoConnect, centralCallback) } else { @@ -742,7 +742,7 @@ open class XYBluetoothGatt protected constructor( transport: Int?, phy: Int?, handler: Handler?): BluetoothGatt? { - logInfo("connectGatt26") + log.info("connectGatt26") return when { transport == null -> device.connectGatt(context, autoConnect, centralCallback) phy == null -> device.connectGatt(context, autoConnect, centralCallback, transport) @@ -754,11 +754,11 @@ open class XYBluetoothGatt protected constructor( private val centralCallback = object : XYBluetoothGattCallback() { override fun onCharacteristicChanged(gatt: BluetoothGatt?, characteristic: BluetoothGattCharacteristic?) { super.onCharacteristicChanged(gatt, characteristic) - logInfo("onCharacteristicChanged: $characteristic") + log.info("onCharacteristicChanged: $characteristic") synchronized(gattListeners) { for ((key, listener) in gattListeners) { GlobalScope.launch { - logInfo("onCharacteristicChanged: $key") + log.info("onCharacteristicChanged: $key") listener.onCharacteristicChanged(gatt, characteristic) } } @@ -767,7 +767,7 @@ open class XYBluetoothGatt protected constructor( override fun onCharacteristicRead(gatt: BluetoothGatt?, characteristic: BluetoothGattCharacteristic?, status: Int) { super.onCharacteristicRead(gatt, characteristic, status) - logInfo("onCharacteristicRead: $characteristic : $status") + log.info("onCharacteristicRead: $characteristic : $status") synchronized(gattListeners) { for ((_, listener) in gattListeners) { GlobalScope.launch { @@ -778,10 +778,10 @@ open class XYBluetoothGatt protected constructor( } override fun onCharacteristicWrite(gatt: BluetoothGatt?, characteristic: BluetoothGattCharacteristic?, status: Int) { - logInfo("onCharacteristicWrite: $status") + log.info("onCharacteristicWrite: $status") super.onCharacteristicWrite(gatt, characteristic, status) synchronized(gattListeners) { - logInfo("onCharacteristicWrite3: $status") + log.info("onCharacteristicWrite3: $status") for ((_, listener) in gattListeners) { GlobalScope.launch { listener.onCharacteristicWrite(gatt, characteristic, status) @@ -792,12 +792,12 @@ open class XYBluetoothGatt protected constructor( override fun onConnectionStateChange(gatt: BluetoothGatt?, status: Int, newState: Int) { super.onConnectionStateChange(gatt, status, newState) - logInfo("onConnectionStateChange: ${gatt?.device?.address} $newState : $status") + log.info("onConnectionStateChange: ${gatt?.device?.address} $newState : $status") synchronized(gattListeners) { _connectionState = newState for ((tag, listener) in gattListeners) { GlobalScope.launch { - logInfo("onConnectionStateChange: $tag") + log.info("onConnectionStateChange: $tag") listener.onConnectionStateChange(gatt, status, newState) } } @@ -809,7 +809,7 @@ open class XYBluetoothGatt protected constructor( override fun onDescriptorRead(gatt: BluetoothGatt?, descriptor: BluetoothGattDescriptor?, status: Int) { super.onDescriptorRead(gatt, descriptor, status) - logInfo("onDescriptorRead: $descriptor : $status") + log.info("onDescriptorRead: $descriptor : $status") synchronized(gattListeners) { for ((_, listener) in gattListeners) { GlobalScope.launch { @@ -821,7 +821,7 @@ open class XYBluetoothGatt protected constructor( override fun onDescriptorWrite(gatt: BluetoothGatt?, descriptor: BluetoothGattDescriptor?, status: Int) { super.onDescriptorWrite(gatt, descriptor, status) - logInfo("onDescriptorWrite: $descriptor : $status") + log.info("onDescriptorWrite: $descriptor : $status") synchronized(gattListeners) { for ((_, listener) in gattListeners) { GlobalScope.launch { @@ -833,7 +833,7 @@ open class XYBluetoothGatt protected constructor( override fun onMtuChanged(gatt: BluetoothGatt?, mtu: Int, status: Int) { super.onMtuChanged(gatt, mtu, status) - logInfo("onMtuChanged: $mtu : $status") + log.info("onMtuChanged: $mtu : $status") synchronized(gattListeners) { for ((_, listener) in gattListeners) { GlobalScope.launch { @@ -845,7 +845,7 @@ open class XYBluetoothGatt protected constructor( override fun onPhyRead(gatt: BluetoothGatt?, txPhy: Int, rxPhy: Int, status: Int) { super.onPhyRead(gatt, txPhy, rxPhy, status) - logInfo("onPhyRead: $txPhy : $rxPhy : $status") + log.info("onPhyRead: $txPhy : $rxPhy : $status") synchronized(gattListeners) { for ((_, listener) in gattListeners) { GlobalScope.launch { @@ -858,7 +858,7 @@ open class XYBluetoothGatt protected constructor( @TargetApi(26) override fun onPhyUpdate(gatt: BluetoothGatt?, txPhy: Int, rxPhy: Int, status: Int) { super.onPhyUpdate(gatt, txPhy, rxPhy, status) - logInfo("onPhyUpdate: $txPhy : $rxPhy : $status") + log.info("onPhyUpdate: $txPhy : $rxPhy : $status") synchronized(gattListeners) { for ((_, listener) in gattListeners) { GlobalScope.launch { @@ -870,7 +870,7 @@ open class XYBluetoothGatt protected constructor( override fun onReadRemoteRssi(gatt: BluetoothGatt?, rssi: Int, status: Int) { super.onReadRemoteRssi(gatt, rssi, status) - logInfo("onReadRemoteRssi: $rssi : $status") + log.info("onReadRemoteRssi: $rssi : $status") this@XYBluetoothGatt.rssi = rssi onDetect(null) synchronized(gattListeners) { @@ -884,7 +884,7 @@ open class XYBluetoothGatt protected constructor( override fun onReliableWriteCompleted(gatt: BluetoothGatt?, status: Int) { super.onReliableWriteCompleted(gatt, status) - logInfo("onReliableWriteCompleted: $status") + log.info("onReliableWriteCompleted: $status") synchronized(gattListeners) { for ((_, listener) in gattListeners) { GlobalScope.launch { @@ -896,7 +896,7 @@ open class XYBluetoothGatt protected constructor( override fun onServicesDiscovered(gatt: BluetoothGatt?, status: Int) { super.onServicesDiscovered(gatt, status) - logInfo("onServicesDiscovered: $status") + log.info("onServicesDiscovered: $status") synchronized(gattListeners) { for ((_, listener) in gattListeners) { GlobalScope.launch { @@ -916,7 +916,7 @@ open class XYBluetoothGatt protected constructor( cleanUpThread = GlobalScope.launch { while (!closed) { - logInfo("cleanUpIfNeeded: ${references}") + log.info("cleanUpIfNeeded: ${references}") //if the global and local last connection times do not match //after the delay, that means a newer connection is now responsible for closing it val localAccessTime = now @@ -930,10 +930,10 @@ open class XYBluetoothGatt protected constructor( //down to zero. We have to check the lastAccess to make sure the delay is after //the last guy, not an earlier one - logInfo("cleanUpIfNeeded: Checking") + log.info("cleanUpIfNeeded: Checking") if (!getStayConnected() && !closed && references == 0 && lastAccessTime < localAccessTime) { - logInfo("cleanUpIfNeeded: Cleaning") + log.info("cleanUpIfNeeded: Cleaning") close().await() } } diff --git a/sdk-android-library/src/main/java/network/xyo/ble/gatt/XYBluetoothGattClient.kt b/sdk-android-library/src/main/java/network/xyo/ble/gatt/XYBluetoothGattClient.kt index 784cea97..03cc93d2 100644 --- a/sdk-android-library/src/main/java/network/xyo/ble/gatt/XYBluetoothGattClient.kt +++ b/sdk-android-library/src/main/java/network/xyo/ble/gatt/XYBluetoothGattClient.kt @@ -25,7 +25,7 @@ open class XYBluetoothGattClient protected constructor( fun readCharacteristicInt(characteristicToRead: BluetoothGattCharacteristic, formatType:Int, offset:Int) : Deferred>{ return asyncBle { - logInfo("readCharacteristicInt") + log.info("readCharacteristicInt") val readResult = readCharacteristic(characteristicToRead).await() var value: Int? = null val error = readResult.error @@ -34,7 +34,7 @@ open class XYBluetoothGattClient protected constructor( } if (error != null) { - logError(error.toString(), false) + log.error(error.toString(), false) } return@asyncBle XYBluetoothResult(value, error) @@ -43,7 +43,7 @@ open class XYBluetoothGattClient protected constructor( fun readCharacteristicString(characteristicToRead: BluetoothGattCharacteristic, offset: Int) : Deferred>{ return asyncBle { - logInfo("readCharacteristicString") + log.info("readCharacteristicString") val readResult = readCharacteristic(characteristicToRead).await() var value: String? = null val error = readResult.error @@ -52,7 +52,7 @@ open class XYBluetoothGattClient protected constructor( } if (error != null) { - logError(error.toString(), false) + log.error(error.toString(), false) } return@asyncBle XYBluetoothResult(value, error) @@ -61,7 +61,7 @@ open class XYBluetoothGattClient protected constructor( fun readCharacteristicFloat(characteristicToRead: BluetoothGattCharacteristic, formatType:Int, offset:Int) : Deferred>{ return asyncBle { - logInfo("readCharacteristicFloat") + log.info("readCharacteristicFloat") val readResult = readCharacteristic(characteristicToRead).await() var value: Float? = null val error = readResult.error @@ -70,7 +70,7 @@ open class XYBluetoothGattClient protected constructor( } if (error != null) { - logError(error.toString(), false) + log.error(error.toString(), false) } return@asyncBle XYBluetoothResult(value, error) @@ -79,7 +79,7 @@ open class XYBluetoothGattClient protected constructor( fun readCharacteristicBytes(characteristicToRead: BluetoothGattCharacteristic) : Deferred>{ return asyncBle { - logInfo("readCharacteristicBytes") + log.info("readCharacteristicBytes") val readResult = readCharacteristic(characteristicToRead).await() var value: ByteArray? = null val error = readResult.error @@ -88,7 +88,7 @@ open class XYBluetoothGattClient protected constructor( } if (error != null) { - logError(error.toString(), false) + log.error(error.toString(), false) } return@asyncBle XYBluetoothResult(value, error) @@ -97,7 +97,7 @@ open class XYBluetoothGattClient protected constructor( fun findAndReadCharacteristicInt(service: UUID, characteristic: UUID, formatType:Int, offset:Int = 0) : Deferred> { return asyncBle { - logInfo("findAndReadCharacteristicInt") + log.info("findAndReadCharacteristicInt") val findResult = findCharacteristic(service, characteristic).await() val characteristicToRead = findResult.value var value: Int? = null @@ -113,7 +113,7 @@ open class XYBluetoothGattClient protected constructor( } if (error != null) { - logError(error.toString(), false) + log.error(error.toString(), false) } return@asyncBle XYBluetoothResult(value, error) @@ -122,7 +122,7 @@ open class XYBluetoothGattClient protected constructor( fun findAndReadCharacteristicFloat(service: UUID, characteristic: UUID, formatType:Int, offset:Int = 0) : Deferred> { return asyncBle { - logInfo("findAndReadCharacteristicFloat") + log.info("findAndReadCharacteristicFloat") val findResult = findCharacteristic(service, characteristic).await() val characteristicToRead = findResult.value var value: Float? = null @@ -138,7 +138,7 @@ open class XYBluetoothGattClient protected constructor( } if (error != null) { - logError(error.toString(), false) + log.error(error.toString(), false) } return@asyncBle XYBluetoothResult(value, error) @@ -147,7 +147,7 @@ open class XYBluetoothGattClient protected constructor( fun findAndReadCharacteristicString(service: UUID, characteristic: UUID, offset:Int = 0) : Deferred> { return asyncBle { - logInfo("findAndReadCharacteristicString") + log.info("findAndReadCharacteristicString") val findResult = findCharacteristic(service, characteristic).await() val characteristicToRead = findResult.value var value: String? = null @@ -163,7 +163,7 @@ open class XYBluetoothGattClient protected constructor( } if (error != null) { - logError(error.toString(), false) + log.error(error.toString(), false) } return@asyncBle XYBluetoothResult(value, error) @@ -172,7 +172,7 @@ open class XYBluetoothGattClient protected constructor( fun findAndReadCharacteristicBytes(service: UUID, characteristic: UUID) : Deferred> { return asyncBle { - logInfo("findAndReadCharacteristicString") + log.info("findAndReadCharacteristicString") val findResult = findCharacteristic(service, characteristic).await() val characteristicToRead = findResult.value var value: ByteArray? = null @@ -188,7 +188,7 @@ open class XYBluetoothGattClient protected constructor( } if (error != null) { - logError(error.toString(), false) + log.error(error.toString(), false) } return@asyncBle XYBluetoothResult(value, error) @@ -197,20 +197,20 @@ open class XYBluetoothGattClient protected constructor( fun findAndWriteCharacteristic(service: UUID, characteristic: UUID, valueToWrite:Int, formatType:Int, offset:Int) : Deferred> { return asyncBle { - logInfo("findAndWriteCharacteristic") + log.info("findAndWriteCharacteristic") var error: XYBluetoothError? = null var value: Int? = null val findResult = findCharacteristic(service, characteristic).await() - logInfo("findAndWriteCharacteristic: Found") + log.info("findAndWriteCharacteristic: Found") val characteristicToWrite = findResult.value if (findResult.error == null) { - logInfo("findAndWriteCharacteristic: $characteristicToWrite") + log.info("findAndWriteCharacteristic: $characteristicToWrite") if (characteristicToWrite != null) { characteristicToWrite.setValue(valueToWrite, formatType, offset) - logInfo("findAndWriteCharacteristic: Set") + log.info("findAndWriteCharacteristic: Set") val writeResult = writeCharacteristic(characteristicToWrite).await() - logInfo("findAndWriteCharacteristic: Write Complete: $writeResult") + log.info("findAndWriteCharacteristic: Write Complete: $writeResult") value = valueToWrite error = writeResult.error } else { @@ -219,7 +219,7 @@ open class XYBluetoothGattClient protected constructor( } if (error != null) { - logError(error.toString(), false) + log.error(error.toString(), false) } lastAccessTime = now @@ -230,7 +230,7 @@ open class XYBluetoothGattClient protected constructor( fun findAndWriteCharacteristicFloat(service: UUID, characteristic: UUID, mantissa: Int, exponent: Int, formatType:Int, offset:Int) : Deferred> { return asyncBle { - logInfo("findAndWriteCharacteristicFloat") + log.info("findAndWriteCharacteristicFloat") var error: XYBluetoothError? = null var value: ByteArray? = null @@ -248,7 +248,7 @@ open class XYBluetoothGattClient protected constructor( } if (error != null) { - logError(error.toString(), false) + log.error(error.toString(), false) } lastAccessTime = now @@ -259,7 +259,7 @@ open class XYBluetoothGattClient protected constructor( fun findAndWriteCharacteristic(service: UUID, characteristic: UUID, valueToWrite:String) : Deferred> { return asyncBle { - logInfo("findAndWriteCharacteristic") + log.info("findAndWriteCharacteristic") var error: XYBluetoothError? = null var value: String? = null @@ -277,7 +277,7 @@ open class XYBluetoothGattClient protected constructor( } if (error != null) { - logError(error.toString(), false) + log.error(error.toString(), false) } lastAccessTime = now @@ -288,7 +288,7 @@ open class XYBluetoothGattClient protected constructor( fun findAndWriteCharacteristic(service: UUID, characteristic: UUID, bytes:ByteArray) : Deferred> { return asyncBle { - logInfo("findAndWriteCharacteristic") + log.info("findAndWriteCharacteristic") var error: XYBluetoothError? = null var value: ByteArray? = null @@ -306,7 +306,7 @@ open class XYBluetoothGattClient protected constructor( } if (error != null) { - logError(error.toString(), false) + log.error(error.toString(), false) } lastAccessTime = now @@ -319,7 +319,7 @@ open class XYBluetoothGattClient protected constructor( fun findAndWriteCharacteristicNotify(service: UUID, characteristic: UUID, enable:Boolean) : Deferred> { return asyncBle { - logInfo("findAndWriteCharacteristicNotify") + log.info("findAndWriteCharacteristicNotify") var error: XYBluetoothError? = null var value: Boolean? = null @@ -346,7 +346,7 @@ open class XYBluetoothGattClient protected constructor( } if (error != null) { - logError(error.toString(), false) + log.error(error.toString(), false) } lastAccessTime = now diff --git a/sdk-android-library/src/main/java/network/xyo/ble/gatt/server/XYBluetoothGattServer.kt b/sdk-android-library/src/main/java/network/xyo/ble/gatt/server/XYBluetoothGattServer.kt index ed133e59..1c31fc02 100644 --- a/sdk-android-library/src/main/java/network/xyo/ble/gatt/server/XYBluetoothGattServer.kt +++ b/sdk-android-library/src/main/java/network/xyo/ble/gatt/server/XYBluetoothGattServer.kt @@ -33,7 +33,7 @@ open class XYBluetoothGattServer(context: Context) : XYBluetoothBase(context) { } fun stopServer() { - logInfo("stopServer") + log.info("stopServer") synchronized(this) { androidGattServer?.close() androidGattServer = null @@ -41,26 +41,26 @@ open class XYBluetoothGattServer(context: Context) : XYBluetoothBase(context) { } fun addListener(key: String, listener: BluetoothGattServerCallback) { - logInfo("addListener") + log.info("addListener") synchronized(listeners) { listeners[key] = listener } } fun removeListener(key: String) { - logInfo("removeListener") + log.info("removeListener") synchronized(listeners) { listeners.remove(key) } } fun getServices(): Array { - logInfo("getServices") + log.info("getServices") return services.values.toTypedArray() } fun isDeviceConnected(bluetoothDevice: BluetoothDevice): Boolean { - logInfo("isDeviceConnected") + log.info("isDeviceConnected") val connectedDevices = devices if (connectedDevices != null) { for (device in connectedDevices) { @@ -73,7 +73,7 @@ open class XYBluetoothGattServer(context: Context) : XYBluetoothBase(context) { } fun addService(serviceToAdd: XYBluetoothService) = asyncBle { - logInfo("addService") + log.info("addService") if (androidGattServer == null) { return@asyncBle XYBluetoothResult(XYBluetoothError("No Gatt Server")) } @@ -204,7 +204,7 @@ open class XYBluetoothGattServer(context: Context) : XYBluetoothBase(context) { private val primaryCallback = object : BluetoothGattServerCallback() { override fun onCharacteristicReadRequest(device: BluetoothDevice?, requestId: Int, offset: Int, characteristic: BluetoothGattCharacteristic?) { - logInfo("onCharacteristicReadRequest") + log.info("onCharacteristicReadRequest") super.onCharacteristicReadRequest(device, requestId, offset, characteristic) val service = services[characteristic?.service?.uuid] @@ -213,7 +213,7 @@ open class XYBluetoothGattServer(context: Context) : XYBluetoothBase(context) { if (readValue != null) { sendResponse(readValue, requestId, device) } else { - logInfo("Could not find response to send back.") + log.info("Could not find response to send back.") androidGattServer?.sendResponse(device, requestId, BluetoothGatt.GATT_FAILURE, 0, null) androidGattServer?.cancelConnection(device) } @@ -225,7 +225,7 @@ open class XYBluetoothGattServer(context: Context) : XYBluetoothBase(context) { } override fun onCharacteristicWriteRequest(device: BluetoothDevice?, requestId: Int, characteristic: BluetoothGattCharacteristic?, preparedWrite: Boolean, responseNeeded: Boolean, offset: Int, value: ByteArray?) { - logInfo("onCharacteristicWriteRequest") + log.info("onCharacteristicWriteRequest") super.onCharacteristicWriteRequest(device, requestId, characteristic, preparedWrite, responseNeeded, offset, value) val service = services[characteristic?.service?.uuid] if (service != null && characteristic != null && device != null) { @@ -233,7 +233,7 @@ open class XYBluetoothGattServer(context: Context) : XYBluetoothBase(context) { if (readValue == true) { sendResponse(value, requestId, device) } else { - logInfo("Could not find a responder to write to.") + log.info("Could not find a responder to write to.") androidGattServer?.sendResponse(device, requestId, BluetoothGatt.GATT_FAILURE, 0, null) androidGattServer?.cancelConnection(device) } @@ -248,8 +248,8 @@ open class XYBluetoothGattServer(context: Context) : XYBluetoothBase(context) { super.onConnectionStateChange(device, status, newState) when (newState) { - BluetoothGatt.STATE_CONNECTED -> logInfo("Device connected from server: ${device?.address}") - BluetoothGatt.STATE_DISCONNECTED -> logInfo("Device disconnect from server: ${device?.address}") + BluetoothGatt.STATE_CONNECTED -> log.info("Device connected from server: ${device?.address}") + BluetoothGatt.STATE_DISCONNECTED -> log.info("Device disconnect from server: ${device?.address}") } for ((_, listener) in listeners) { diff --git a/sdk-android-library/src/main/java/network/xyo/ble/scanner/XYScanResultManual.kt b/sdk-android-library/src/main/java/network/xyo/ble/scanner/XYScanResultManual.kt index 6e937d53..a7fd6443 100644 --- a/sdk-android-library/src/main/java/network/xyo/ble/scanner/XYScanResultManual.kt +++ b/sdk-android-library/src/main/java/network/xyo/ble/scanner/XYScanResultManual.kt @@ -22,11 +22,11 @@ class XYScanResultManual ( get() = _timestampNanos override fun describeContents(): Int { - logError("describeContents: Not Implemented", true) + log.error("describeContents: Not Implemented", true) return 0 } override fun writeToParcel(dest: Parcel, flags: Int) { - logError("writeToParcel: Not Implemented", true) + log.error("writeToParcel: Not Implemented", true) } } \ No newline at end of file diff --git a/sdk-android-library/src/main/java/network/xyo/ble/scanner/XYSmartScan.kt b/sdk-android-library/src/main/java/network/xyo/ble/scanner/XYSmartScan.kt index 52df901b..bea33de9 100644 --- a/sdk-android-library/src/main/java/network/xyo/ble/scanner/XYSmartScan.kt +++ b/sdk-android-library/src/main/java/network/xyo/ble/scanner/XYSmartScan.kt @@ -62,11 +62,11 @@ abstract class XYSmartScan(context: Context) : XYBluetoothBase(context) { val bluetoothAdapterReceiver = object: BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { val action = intent?.getAction() - logInfo("onReceive: Action=${action}") + log.info("onReceive: Action=${action}") when (action) { BluetoothAdapter.ACTION_STATE_CHANGED -> { val state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) - logInfo("onReceive: State= ${state}") + log.info("onReceive: State= ${state}") if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) { if (restartingBluetooth) { @@ -185,13 +185,13 @@ abstract class XYSmartScan(context: Context) : XYBluetoothBase(context) { } open suspend fun start(): Boolean { - logInfo("start") + log.info("start") startTime = now return true } open suspend fun stop(): Boolean { - logInfo("stop") + log.info("stop") startTime = 0 scanResultCount = 0 return true @@ -199,7 +199,7 @@ abstract class XYSmartScan(context: Context) : XYBluetoothBase(context) { private var restartingBluetooth = false protected fun restartBluetooth() { - logInfo(">>>>> restartBluetooth: Restarting Bluetooth Adapter <<<<<") + log.info(">>>>> restartBluetooth: Restarting Bluetooth Adapter <<<<<") restartingBluetooth = true BluetoothAdapter.getDefaultAdapter().disable() } @@ -257,7 +257,7 @@ abstract class XYSmartScan(context: Context) : XYBluetoothBase(context) { } private fun reportEntered(device: XYBluetoothDevice) { - logInfo("reportEntered") + log.info("reportEntered") synchronized(listeners) { for ((_, listener) in listeners) { GlobalScope.launch { @@ -268,7 +268,7 @@ abstract class XYSmartScan(context: Context) : XYBluetoothBase(context) { } private fun reportExited(device: XYBluetoothDevice) { - logInfo("reportExited") + log.info("reportExited") synchronized(listeners) { for ((_, listener) in listeners) { GlobalScope.launch { @@ -279,7 +279,7 @@ abstract class XYSmartScan(context: Context) : XYBluetoothBase(context) { } private fun reportDetected(device: XYBluetoothDevice) { - //logInfo("reportDetected") + //log.info("reportDetected") synchronized(listeners) { for ((_, listener) in listeners) { GlobalScope.launch { diff --git a/sdk-android-library/src/main/java/network/xyo/ble/scanner/XYSmartScanModern.kt b/sdk-android-library/src/main/java/network/xyo/ble/scanner/XYSmartScanModern.kt index 3a4d8ffe..647de492 100644 --- a/sdk-android-library/src/main/java/network/xyo/ble/scanner/XYSmartScanModern.kt +++ b/sdk-android-library/src/main/java/network/xyo/ble/scanner/XYSmartScanModern.kt @@ -14,7 +14,7 @@ import java.util.* @TargetApi(21) class XYSmartScanModern(context: Context) : XYSmartScan(context) { override suspend fun start(): Boolean { - logInfo("start") + log.info("start") super.start() val result = asyncBle { @@ -22,13 +22,13 @@ class XYSmartScanModern(context: Context) : XYSmartScan(context) { val bluetoothAdapter = bluetoothManager?.adapter bluetoothAdapter.guard { - logInfo("Bluetooth Disabled") + log.info("Bluetooth Disabled") return@asyncBle XYBluetoothResult(false) } val scanner = bluetoothAdapter?.bluetoothLeScanner if (scanner == null) { - logInfo("startScan:Failed to get Bluetooth Scanner. Disabled?") + log.info("startScan:Failed to get Bluetooth Scanner. Disabled?") return@asyncBle XYBluetoothResult(false) } else { val filters = ArrayList() @@ -47,7 +47,7 @@ class XYSmartScanModern(context: Context) : XYSmartScan(context) { private val callback = object : ScanCallback() { override fun onBatchScanResults(results: MutableList?) { super.onBatchScanResults(results) - //logInfo("onBatchScanResults: $results") + //log.info("onBatchScanResults: $results") results.guard { return } val xyResults = ArrayList() for (result in results!!) { @@ -58,7 +58,7 @@ class XYSmartScanModern(context: Context) : XYSmartScan(context) { override fun onScanFailed(errorCode: Int) { super.onScanFailed(errorCode) - logError("onScanFailed: ${errorCode}, ${codeToScanFailed(errorCode)}", false) + log.error("onScanFailed: ${errorCode}, ${codeToScanFailed(errorCode)}", false) if (ScanCallback.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED == errorCode) { restartBluetooth() } @@ -66,7 +66,7 @@ class XYSmartScanModern(context: Context) : XYSmartScan(context) { override fun onScanResult(callbackType: Int, result: ScanResult?) { super.onScanResult(callbackType, result) - //logInfo("onBatchScanResults: $result") + //log.info("onBatchScanResults: $result") result.guard { return } val xyResults = ArrayList() xyResults.add(XYScanResultModern(result!!)) @@ -79,19 +79,19 @@ class XYSmartScanModern(context: Context) : XYSmartScan(context) { } override suspend fun stop(): Boolean { - logInfo("stop") + log.info("stop") super.stop() val result = asyncBle { val bluetoothAdapter = this@XYSmartScanModern.bluetoothAdapter if (bluetoothAdapter == null) { - logInfo("stop: Bluetooth Disabled") + log.info("stop: Bluetooth Disabled") return@asyncBle XYBluetoothResult(false) } val scanner = bluetoothAdapter.bluetoothLeScanner if (scanner == null) { - logInfo("stop:Failed to get Bluetooth Scanner. Disabled?") + log.info("stop:Failed to get Bluetooth Scanner. Disabled?") return@asyncBle XYBluetoothResult(false) } diff --git a/sdk-android-library/src/main/java/network/xyo/ble/services/Service.kt b/sdk-android-library/src/main/java/network/xyo/ble/services/Service.kt index 8447af54..37000e6a 100644 --- a/sdk-android-library/src/main/java/network/xyo/ble/services/Service.kt +++ b/sdk-android-library/src/main/java/network/xyo/ble/services/Service.kt @@ -73,7 +73,7 @@ abstract class Service(val device: XYBluetoothDevice) : XYBase() { private fun writeInt(characteristic: UUID, value: Int, formatType: Int = BluetoothGattCharacteristic.FORMAT_UINT8, offset:Int = 0): Deferred> { return device.connectionWithResult { - logInfo("writeInt: connection") + log.info("writeInt: connection") return@connectionWithResult device.findAndWriteCharacteristic( serviceUuid, characteristic, diff --git a/sdk-android-sample/build.gradle b/sdk-android-sample/build.gradle index fab3bd58..62adba6f 100644 --- a/sdk-android-sample/build.gradle +++ b/sdk-android-sample/build.gradle @@ -74,6 +74,7 @@ dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') api "com.google.android.material:material:1.0.0" api "androidx.coordinatorlayout:coordinatorlayout:1.0.0" + api "com.nabinbhandari.android:permissions:3.8" localImplementation project(':xycore') jitpackApi 'com.github.XYOracleNetwork:sdk-core-android:1.0.3' diff --git a/sdk-android-sample/src/main/java/network/xyo/ble/sample/XYApplication.kt b/sdk-android-sample/src/main/java/network/xyo/ble/sample/XYApplication.kt index a7c32cd9..71b2798a 100644 --- a/sdk-android-sample/src/main/java/network/xyo/ble/sample/XYApplication.kt +++ b/sdk-android-sample/src/main/java/network/xyo/ble/sample/XYApplication.kt @@ -41,10 +41,12 @@ class XYApplication : Application() { } override fun onTerminate() { - XYBase.logInfo("XYApplication", "onTerminate") + log.info("onTerminate") GlobalScope.async { scanner.stop() } super.onTerminate() } + + companion object: XYBase() } \ No newline at end of file diff --git a/sdk-android-sample/src/main/java/network/xyo/ble/sample/activities/XYOBleSampleActivity.kt b/sdk-android-sample/src/main/java/network/xyo/ble/sample/activities/XYOBleSampleActivity.kt index 152040e3..795cfa0b 100644 --- a/sdk-android-sample/src/main/java/network/xyo/ble/sample/activities/XYOBleSampleActivity.kt +++ b/sdk-android-sample/src/main/java/network/xyo/ble/sample/activities/XYOBleSampleActivity.kt @@ -16,15 +16,16 @@ import network.xyo.ble.devices.XYFinderBluetoothDevice import network.xyo.ble.sample.R import network.xyo.ble.sample.adapters.XYDeviceAdapter import network.xyo.ble.scanner.XYSmartScan -import network.xyo.core.XYPermissions import network.xyo.ui.ui +import com.nabinbhandari.android.permissions.PermissionHandler +import com.nabinbhandari.android.permissions.Permissions class XYOBleSampleActivity : XYOAppBaseActivity() { private var adapter: BaseAdapter? = null override fun onCreate(savedInstanceState: Bundle?) { - logInfo("onCreate") + log.info("onCreate") super.onCreate(savedInstanceState) setContentView(R.layout.activity_xyo_ble_sample) @@ -93,16 +94,29 @@ class XYOBleSampleActivity : XYOAppBaseActivity() { } override fun onResume() { - logInfo("onResume") + log.info("onResume") super.onResume() connectListeners() - val permissions = XYPermissions(this) - permissions.requestPermission(android.Manifest.permission.ACCESS_FINE_LOCATION, - "Location services are needed to connection and track your finders.", - XYPermissions.LOCATION_PERMISSIONS_REQ_CODE) - - permissions.requestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, "Allow app to access your storage in order to load firmware files?", 0) + Permissions.check( + this, + Manifest.permission.ACCESS_FINE_LOCATION, + "Location services are needed to connection and track your finders.", + object : PermissionHandler() { + override fun onGranted() { + } + } + ) + + Permissions.check( + this, + Manifest.permission.WRITE_EXTERNAL_STORAGE, + "Allow app to access your storage in order to load firmware files?", + object : PermissionHandler() { + override fun onGranted() { + } + } + ) ui { adapter?.notifyDataSetChanged() } diff --git a/sdk-android-sample/src/main/java/network/xyo/ble/sample/activities/XYOFinderDeviceActivity.kt b/sdk-android-sample/src/main/java/network/xyo/ble/sample/activities/XYOFinderDeviceActivity.kt index b93e718a..e174f9ce 100644 --- a/sdk-android-sample/src/main/java/network/xyo/ble/sample/activities/XYOFinderDeviceActivity.kt +++ b/sdk-android-sample/src/main/java/network/xyo/ble/sample/activities/XYOFinderDeviceActivity.kt @@ -37,7 +37,7 @@ class XYOFinderDeviceActivity : XYOAppBaseActivity() { super.onCreate(savedInstanceState) val deviceHash = intent.getIntExtra(EXTRA_DEVICEHASH, 0) - logInfo("onCreate: $deviceHash") + log.info("onCreate: $deviceHash") device = scanner.devices[deviceHash] if (device == null) { showToast("Failed to Find Device") @@ -63,7 +63,7 @@ class XYOFinderDeviceActivity : XYOAppBaseActivity() { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) - logInfo(TAG, "onActivityResult requestCode: $requestCode") + log.info(TAG, "onActivityResult requestCode: $requestCode") val frag = (supportFragmentManager.findFragmentById(R.id.container) as FirmwareUpdateFragment?) frag?.onFileSelected(requestCode, resultCode, data) @@ -94,7 +94,7 @@ class XYOFinderDeviceActivity : XYOAppBaseActivity() { } override fun connectionStateChanged(device: XYBluetoothDevice, newState: Int) { - logInfo("connectionStateChanged: $newState") + log.info("connectionStateChanged: $newState") update() if (newState == 2) { showToast("Connected") @@ -132,7 +132,7 @@ class XYOFinderDeviceActivity : XYOAppBaseActivity() { } override fun connectionStateChanged(device: XYBluetoothDevice, newState: Int) { - logInfo("connectionStateChanged: $newState") + log.info("connectionStateChanged: $newState") update() if (newState == 2) { showToast("Connected") @@ -155,13 +155,13 @@ class XYOFinderDeviceActivity : XYOAppBaseActivity() { } private fun addListener() { - logInfo("addListener: $device") + log.info("addListener: $device") (device as? XY4BluetoothDevice)?.addListener(TAG, xy4DeviceListener) (device as? XY3BluetoothDevice)?.addListener(TAG, xy3DeviceListener) } private fun removeListener() { - logInfo("removeListener: $device") + log.info("removeListener: $device") (device as? XY4BluetoothDevice)?.removeListener(TAG) (device as? XY3BluetoothDevice)?.removeListener(TAG) } diff --git a/sdk-android-sample/src/main/java/network/xyo/ble/sample/fragments/FirmwareUpdateFragment.kt b/sdk-android-sample/src/main/java/network/xyo/ble/sample/fragments/FirmwareUpdateFragment.kt index 612606fa..ad6ed355 100644 --- a/sdk-android-sample/src/main/java/network/xyo/ble/sample/fragments/FirmwareUpdateFragment.kt +++ b/sdk-android-sample/src/main/java/network/xyo/ble/sample/fragments/FirmwareUpdateFragment.kt @@ -21,6 +21,7 @@ import network.xyo.ble.firmware.OtaUpdate import network.xyo.ble.sample.R import network.xyo.ui.ui import network.xyo.ble.sample.fragments.core.BackFragmentListener +import network.xyo.core.XYBase class FirmwareUpdateFragment : XYAppBaseFragment(), BackFragmentListener { @@ -37,7 +38,7 @@ class FirmwareUpdateFragment : XYAppBaseFragment(), BackFragmentListener { if (OtaFile.createFileDirectory()) { context?.getSharedPreferences("settings", Context.MODE_PRIVATE)?.edit()?.putBoolean("fileDirectoriesCreated", true)?.apply() } else { - logInfo(TAG, "Failed to create files directory") + log.info("Failed to create files directory") } } } @@ -101,7 +102,7 @@ class FirmwareUpdateFragment : XYAppBaseFragment(), BackFragmentListener { private val updateListener = object : OtaUpdate.Listener() { override fun updated(device: XYBluetoothDevice) { - logInfo("updateListener: updated") + log.info("updateListener: updated") updateInProgress = false ui { activity?.hideProgressSpinner() @@ -111,7 +112,7 @@ class FirmwareUpdateFragment : XYAppBaseFragment(), BackFragmentListener { } override fun failed(device: XYBluetoothDevice, error: String) { - logInfo("updateListener: failed: $error") + log.info("updateListener: failed: $error") updateInProgress = false val gattError = error.contains("133") @@ -130,7 +131,7 @@ class FirmwareUpdateFragment : XYAppBaseFragment(), BackFragmentListener { override fun progress(sent: Int, total: Int) { val txt = "sending chunk $sent of $total" - logInfo(txt) + log.info(txt) ui { tv_file_progress?.text = txt } @@ -184,7 +185,7 @@ class FirmwareUpdateFragment : XYAppBaseFragment(), BackFragmentListener { tv_file_progress?.text = getString(R.string.update_started) } - logInfo(TAG, "performUpdate started: $String") + log.info("performUpdate started: $String") (activity?.device as? XY4BluetoothDevice)?.updateFirmware(firmwareFileName!!, updateListener) } else { ui { showToast("Select a File first") } @@ -196,7 +197,7 @@ class FirmwareUpdateFragment : XYAppBaseFragment(), BackFragmentListener { // TODO - Why are we making this dependency? [AT] -- @Suppress("UNUSED_PARAMETER") fun onFileSelected(requestCode: Int, resultCode: Int, data: Intent?) { - logInfo(TAG, "onFileSelected requestCode: $requestCode") + log.info( "onFileSelected requestCode: $requestCode") data?.data.let { uri -> tv_file_name?.text = uri.toString() @@ -204,10 +205,7 @@ class FirmwareUpdateFragment : XYAppBaseFragment(), BackFragmentListener { } - companion object { - private val TAG = FirmwareUpdateFragment::class.java.simpleName - - fun newInstance() = - FirmwareUpdateFragment() + companion object: XYBase() { + fun newInstance() = FirmwareUpdateFragment() } } diff --git a/sdk-android-sample/src/main/java/network/xyo/ble/sample/fragments/InfoFragment.kt b/sdk-android-sample/src/main/java/network/xyo/ble/sample/fragments/InfoFragment.kt index e8fab292..88228c0f 100644 --- a/sdk-android-sample/src/main/java/network/xyo/ble/sample/fragments/InfoFragment.kt +++ b/sdk-android-sample/src/main/java/network/xyo/ble/sample/fragments/InfoFragment.kt @@ -16,6 +16,7 @@ import kotlinx.coroutines.launch import network.xyo.ble.devices.* import network.xyo.ble.gatt.XYBluetoothGatt import network.xyo.ble.sample.R +import network.xyo.core.XYBase import network.xyo.ui.ui @@ -61,7 +62,7 @@ class InfoFragment : XYAppBaseFragment(), View.OnClickListener, CompoundButton.O override fun onResume() { super.onResume() - logInfo("onResume: InfoFragment") + log.info("onResume: InfoFragment") updateAdList() updateUI() } @@ -72,7 +73,7 @@ class InfoFragment : XYAppBaseFragment(), View.OnClickListener, CompoundButton.O private fun updateUI() { ui { - logInfo("update") + log.info("update") if (activity?.device != null) { text_family.text = activity?.device?.name @@ -189,7 +190,7 @@ class InfoFragment : XYAppBaseFragment(), View.OnClickListener, CompoundButton.O } private fun find() { - logInfo("beepButton: got xyDevice") + log.info("beepButton: got xyDevice") ui { button_find?.isEnabled = false } @@ -204,7 +205,7 @@ class InfoFragment : XYAppBaseFragment(), View.OnClickListener, CompoundButton.O } private fun wake() { - logInfo("stayAwakeButton: onClick") + log.info("stayAwakeButton: onClick") ui { button_stay_awake?.isEnabled = false } @@ -223,7 +224,7 @@ class InfoFragment : XYAppBaseFragment(), View.OnClickListener, CompoundButton.O } private fun sleep() { - logInfo("fallAsleepButton: onClick") + log.info("fallAsleepButton: onClick") ui { button_fall_asleep?.isEnabled = false } @@ -242,7 +243,7 @@ class InfoFragment : XYAppBaseFragment(), View.OnClickListener, CompoundButton.O } private fun lock() { - logInfo("lockButton: onClick") + log.info("lockButton: onClick") ui { button_lock?.isEnabled = false } @@ -264,7 +265,7 @@ class InfoFragment : XYAppBaseFragment(), View.OnClickListener, CompoundButton.O } private fun unlock() { - logInfo("unlockButton: onClick") + log.info("unlockButton: onClick") ui { button_unlock?.isEnabled = false } @@ -288,11 +289,11 @@ class InfoFragment : XYAppBaseFragment(), View.OnClickListener, CompoundButton.O private fun updateStayAwakeEnabledStates(): Deferred { return GlobalScope.async { - logInfo("updateStayAwakeEnabledStates") + log.info("updateStayAwakeEnabledStates") val xy4 = activity?.device as? XY4BluetoothDevice if (xy4 != null) { val stayAwake = xy4.primary.stayAwake.get().await() - logInfo("updateStayAwakeEnabledStates: ${stayAwake.value}") + log.info("updateStayAwakeEnabledStates: ${stayAwake.value}") ui { this@InfoFragment.isVisible.let { if (stayAwake.value != 0) { @@ -305,7 +306,7 @@ class InfoFragment : XYAppBaseFragment(), View.OnClickListener, CompoundButton.O } } } else { - logError("updateStayAwakeEnabledStates: Not an XY4!", false) + log.error("updateStayAwakeEnabledStates: Not an XY4!", false) } return@async } @@ -346,12 +347,12 @@ class InfoFragment : XYAppBaseFragment(), View.OnClickListener, CompoundButton.O //it is possible that reading the lock value is not implemented in the firmware private fun updateLockValue(): Deferred { return GlobalScope.async { - logInfo("updateLockValue") + log.info("updateLockValue") val xy4 = activity?.device as? XY4BluetoothDevice if (xy4 != null) { val lock = xy4.primary.lock.get().await() - logInfo("updateLock: $lock.value") + log.info("updateLock: $lock.value") ui { this@InfoFragment.isVisible.let { if (lock.error != null) { @@ -388,18 +389,16 @@ class InfoFragment : XYAppBaseFragment(), View.OnClickListener, CompoundButton.O val text = "Hello+$i" val write = xy4.primary.lock.set(XY4BluetoothDevice.DefaultLockCode).await() if (write.error == null) { - logInfo("testXy4: Success: $text") + log.info("testXy4: Success: $text") } else { - logInfo("testXy4: Fail: $text : ${write.error}") + log.info("testXy4: Fail: $text : ${write.error}") } } } } } - companion object { - - fun newInstance() = - InfoFragment() + companion object: XYBase() { + fun newInstance() = InfoFragment() } } diff --git a/sdk-android-sample/src/main/java/network/xyo/ble/sample/fragments/XYAppBaseFragmentt.kt b/sdk-android-sample/src/main/java/network/xyo/ble/sample/fragments/XYAppBaseFragmentt.kt index 6e35ebab..fad6ae0d 100644 --- a/sdk-android-sample/src/main/java/network/xyo/ble/sample/fragments/XYAppBaseFragmentt.kt +++ b/sdk-android-sample/src/main/java/network/xyo/ble/sample/fragments/XYAppBaseFragmentt.kt @@ -2,6 +2,7 @@ package network.xyo.ble.sample.fragments import android.content.Context import network.xyo.ble.sample.activities.XYOFinderDeviceActivity +import network.xyo.core.XYBase import network.xyo.ui.XYBaseFragment import network.xyo.ui.ui @@ -15,7 +16,7 @@ abstract class XYAppBaseFragment : XYBaseFragment() { if (context is XYOFinderDeviceActivity) { activity = context } else { - logError(TAG, "context is not instance of XYOFinderDeviceActivity!", true) + log.error("context is not instance of XYOFinderDeviceActivity!", true) } } @@ -37,7 +38,5 @@ abstract class XYAppBaseFragment : XYBaseFragment() { } } - companion object { - private val TAG = XYAppBaseFragment::class.java.simpleName - } + companion object: XYBase() } \ No newline at end of file diff --git a/sdk-android-sample/src/main/java/network/xyo/ble/sample/views/XYBLEStatsView.kt b/sdk-android-sample/src/main/java/network/xyo/ble/sample/views/XYBLEStatsView.kt index 98f7e8a8..b9996f19 100644 --- a/sdk-android-sample/src/main/java/network/xyo/ble/sample/views/XYBLEStatsView.kt +++ b/sdk-android-sample/src/main/java/network/xyo/ble/sample/views/XYBLEStatsView.kt @@ -31,7 +31,7 @@ class XYBLEStatsView(context: Context, attrs: AttributeSet) : LinearLayout(conte } override fun connectionStateChanged(device: XYBluetoothDevice, newState: Int) { - XYBase.logInfo(TAG, "connectionStateChanged") + log.info("connectionStateChanged") } override fun statusChanged(status: XYSmartScan.Status) { @@ -41,7 +41,7 @@ class XYBLEStatsView(context: Context, attrs: AttributeSet) : LinearLayout(conte init { scanner = (context.applicationContext as XYApplication).scanner - scanner.addListener(TAG, smartScanListener) + scanner.addListener("XYBLEStatsView", smartScanListener) } fun update() { @@ -53,7 +53,5 @@ class XYBLEStatsView(context: Context, attrs: AttributeSet) : LinearLayout(conte } } - companion object { - private val TAG = XYBLEStatsView::class.java.simpleName - } + companion object: XYBase() } diff --git a/sdk-android-sample/src/main/java/network/xyo/ble/sample/views/XYDeviceItemView.kt b/sdk-android-sample/src/main/java/network/xyo/ble/sample/views/XYDeviceItemView.kt index 5460c57e..30d1e908 100644 --- a/sdk-android-sample/src/main/java/network/xyo/ble/sample/views/XYDeviceItemView.kt +++ b/sdk-android-sample/src/main/java/network/xyo/ble/sample/views/XYDeviceItemView.kt @@ -80,7 +80,7 @@ class XYDeviceItemView(context: Context, attrs: AttributeSet) : RelativeLayout(c } override fun connectionStateChanged(device: XYBluetoothDevice, newState: Int) { - XYBase.logInfo(TAG,"connectionStateChanged") + log.info(TAG,"connectionStateChanged") } override fun buttonSinglePressed(device: XYFinderBluetoothDevice) { @@ -93,7 +93,7 @@ class XYDeviceItemView(context: Context, attrs: AttributeSet) : RelativeLayout(c if (device != null) { device.removeListener(TAG) } else { - XYBase.logError(TAG, "Setting NULL device") + log.error("Setting NULL device") } this.device = device @@ -102,7 +102,7 @@ class XYDeviceItemView(context: Context, attrs: AttributeSet) : RelativeLayout(c update() } - companion object { + companion object : XYBase() { private val TAG = XYDeviceItemView::class.java.simpleName } } From a33586fa11859294be8f50e40f48296c81412e31 Mon Sep 17 00:00:00 2001 From: Arie Trouw Date: Sat, 29 Dec 2018 13:50:28 -0800 Subject: [PATCH 2/2] Dependencies Updated --- sdk-android-library/build.gradle | 2 +- sdk-android-sample/build.gradle | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/sdk-android-library/build.gradle b/sdk-android-library/build.gradle index f84fcba6..f191931c 100644 --- a/sdk-android-library/build.gradle +++ b/sdk-android-library/build.gradle @@ -95,7 +95,7 @@ dependencies { api "androidx.annotation:annotation:1.0.1" localImplementation project(':xycore') - jitpackApi 'com.github.XYOracleNetwork:sdk-core-android:1.0.3' + jitpackApi 'com.github.XYOracleNetwork:sdk-core-android:1.1.0' implementation 'com.github.kotlin-graphics:kotlin-unsigned:v2.1' } diff --git a/sdk-android-sample/build.gradle b/sdk-android-sample/build.gradle index 62adba6f..d5f61e3f 100644 --- a/sdk-android-sample/build.gradle +++ b/sdk-android-sample/build.gradle @@ -77,10 +77,10 @@ dependencies { api "com.nabinbhandari.android:permissions:3.8" localImplementation project(':xycore') - jitpackApi 'com.github.XYOracleNetwork:sdk-core-android:1.0.3' + jitpackApi 'com.github.XYOracleNetwork:sdk-core-android:1.1.0' localImplementation project(':xyui') - jitpackApi 'com.github.XYOracleNetwork:sdk-ui-android:1.0.2' + jitpackApi 'com.github.XYOracleNetwork:sdk-ui-android:1.1.1' api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion" api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"