Skip to content

Commit

Permalink
refactor: clean android code and added docs string in kotlin ble_module.
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhman-sukh committed Sep 1, 2024
1 parent 1bf958b commit af1b0e6
Show file tree
Hide file tree
Showing 98 changed files with 203 additions and 322 deletions.
2 changes: 1 addition & 1 deletion docs/qaul/commit-guidelines.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ For automated code documentation generation, use triple slashes `///` line comme
Each code file needs to start with the qaul license:

```rs
// Copyright (c) 2021 Open Community Project Association https://ocpa.ch
// Copyright (c) 2023 Open Community Project Association https://ocpa.ch
// This software is published under the AGPLv3 license.
```
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 Open Community Project Association https://ocpa.ch
// Copyright (c) 2023 Open Community Project Association https://ocpa.ch
// This software is published under the AGPLv3 license.

package net.qaul.ble
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 Open Community Project Association https://ocpa.ch
// Copyright (c) 2023 Open Community Project Association https://ocpa.ch
// This software is published under the AGPLv3 license.

package net.qaul.ble
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 Open Community Project Association https://ocpa.ch
// Copyright (c) 2023 Open Community Project Association https://ocpa.ch
// This software is published under the AGPLv3 license.

package net.qaul.ble
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 Open Community Project Association https://ocpa.ch
// Copyright (c) 2023 Open Community Project Association https://ocpa.ch
// This software is published under the AGPLv3 license.

package net.qaul.ble
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 Open Community Project Association https://ocpa.ch
// Copyright (c) 2023 Open Community Project Association https://ocpa.ch
// This software is published under the AGPLv3 license.

package net.qaul.ble.callback
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright (c) 2021 Open Community Project Association https://ocpa.ch
// Copyright (c) 2023 Open Community Project Association https://ocpa.ch
// This software is published under the AGPLv3 license.

/**
* BleActor is an instance of GATT Client which is used to connect to other GATT Servers.
*/
package net.qaul.ble.core

