Skip to content

Commit

Permalink
Refactor the rust ble_module
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhman-sukh committed Jul 3, 2024
1 parent e6683a4 commit 439daab
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 161 deletions.
3 changes: 1 addition & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,5 @@
"[proto3]": {
"editor.defaultFormatter": "xaver.clang-format",
"editor.formatOnSave": true
},
"cmake.sourceDirectory": "/home/death/Desktop/linux_backup/openSource/gsoc/qaul.net/qaul_ui/linux"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ class BleActor(private val mContext: Context, var listener: BleConnectionListene
gatt: BluetoothGatt, descriptor: BluetoothGattDescriptor, status: Int
) {
super.onDescriptorWrite(gatt, descriptor, status)
AppLog.e(TAG, "onDescriptorWrite asked")
if (descriptorWriteQueue != null && descriptorWriteQueue.size > 0) {
descriptorWriteQueue.remove()
if (descriptorWriteQueue.size > 0) writeGattDescriptor(descriptorWriteQueue.element()) else {
Expand All @@ -230,7 +229,8 @@ class BleActor(private val mContext: Context, var listener: BleConnectionListene
}
}
// if (isReconnect && isFromMessage) {
// writeServiceData(BleService.SERVICE_UUID, BleService.MSG_CHAR, tempData, attempt)
// AppLog.e(TAG, "onDescriptorWrite asked")
writeServiceData(BleService.SERVICE_UUID, BleService.MSG_CHAR, tempData, attempt)
// attempt = 0
// tempData = ByteArray(0)
// isReconnect = false
Expand Down Expand Up @@ -262,7 +262,7 @@ class BleActor(private val mContext: Context, var listener: BleConnectionListene
}

fun send(data: String): Int {
AppLog.e(TAG, "send data-----------------> isWriting $isWriting data $data")
// AppLog.e(TAG, "send data-----------------> isWriting $isWriting data $data")
var data = data
while (data.length > 40) {
sendQueue.add(data.substring(0, 40))
Expand All @@ -278,10 +278,9 @@ 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())
// AppLog.e(TAG, "_send(): $attempt Sending: " + sendQueue.peek())
val tx = BLEUtils.hexToByteArray(sendQueue.poll())
// AppLog.e(TAG, "_send(): $attempt Sending: ")
// val tx = sendQueue.poll()?.toByteArray(Charset.forName("UTF-8"))
// val tx = sendQueue.poll()?.toByteArray(Charset.forName("UTF-8"))
isWriting = true // Set the write in progress flag
writeServiceData(BleService.SERVICE_UUID, BleService.MSG_CHAR, tx, attempt)
return true
Expand Down Expand Up @@ -348,7 +347,6 @@ class BleActor(private val mContext: Context, var listener: BleConnectionListene
* This method is used to write descriptor of gatt
*/
private fun writeGattDescriptor(d: BluetoothGattDescriptor) {
AppLog.e(TAG, "writeGattDescriptor request ")
if (isCharacteristicNotifiable(d.characteristic)) {
d.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE
} else {
Expand Down Expand Up @@ -443,7 +441,6 @@ class BleActor(private val mContext: Context, var listener: BleConnectionListene
val characteristic = service.getCharacteristic(UUID.fromString(charUUID))
if (characteristic != null) {
characteristic.value = data
AppLog.e(TAG, "===================Characterstic value = $data")
return mBluetoothGatt!!.writeCharacteristic(characteristic)
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ open class BleWrapperClass(context: Activity) {
val bleRes = BleOuterClass.Ble.newBuilder()
val directReceived = BleOuterClass.BleDirectReceived.newBuilder()
val msgData = String(message).removeSuffix("$$").removePrefix("$$")
AppLog.e(TAG, "---->onMessageReceived---> " + msgData)
val msgObject = Gson().fromJson(msgData, Message::class.java)
directReceived.from = ByteString.copyFrom(bleDevice.qaulId)
directReceived.data = ByteString.copyFrom(msgObject.message)
Expand Down Expand Up @@ -428,7 +427,6 @@ open class BleWrapperClass(context: Activity) {
Class.forName(className)
true
} catch (e: ClassNotFoundException) {
e.printStackTrace()
false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ class BleService : LifecycleService() {
)
// AppLog.e(TAG, "Write Request Received: " + String(value) + " :: " + requestId)
val s = BLEUtils.byteToHex(value)
AppLog.e(TAG, "Data in hex:: $s")
// AppLog.e(TAG, "Data in hex:: $s")
var bleDevice = ignoreList.find { it.macAddress == device.address }
if (bleDevice == null) {
bleDevice = receiveList.find { it.macAddress == device.address }
Expand All @@ -437,12 +437,9 @@ class BleService : LifecycleService() {
var oldValue = msgMap[device.address]
if (s.endsWith("2424") || (oldValue!!.endsWith("24") && s == "24")) {
//SendResponse of oldValue

AppLog.e(TAG, "onCharacteristicWriteRequest: contain 2424")
oldValue += s
val msgData = String(BLEUtils.hexToByteArray(oldValue)!!).removeSuffix("$$")
.removePrefix("$$")
Log.e(TAG, "Msg Data:: $msgData")
if (!msgData.contains("$$")) {
val msgObject = Gson().fromJson(msgData, Message::class.java)
if (bleDevice == null) {
Expand All @@ -460,7 +457,6 @@ class BleService : LifecycleService() {
Log.e(TAG, "onCharacteristicWriteRequest: contain $$")
}
} else {
AppLog.e(TAG, "onCharacteristicWriteRequest: not contain 2424")
oldValue += s
msgMap[device.address] = oldValue
}
Expand All @@ -469,7 +465,6 @@ class BleService : LifecycleService() {
//Send Response of s
val msgData = String(BLEUtils.hexToByteArray(s)!!).removeSuffix("$$")
.removePrefix("$$")
AppLog.e(TAG, "Got whole message at once $msgData")
val msgObject = Gson().fromJson(msgData, Message::class.java)
if (bleDevice == null) {
bleDevice = BLEScanDevice.getDevice()
Expand Down Expand Up @@ -843,7 +838,7 @@ class BleService : LifecycleService() {
AppLog.e(
TAG, "sendMessage ${BLEUtils.byteToHex(message)}"
)
// var mainQueue: Queue<Triple<String, ByteArray, ByteArray>>? = null
var mainQueue: Queue<Triple<String, ByteArray, ByteArray>>? = null
bleDevice?.let {
if (hashMap.containsKey(it.macAddress)) {
var queue = hashMap[it.macAddress!!]
Expand All @@ -853,14 +848,14 @@ class BleService : LifecycleService() {
queue = LinkedList()
}
hashMap[it.macAddress!!] = queue!!
// mainQueue = queue
mainQueue = queue
// AppLog.d(TAG, " Manual send ======= Queue size was already 1 ")
} else {
// AppLog.d(TAG, " Manual send ===== Queue size was empty ")
val queue: Queue<Triple<String, ByteArray, ByteArray>> = LinkedList()
queue.add(Triple(id, from, message))
hashMap[it.macAddress!!] = queue
// mainQueue = queue
mainQueue = queue
}
// AppLog.e(TAG, "device--> ${it.macAddress} ${mainQueue?.size}")
sendMessageFromQueu(it.macAddress!!, true)
Expand Down
2 changes: 1 addition & 1 deletion qaul_ui/lib/screens/home/tabs/users_tab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class _AvailableConnections extends StatelessWidget {
if (_hasInternet) ...[const Icon(CupertinoIcons.globe), space],
if (_hasLan) ...[const Icon(Icons.wifi), space],
if (_hasLocal) ...[const Icon(Icons.cable), space],
if (_hasBluetooth) const Icon(Icons.bluetooth),
if (_hasBluetooth) ...[const Icon(Icons.bluetooth)],
],
),
);
Expand Down
Loading

0 comments on commit 439daab

Please sign in to comment.