Skip to content

Commit

Permalink
Code Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
izivkov committed Apr 19, 2024
1 parent 17b3e5d commit f7de573
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 36 deletions.
2 changes: 1 addition & 1 deletion api/src/main/java/org/avmedia/gshockapi/ble/Connection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ object Connection {
bleManager?.setDataCallback(dataCallback)
}

fun write(handle: Int, data: ByteArray) {
fun write(handle: READ_WRITE_MODE, data: ByteArray) {
scope.launch {
bleManager?.write(handle, data)
}
Expand Down
28 changes: 16 additions & 12 deletions api/src/main/java/org/avmedia/gshockapi/ble/IGShockManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ enum class ConnectionState {
}

typealias onConnectedType = (String, String) -> Unit
enum class READ_WRITE_MODE {
WRITABLE_WITHOUT_RESPONSE,
WRITABLE_NOTIFIABLE
}

interface GSHock {
suspend fun connect(device: BluetoothDevice, onConnected: (String, String) -> Unit)
fun release()
fun setDataCallback(dataCallback: IDataReceived?)
fun enableNotifications()
suspend fun write(handle: Int, data: ByteArray)
abstract var connectionState: ConnectionState
suspend fun write(handle: READ_WRITE_MODE, data: ByteArray)
}

class IGShockManager(
Expand All @@ -38,8 +42,8 @@ private class GShockManagerImpl(
context: Context,
) : BleManager(context), GSHock {

private lateinit var readCharacteristicVar: BluetoothGattCharacteristic
private lateinit var writeCharacteristicVar: BluetoothGattCharacteristic
private lateinit var readCharacteristicHolder: BluetoothGattCharacteristic
private lateinit var writeCharacteristicHolder: BluetoothGattCharacteristic
var dataReceivedCallback: IDataReceived? = null
private lateinit var device: BluetoothDevice
override var connectionState = ConnectionState.DISCONNECTED
Expand All @@ -51,7 +55,7 @@ private class GShockManagerImpl(

override fun initialize() {
super.initialize()
setNotificationCallback(writeCharacteristicVar).with { _, data ->
setNotificationCallback(writeCharacteristicHolder).with { _, data ->

fun ByteArray.toHexString(): String =
joinToString(separator = " ", prefix = "0x") { String.format("%02X", it) }
Expand All @@ -62,7 +66,7 @@ private class GShockManagerImpl(
dataReceivedCallback?.dataReceived(hexData)
}

enableNotifications(writeCharacteristicVar).enqueue()
enableNotifications(writeCharacteristicHolder).enqueue()
ProgressEvents.onNext("BleManagerInitialized")
}

Expand Down Expand Up @@ -98,7 +102,7 @@ private class GShockManagerImpl(
}

override fun enableNotifications() {
enableNotifications(writeCharacteristicVar)
enableNotifications(writeCharacteristicHolder)
.fail { _, status ->
// Handle failure to enable notifications
Timber.i("Failed to enable notifications. Status: $status")
Expand Down Expand Up @@ -157,11 +161,11 @@ private class GShockManagerImpl(
@SuppressLint("NewApi", "MissingPermission")
override fun isRequiredServiceSupported(gatt: BluetoothGatt): Boolean {
gatt.getService(CasioConstants.WATCH_FEATURES_SERVICE_UUID)?.apply {
readCharacteristicVar = getCharacteristic(
readCharacteristicHolder = getCharacteristic(
CasioConstants.CASIO_READ_REQUEST_FOR_ALL_FEATURES_CHARACTERISTIC_UUID,
)

writeCharacteristicVar = getCharacteristic(
writeCharacteristicHolder = getCharacteristic(
CasioConstants.CASIO_ALL_FEATURES_CHARACTERISTIC_UUID,
)
return true
Expand Down Expand Up @@ -191,16 +195,16 @@ private class GShockManagerImpl(
|------00002902-0000-1000-8000-00805f9b34fb: EMPTY
*/

override suspend fun write(handle: Int, data: ByteArray) {
override suspend fun write(handle: READ_WRITE_MODE, data: ByteArray) {

if (!this::readCharacteristicVar.isInitialized || !this::writeCharacteristicVar.isInitialized) {
if (!this::readCharacteristicHolder.isInitialized || !this::writeCharacteristicHolder.isInitialized) {
ProgressEvents.onNext("ApiError", "Connection failed. Please try again.")
disconnect()
return
}

val characteristic = if (handle == 0xC) readCharacteristicVar else writeCharacteristicVar
val writeType = if (handle == 0xC) BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE else BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT
val characteristic = if (handle == READ_WRITE_MODE.WRITABLE_WITHOUT_RESPONSE) readCharacteristicHolder else writeCharacteristicHolder
val writeType = if (handle == READ_WRITE_MODE.WRITABLE_WITHOUT_RESPONSE) BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE else BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT

writeCharacteristic(
characteristic,
Expand Down
9 changes: 5 additions & 4 deletions api/src/main/java/org/avmedia/gshockapi/io/AlarmsIO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.google.gson.Gson
import kotlinx.coroutines.CompletableDeferred
import org.avmedia.gshockapi.Alarm
import org.avmedia.gshockapi.ble.Connection
import org.avmedia.gshockapi.ble.READ_WRITE_MODE
import org.avmedia.gshockapi.casio.Alarms
import org.avmedia.gshockapi.casio.CasioConstants
import org.avmedia.gshockapi.utils.Utils
Expand Down Expand Up @@ -70,23 +71,23 @@ object AlarmsIO {
fun sendToWatch(message: String) {
// get alarm 1
CasioIO.writeCmd(
0x000c,
READ_WRITE_MODE.WRITABLE_WITHOUT_RESPONSE,
Utils.byteArray(CasioConstants.CHARACTERISTICS.CASIO_SETTING_FOR_ALM.code.toByte())
)

// get the rest of the alarms
CasioIO.writeCmd(
0x000c,
READ_WRITE_MODE.WRITABLE_WITHOUT_RESPONSE,
Utils.byteArray(CasioConstants.CHARACTERISTICS.CASIO_SETTING_FOR_ALM2.code.toByte())
)
}

fun sendToWatchSet(message: String) {
val alarmsJsonArr: JSONArray = JSONObject(message).get("value") as JSONArray
val alarmCasio0 = Alarms.fromJsonAlarmFirstAlarm(alarmsJsonArr[0] as JSONObject)
CasioIO.writeCmd(0x000e, alarmCasio0)
CasioIO.writeCmd(READ_WRITE_MODE.WRITABLE_NOTIFIABLE, alarmCasio0)
val alarmCasio: ByteArray = Alarms.fromJsonAlarmSecondaryAlarms(alarmsJsonArr)
CasioIO.writeCmd(0x000e, alarmCasio)
CasioIO.writeCmd(READ_WRITE_MODE.WRITABLE_NOTIFIABLE, alarmCasio)
}

object AlarmDecoder {
Expand Down
3 changes: 2 additions & 1 deletion api/src/main/java/org/avmedia/gshockapi/io/AppInfoIO.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.avmedia.gshockapi.io

import kotlinx.coroutines.CompletableDeferred
import org.avmedia.gshockapi.ble.READ_WRITE_MODE
import org.avmedia.gshockapi.utils.Utils

object AppInfoIO {
Expand Down Expand Up @@ -31,7 +32,7 @@ object AppInfoIO {
// In this case, set it to the hardcoded value bellow, so 'D' button will work again.
val appInfoCompactStr = Utils.toCompactString(data)
if (appInfoCompactStr == "22FFFFFFFFFFFFFFFFFFFF00") {
CasioIO.writeCmd(0xE, "223488F4E5D5AFC829E06D02")
CasioIO.writeCmd(READ_WRITE_MODE.WRITABLE_NOTIFIABLE, "223488F4E5D5AFC829E06D02")
}
}

Expand Down
10 changes: 5 additions & 5 deletions api/src/main/java/org/avmedia/gshockapi/io/CasioIO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ package org.avmedia.gshockapi.io

import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothGattCharacteristic
import org.avmedia.gshockapi.ProgressEvents
import org.avmedia.gshockapi.ble.Connection
import org.avmedia.gshockapi.ble.READ_WRITE_MODE
import java.util.UUID

object CasioIO {
Expand All @@ -24,22 +24,22 @@ object CasioIO {
enum class DTS_STATE(val state: Int) { ZERO(0), TWO(2), FOUR(4) }

fun request(request: String) {
writeCmd(0xC, request)
writeCmd(READ_WRITE_MODE.WRITABLE_WITHOUT_RESPONSE, request)
}

fun init() {
}

fun writeCmd(handle: Int, bytesArray: ByteArray) {
fun writeCmd(handle: READ_WRITE_MODE, bytesArray: ByteArray) {
Connection.write(handle, bytesArray)
}

fun writeCmd(handle: Int, cmd: String) {
fun writeCmd(handle: READ_WRITE_MODE, cmd: String) {
writeCmdFromString(handle, cmd)
}

/// new
private fun writeCmdFromString(handle: Int, bytesStr: String) {
private fun writeCmdFromString(handle: READ_WRITE_MODE, bytesStr: String) {
Connection.write(handle, toCasioCmd(bytesStr))
}

Expand Down
9 changes: 5 additions & 4 deletions api/src/main/java/org/avmedia/gshockapi/io/EventsIO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.google.gson.Gson
import kotlinx.coroutines.CompletableDeferred
import org.avmedia.gshockapi.Event
import org.avmedia.gshockapi.ble.Connection
import org.avmedia.gshockapi.ble.READ_WRITE_MODE
import org.avmedia.gshockapi.casio.*
import org.avmedia.gshockapi.utils.Utils
import org.avmedia.gshockapi.utils.Utils.getBooleanSafe
Expand Down Expand Up @@ -153,7 +154,7 @@ object EventsIO {
val reminderJson = remindersJsonArr.getJSONObject(element)
val title = ReminderEncoder.reminderTitleFromJson(reminderJson)
CasioIO.writeCmd(
0x000e, Utils.byteArrayOfInts(
READ_WRITE_MODE.WRITABLE_NOTIFIABLE, Utils.byteArrayOfInts(
CasioConstants.CHARACTERISTICS.CASIO_REMINDER_TITLE.code, index + 1
) + title
)
Expand All @@ -162,7 +163,7 @@ object EventsIO {
reminderTime += CasioConstants.CHARACTERISTICS.CASIO_REMINDER_TIME.code
reminderTime += index + 1
reminderTime += ReminderEncoder.reminderTimeFromJson(reminderJson)
CasioIO.writeCmd(0x000e, Utils.byteArrayOfIntArray(reminderTime))
CasioIO.writeCmd(READ_WRITE_MODE.WRITABLE_NOTIFIABLE, Utils.byteArrayOfIntArray(reminderTime))
}

Timber.i("Got reminders $remindersJsonArr")
Expand All @@ -172,13 +173,13 @@ object EventsIO {
var index = 1
repeat(5) {
CasioIO.writeCmd(
0x000e, Utils.byteArrayOfInts(
READ_WRITE_MODE.WRITABLE_NOTIFIABLE, Utils.byteArrayOfInts(
CasioConstants.CHARACTERISTICS.CASIO_REMINDER_TITLE.code, index
) + ByteArray(18)
)

CasioIO.writeCmd(
0x000e, Utils.byteArrayOfInts(
READ_WRITE_MODE.WRITABLE_NOTIFIABLE, Utils.byteArrayOfInts(
CasioConstants.CHARACTERISTICS.CASIO_REMINDER_TIME.code, index
) + ByteArray(9)
)
Expand Down
5 changes: 3 additions & 2 deletions api/src/main/java/org/avmedia/gshockapi/io/SettingsIO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.google.gson.Gson
import kotlinx.coroutines.CompletableDeferred
import org.avmedia.gshockapi.Settings
import org.avmedia.gshockapi.ble.Connection
import org.avmedia.gshockapi.ble.READ_WRITE_MODE
import org.avmedia.gshockapi.casio.CasioConstants
import org.avmedia.gshockapi.utils.Utils
import org.json.JSONObject
Expand Down Expand Up @@ -149,14 +150,14 @@ pwr. saving off:00010000
@Suppress("UNUSED_PARAMETER")
fun sendToWatch(message: String) {
CasioIO.writeCmd(
0x000c,
READ_WRITE_MODE.WRITABLE_WITHOUT_RESPONSE,
Utils.byteArray(CasioConstants.CHARACTERISTICS.CASIO_SETTING_FOR_BASIC.code.toByte())
)
}

fun sendToWatchSet(message: String) {
val settings = JSONObject(message).get("value") as JSONObject
CasioIO.writeCmd(0x000e, SettingsEncoder.encode(settings))
CasioIO.writeCmd(READ_WRITE_MODE.WRITABLE_NOTIFIABLE, SettingsEncoder.encode(settings))
}

object SettingsEncoder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.google.gson.Gson
import kotlinx.coroutines.CompletableDeferred
import org.avmedia.gshockapi.Settings
import org.avmedia.gshockapi.ble.Connection
import org.avmedia.gshockapi.ble.READ_WRITE_MODE
import org.avmedia.gshockapi.casio.CasioConstants
import org.avmedia.gshockapi.utils.Utils
import org.json.JSONObject
Expand Down Expand Up @@ -79,7 +80,7 @@ object TimeAdjustmentIO {
@Suppress("UNUSED_PARAMETER")
fun sendToWatch(message: String) {
CasioIO.writeCmd(
0x000c,
READ_WRITE_MODE.WRITABLE_WITHOUT_RESPONSE,
Utils.byteArray(CasioConstants.CHARACTERISTICS.CASIO_SETTING_FOR_BLE.code.toByte())
)
}
Expand All @@ -93,7 +94,7 @@ object TimeAdjustmentIO {
)
val encodedTimeAdj = encodeTimeAdjustment(settings)
if (encodedTimeAdj.isNotEmpty()) {
CasioIO.writeCmd(0x000e, encodedTimeAdj)
CasioIO.writeCmd(READ_WRITE_MODE.WRITABLE_NOTIFIABLE, encodedTimeAdj)
}
}

Expand Down
7 changes: 4 additions & 3 deletions api/src/main/java/org/avmedia/gshockapi/io/TimeIO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.os.Build
import androidx.annotation.RequiresApi
import org.avmedia.gshockapi.WatchInfo
import org.avmedia.gshockapi.ble.Connection
import org.avmedia.gshockapi.ble.READ_WRITE_MODE
import org.avmedia.gshockapi.casio.CasioConstants
import org.avmedia.gshockapi.casio.CasioTimeZoneHelper
import org.avmedia.gshockapi.utils.Utils
Expand Down Expand Up @@ -134,7 +135,7 @@ object TimeIO {
private suspend fun <T> readAndWrite(function: KSuspendFunction1<T, String>, param: T) {
val ret: String = function(param)
val shortStr = Utils.toCompactString(ret)
CasioIO.writeCmd(0xE, shortStr)
CasioIO.writeCmd(READ_WRITE_MODE.WRITABLE_NOTIFIABLE, shortStr)
}

private suspend fun writeDST() {
Expand Down Expand Up @@ -200,7 +201,7 @@ object TimeIO {
suspend fun <T> readAndWrite(function: KSuspendFunction1<T, String>, param: T) {
val ret: String = function(param)
val shortStr = Utils.toCompactString(ret)
CasioIO.writeCmd(0xE, shortStr)
CasioIO.writeCmd(READ_WRITE_MODE.WRITABLE_NOTIFIABLE, shortStr)
}

readAndWrite(::getDSTWatchStateWithTZ, CasioIO.DTS_STATE.ZERO)
Expand All @@ -226,7 +227,7 @@ object TimeIO {
val timeCommand =
Utils.byteArrayOfInts(CasioConstants.CHARACTERISTICS.CASIO_CURRENT_TIME.code) + timeData

CasioIO.writeCmd(0x000e, timeCommand)
CasioIO.writeCmd(READ_WRITE_MODE.WRITABLE_NOTIFIABLE, timeCommand)
}

object TimeEncoder {
Expand Down
5 changes: 3 additions & 2 deletions api/src/main/java/org/avmedia/gshockapi/io/TimerIO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.os.Build
import androidx.annotation.RequiresApi
import kotlinx.coroutines.CompletableDeferred
import org.avmedia.gshockapi.ble.Connection
import org.avmedia.gshockapi.ble.READ_WRITE_MODE
import org.avmedia.gshockapi.casio.CasioConstants
import org.avmedia.gshockapi.utils.Utils
import org.json.JSONObject
Expand Down Expand Up @@ -41,14 +42,14 @@ object TimerIO {
@Suppress("UNUSED_PARAMETER")
fun sendToWatch(message: String) {
CasioIO.writeCmd(
0x000c,
READ_WRITE_MODE.WRITABLE_WITHOUT_RESPONSE,
Utils.byteArray(CasioConstants.CHARACTERISTICS.CASIO_TIMER.code.toByte())
)
}

fun sendToWatchSet(message: String) {
val seconds = JSONObject(message).get("value").toString()
CasioIO.writeCmd(0x000e, TimerEncoder.encode(seconds))
CasioIO.writeCmd(READ_WRITE_MODE.WRITABLE_NOTIFIABLE, TimerEncoder.encode(seconds))
}

object TimerDecoder {
Expand Down

0 comments on commit f7de573

Please sign in to comment.