import android.annotation.SuppressLint
Expand Down Expand Up @@ -59,19 +62,12 @@ class BleActor(private val mContext: Context, var listener: BleConnectionListene
*/
fun setDevice(device: BLEScanDevice?, isFromMessage: Boolean) {
this.isFromMessage = isFromMessage
// if (mBluetoothGatt != null && !isFromMessage) {
bleDevice = device
bluetoothDevice = device!!.bluetoothDevice

Handler(Looper.getMainLooper()).postDelayed({
connectDevice()
}, 500)

// } else {
// Handler(Looper.getMainLooper()).postDelayed({
// send(BLEUtils.byteToHex(tempData))
// }, 500)
// }
}

/**
Expand Down Expand Up @@ -122,7 +118,6 @@ class BleActor(private val mContext: Context, var listener: BleConnectionListene
closeGatt()
cancelTimer()
if (descriptorWriteQueue != null && descriptorWriteQueue.size > 0) descriptorWriteQueue.clear()
// listener!!.onDisconnected(bleDevice!!)
if (!disconnectedFromDevice) listener!!.onDisconnected(bleDevice!!) else disconnectedFromDevice =
false
if (isFromMessage) {
Expand All @@ -149,14 +144,7 @@ class BleActor(private val mContext: Context, var listener: BleConnectionListene
TAG,
"onCharacteristicRead : " + characteristic.uuid.toString() + " , value -> $data"
)


if (isFromMessage) {
// AppLog.e(
// TAG,
// "tempData -> $tempData"
// )
// gatt.requestMtu(180)
send(BLEUtils.byteToHex(tempData))
return
}
Expand All @@ -169,21 +157,17 @@ class BleActor(private val mContext: Context, var listener: BleConnectionListene
if (characteristic.uuid.toString()
.lowercase() == BleService.READ_CHAR.lowercase() && !isFromMessage
) {
// disConnectedDevice() //onCharacteristicRead
// disConnectedDevice()
}
}


/**
* This method keeps on sending the data to the device until the queue is empty
*/
override fun onCharacteristicWrite(
gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic, status: Int
) {
super.onCharacteristicWrite(gatt, characteristic, status)
// AppLog.e(
// TAG,
// "onCharacteristicWrite -------------->, data : " + BLEUtils.byteToHex(
// characteristic.value
// )
// )
if (listener != null) {
if (messageId.isEmpty() || messageId.isBlank()) {
listener!!.onCharacteristicWrite(gatt = gatt, characteristic = characteristic)
Expand All @@ -193,7 +177,7 @@ class BleActor(private val mContext: Context, var listener: BleConnectionListene
listener!!.onMessageSent(
gatt = gatt, value = tempData, id = messageId
)
disConnectedDevice() //onCharacteristicWrite
disConnectedDevice()
tempData = ByteArray(0)
}
}
Expand All @@ -215,12 +199,14 @@ class BleActor(private val mContext: Context, var listener: BleConnectionListene
}
}

// Descriptor method (Unused for this architecture)
override fun onDescriptorRead(
gatt: BluetoothGatt, descriptor: BluetoothGattDescriptor, status: Int
) {
super.onDescriptorRead(gatt, descriptor, status)
}

// Descriptor method (Unused for this architecture)
override fun onDescriptorWrite(
gatt: BluetoothGatt, descriptor: BluetoothGattDescriptor, status: Int
) {
Expand All @@ -230,21 +216,10 @@ class BleActor(private val mContext: Context, var listener: BleConnectionListene
if (descriptorWriteQueue.size > 0) writeGattDescriptor(descriptorWriteQueue.element())
else {
if (listener != null) {
// AppLog.e(TAG, "onDescriptorWrite asked")
listener!!.onDescriptorWrite(bleDevice!!, this@BleActor)
}
}
}
// if (isReconnect && isFromMessage) {
// AppLog.e(TAG, "onDescriptorWrite asked")
// writeServiceData(BleService.SERVICE_UUID, BleService.MSG_CHAR, tempData, attempt)
// attempt = 0
// tempData = ByteArray(0)
// isReconnect = false
// } else if (isFromMessage) {
// writeServiceData(BleService.SERVICE_UUID, BleService.MSG_CHAR, tempData, 0)
// tempData = ByteArray(0)
// }
}

override fun onMtuChanged(gatt: BluetoothGatt?, mtu: Int, status: Int) {
Expand All @@ -268,6 +243,12 @@ class BleActor(private val mContext: Context, var listener: BleConnectionListene
}
}

/**
* This method sends data to GATT Server and manages the bytes written.
* The android gatt client can write only 20 or less bytes at a time.
* So, here we use delimiters "$$" for every message before breaking into multiple and sending to the server
* On server side, it will concatenate the received bytes and check for the delimiters to form the complete message.
*/
fun send(data: String): Int {
AppLog.e(TAG, "send data-----------------> data $data")
var data = data
Expand All @@ -276,7 +257,6 @@ class BleActor(private val mContext: Context, var listener: BleConnectionListene
data = data.substring(40)
}
if (data.isNotEmpty()) sendQueue.add(data)
// AppLog.e(TAG, "sendQueue.size: " + sendQueue.size)
if(sendQueue.size > 0) {
if (!isWriting) _send()
}
Expand All @@ -288,9 +268,7 @@ class BleActor(private val mContext: Context, var listener: BleConnectionListene
AppLog.e(TAG, "_send(): EMPTY QUEUE")
return false
}
// AppLog.e(TAG, "_send(): $attempt Sending: " + sendQueue.peek())
val tx = BLEUtils.hexToByteArray(sendQueue.poll())
// val tx = sendQueue.poll()?.toByteArray(Charset.forName("UTF-8"))
isWriting = true // Set the write in progress flag
if (!writeServiceData(BleService.SERVICE_UUID, BleService.MSG_CHAR, tx, attempt)) {
return false;
Expand Down Expand Up @@ -321,6 +299,8 @@ class BleActor(private val mContext: Context, var listener: BleConnectionListene
if (characteristics != null && characteristics.size > 0) {
for (i in characteristics.indices) {
val characteristic = characteristics[i]
// Below lines of code are useless and are always false for current architecture.
// Present to maintain code consistency.
if (characteristic != null && (isCharacteristicNotifiable(characteristic) || isCharacteristicIndicate(
characteristic
))
Expand All @@ -336,7 +316,6 @@ class BleActor(private val mContext: Context, var listener: BleConnectionListene
}
}
if (!isQaulDevice) {
// listener?.addToBlackList(this.bleDevice!!)
disConnectedDevice() //discoverServices
return
}
Expand All @@ -345,9 +324,11 @@ class BleActor(private val mContext: Context, var listener: BleConnectionListene
listener!!.onServiceDiscovered(bluetoothDevice!!.address)
}
}
// Descriptor write queue always empty for current architecture.
// It finally calls "readServiceData()" function to read read_char value from other qaul device.
if (descriptorWriteQueue.size > 0) {
// writeGattDescriptor(descriptorWriteQueue.element())
} else {
}
else {
if (listener != null) {
mBluetoothGatt
listener!!.onDescriptorWrite(this.bleDevice!!, this)
Expand Down Expand Up @@ -442,32 +423,22 @@ class BleActor(private val mContext: Context, var listener: BleConnectionListene
serUUID: String, charUUID: String, data: ByteArray?, attempt: Int
): Boolean {
if (attempt < 3) {
if (data != null) {
// AppLog.e(
// TAG,
// "writeServiceData -----------> : data : ${data}"
// )
if (data != null) {
if (mBluetoothGatt != null) {
val service = mBluetoothGatt!!.getService(UUID.fromString(serUUID))
if (service != null) {
val characteristic = service.getCharacteristic(UUID.fromString(charUUID))
if (characteristic != null) {

/**
* This is a workaround for Android_Sdk_version 33 or less devices.
* The latest API is more efficient and reliable for writing data to the device.
*/
if (Build.VERSION.SDK_INT >= 33) {
val returnValue = mBluetoothGatt!!.writeCharacteristic(characteristic, data, BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT)
// AppLog.e(
// TAG,
// "writeServiceData -----------> : data : ${data} , returnValue : $returnValue"
// )
return true
} else {
characteristic.value = data
return mBluetoothGatt!!.writeCharacteristic(characteristic)
// AppLog.e(
// TAG,
// "writeServiceData -----------> : data : ${data} , directSendResult : $directSendResult"
// )
// return directSendResult
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 Open Community Project Association https://ocpa.ch
// Copyright (c) 2023 Open Community Project Association https://ocpa.ch
// This software is published under the AGPLv3 license.

package net.qaul.ble.core
Expand Down Expand Up @@ -276,7 +276,6 @@ open class BleWrapperClass(context: Activity) {
directReceived.data = ByteString.copyFrom(msgObject.message)

AppLog.e(TAG, "---->onMessageReceived3---> ${directReceived.data}")
// ByteString.copyFrom(msgObject.message, Charset.defaultCharset())
bleRes.directReceived = directReceived.build()
// AppLog.e(TAG, "---->onMessageReceived msgObject $msgObject")
sendResponse(bleRes)
Expand Down Expand Up @@ -600,85 +599,13 @@ open class BleWrapperClass(context: Activity) {
/**
* Request User to Turn On Location
*/

private fun enableLocation(context: Activity, locationReqCode: Int) {
Log.i(TAG, "enableLocation()")

val intent = Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS
)
context.startActivityForResult(intent, locationReqCode)
//
// val googleApiClient: GoogleApiClient = GoogleApiClient.Builder(context)
// .addApi(LocationServices.API).build()
// googleApiClient.connect()
//
// val locationRequest: LocationRequest = LocationRequest.create()
// locationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
// locationRequest.interval = 10000
// locationRequest.fastestInterval = (10000 / 2).toLong()
//
// val builder: LocationSettingsRequest.Builder =
// LocationSettingsRequest.Builder().addLocationRequest(locationRequest)
// builder.setAlwaysShow(true)
// val activity1 = context
// val result: PendingResult<LocationSettingsResult> =
// LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build())
// result.setResultCallback { result ->
// val status = result.status
// when (status.statusCode) {
// LocationSettingsStatusCodes.SUCCESS -> AppLog.i(
// TAG,
// "All location settings are satisfied."
// )
// LocationSettingsStatusCodes.RESOLUTION_REQUIRED -> {
// AppLog.e(
// TAG,
// "Location settings are not satisfied. Show the user a dialog to upgrade location settings "
// )
// try {
//// Show the dialog by calling startResolutionForResult(), and check the result
//// in onActivityResult().
// status.startResolutionForResult(activity1, locationReqCode)
// } catch (e: IntentSender.SendIntentException) {
// AppLog.e(
// TAG,
// "PendingIntent unable to execute request."
// )
// }
// }
// LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE -> AppLog.e(
// TAG,
// "Location settings are inadequate, and cannot be fixed here. Dialog not created."
// )
// }
// }

// val locationRequest = LocationRequest.create()
// .setInterval(10 * 100000)
// .setFastestInterval(2 * 100000)
// .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
//
// val builder = LocationSettingsRequest.Builder()
// .addLocationRequest(locationRequest)
//
// LocationServices
// .getSettingsClient(context)
// .checkLocationSettings(builder.build())
// .addOnSuccessListener(context) { response: LocationSettingsResponse? -> }
// .addOnFailureListener(context) { ex ->
// if (ex is ResolvableApiException) {
// try {
// val intentSenderRequest =
// IntentSenderRequest.Builder((ex as ResolvableApiException).resolution)
// .build()
// context.startActivityForResult(intentSenderRequest.fillInIntent, locationReqCode)
// // resolutionForResult.launch(intentSenderRequest)
// } catch (exception: java.lang.Exception) {
// Log.d(TAG, "enableLocationSettings: $exception")
// }
// }
// }
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 Open Community Project Association https://ocpa.ch
// Copyright (c) 2023 Open Community Project Association https://ocpa.ch
// This software is published under the AGPLv3 license.

package net.qaul.ble.model
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 Open Community Project Association https://ocpa.ch
// Copyright (c) 2023 Open Community Project Association https://ocpa.ch
// This software is published under the AGPLv3 license.

package net.qaul.ble.model
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) 2023 Open Community Project Association https://ocpa.ch
// This software is published under the AGPLv3 license.

package net.qaul.ble.model

import com.google.gson.annotations.SerializedName
Expand Down
Loading

0 comments on commit af1b0e6

Please sign in to comment.