Skip to content

Commit

Permalink
Message Module Completed
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalbluepixel authored and MathJud committed Jan 11, 2022
1 parent c23bdee commit a1c459a
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 16 deletions.
20 changes: 14 additions & 6 deletions android/app/src/main/java/net/qaul/qaul/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ class MainActivity : AppCompatActivity(), BleRequestCallback {
directSend.id = System.currentTimeMillis().toString()
bleReq.directSend = directSend.build()
bleWrapperClass.receiveRequest(bleReq = bleReq.build(), callback = this)
runOnUiThread {
Toast.makeText(this, "Connecting...", Toast.LENGTH_SHORT).show()
}
}


Expand Down Expand Up @@ -274,13 +277,18 @@ class MainActivity : AppCompatActivity(), BleRequestCallback {
BleOuterClass.Ble.MessageCase.DIRECT_RECEIVED -> {
val directReceived: BleOuterClass.BleDirectReceived = ble.directReceived
AppLog.e("directReceived: ", Gson().toJson(directReceived))
val message: String =
String(directReceived.data!!.toByteArray()).removeSuffix("$$")
.removePrefix("$$")
val msgObject = Gson().fromJson(message, net.qaul.ble.model.Message::class.java)
val message: String = directReceived.data.toString(Charset.defaultCharset())
val qaulId: String = directReceived.qaulId.toString(Charset.defaultCharset())
runOnUiThread {
binding.tvMessage.text = message
binding.etQaulId.setText(qaulId)
}
}
BleOuterClass.Ble.MessageCase.DIRECT_SEND_RESULT -> {
val directSendResult: BleOuterClass.BleDirectSendResult = ble.directSendResult
AppLog.e("directSendResult: ", Gson().toJson(directSendResult))
runOnUiThread {
binding.tvMessage.text = msgObject.message
binding.etQaulId.setText(msgObject.qaulId)
Toast.makeText(this, directSendResult.errorMessage, Toast.LENGTH_SHORT).show()
}
}
else -> {
Expand Down
58 changes: 50 additions & 8 deletions android/blemodule/src/main/java/net/qaul/ble/core/BleActor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import android.content.Context
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.widget.Toast
import net.qaul.ble.AppLog
import net.qaul.ble.BLEUtils
import net.qaul.ble.model.BLEScanDevice
import net.qaul.ble.service.BleService
import java.lang.Exception
import java.util.*

class BleActor(private val mContext: Context, var listener: BleConnectionListener?) {
Expand All @@ -39,7 +39,7 @@ class BleActor(private val mContext: Context, var listener: BleConnectionListene
mBluetoothGatt!!.disconnect()
Handler(Looper.myLooper()!!).postDelayed({
mBluetoothGatt!!.close()
},200)
}, 200)
}
}

Expand Down Expand Up @@ -114,6 +114,15 @@ class BleActor(private val mContext: Context, var listener: BleConnectionListene
if (descriptorWriteQueue != null && descriptorWriteQueue.size > 0) descriptorWriteQueue.clear()
if (!disconnectedFromDevice) listener!!.onDisconnected(bleDevice!!) else disconnectedFromDevice =
false
if (isFromMessage) {
if (mBluetoothGatt != null) {
BleService.bleService!!.bleCallback?.onMessageSent(
id = messageId,
success = false,
data = tempData
)
}
}
}
}

Expand Down Expand Up @@ -142,7 +151,9 @@ class BleActor(private val mContext: Context, var listener: BleConnectionListene
tempData = ByteArray(0)
return
}
if (characteristic.uuid.toString().lowercase() == BleService.READ_CHAR.lowercase() && !isFromMessage) {
if (characteristic.uuid.toString()
.lowercase() == BleService.READ_CHAR.lowercase() && !isFromMessage
) {
disConnectedDevice()
}
}
Expand All @@ -157,7 +168,11 @@ class BleActor(private val mContext: Context, var listener: BleConnectionListene
if (messageId.isEmpty() || messageId.isBlank()) {
listener!!.onCharacteristicWrite(gatt = gatt, characteristic = characteristic)
} else {
listener!!.onMessageSent(gatt = gatt, characteristic = characteristic, id = messageId)
listener!!.onMessageSent(
gatt = gatt,
characteristic = characteristic,
id = messageId
)
disConnectedDevice()
}
}
Expand Down Expand Up @@ -228,8 +243,13 @@ class BleActor(private val mContext: Context, var listener: BleConnectionListene
var isQaulDevice = false
for (gattService in serviceList) {
AppLog.e("SERVICE_UUID", gattService.uuid.toString())
if (gattService.uuid.toString().lowercase().trim() == BleService.SERVICE_UUID.lowercase().trim()) {
AppLog.e(TAG, "service : " + gattService.uuid.toString() + " " + bleDevice?.macAddress)
if (gattService.uuid.toString().lowercase()
.trim() == BleService.SERVICE_UUID.lowercase().trim()
) {
AppLog.e(
TAG,
"service : " + gattService.uuid.toString() + " " + bleDevice?.macAddress
)
isQaulDevice = true
listener?.addToIgnoreList(this.bleDevice!!)
val characteristics =
Expand Down Expand Up @@ -305,6 +325,15 @@ class BleActor(private val mContext: Context, var listener: BleConnectionListene
failedTask!!.cancel()
if (listener != null) {
listener!!.onConnectionFailed(bleDevice!!)
if (isFromMessage) {
if (mBluetoothGatt != null) {
BleService.bleService!!.bleCallback?.onMessageSent(
id = messageId,
success = false,
data = tempData
)
}
}
}
}
}
Expand Down Expand Up @@ -341,7 +370,12 @@ class BleActor(private val mContext: Context, var listener: BleConnectionListene
/**
* User write data to device
*/
fun writeServiceData(serUUID: String, charUUID: String, data: ByteArray?, attempt: Int): Boolean {
fun writeServiceData(
serUUID: String,
charUUID: String,
data: ByteArray?,
attempt: Int
): Boolean {
if (attempt < 3) {
if (data != null) {
AppLog.d(
Expand Down Expand Up @@ -388,7 +422,11 @@ class BleActor(private val mContext: Context, var listener: BleConnectionListene
}
}
}
BleService.bleService!!.bleCallback?.onMessageSent(id = messageId, success = false, data = data!!)
BleService.bleService!!.bleCallback?.onMessageSent(
id = messageId,
success = false,
data = data!!
)
return false
}

Expand All @@ -406,19 +444,23 @@ class BleActor(private val mContext: Context, var listener: BleConnectionListene
gatt: BluetoothGatt?,
characteristic: BluetoothGattCharacteristic?
)

fun onCharacteristicWrite(
gatt: BluetoothGatt?,
characteristic: BluetoothGattCharacteristic?
)

fun onMessageSent(
gatt: BluetoothGatt?,
characteristic: BluetoothGattCharacteristic?, id: String
)

fun onCharacteristicChanged(
macAddress: String?,
gatt: BluetoothGatt?,
characteristic: BluetoothGattCharacteristic?
)

fun addToBlackList(bleScanDevice: BLEScanDevice)
fun addToIgnoreList(bleScanDevice: BLEScanDevice)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,17 @@ import androidx.lifecycle.LifecycleService
import com.google.android.gms.common.api.GoogleApiClient
import com.google.android.gms.common.api.PendingResult
import com.google.android.gms.location.*
import com.google.gson.Gson
import com.google.protobuf.ByteString
import net.qaul.ble.AppLog
import net.qaul.ble.BLEUtils
import net.qaul.ble.RemoteLog
import net.qaul.ble.callback.BleRequestCallback
import net.qaul.ble.model.BLEScanDevice
import net.qaul.ble.model.Message
import net.qaul.ble.service.BleService
import qaul.sys.ble.BleOuterClass
import java.nio.charset.Charset

class BleWrapperClass(context: AppCompatActivity) {
private val TAG: String = BleWrapperClass::class.java.simpleName
Expand Down Expand Up @@ -195,10 +199,13 @@ class BleWrapperClass(context: AppCompatActivity) {
override fun onMessageReceived(bleDevice: BLEScanDevice, message: ByteArray) {
val bleRes = BleOuterClass.Ble.newBuilder()
val directReceived = BleOuterClass.BleDirectReceived.newBuilder()
val msgData = String(message).removeSuffix("$$")
.removePrefix("$$")
val msgObject = Gson().fromJson(msgData, Message::class.java)
directReceived.from = bleDevice.macAddress
directReceived.mode = BleOuterClass.BleMode.low_latency
directReceived.qaulId = ByteString.copyFrom(bleDevice.qaulId)
directReceived.data = ByteString.copyFrom(message)
directReceived.data = ByteString.copyFrom(msgObject.message, Charset.defaultCharset())
bleRes.directReceived = directReceived.build()
bleCallback?.bleResponse(ble = bleRes.build())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class BleService : LifecycleService() {
private val outOfRangeChecker = Handler(Looper.getMainLooper())
private val devicesList = Collections.synchronizedList(arrayListOf<BLEScanDevice>())
private val ignoreList = Collections.synchronizedList(arrayListOf<BLEScanDevice>())
private val receiveList = Collections.synchronizedList(arrayListOf<BLEScanDevice>())
private val blackList = Collections.synchronizedList(arrayListOf<BLEScanDevice>())
private val uuidList = arrayListOf<ParcelUuid>()
private var filters: ArrayList<ScanFilter> = arrayListOf()
Expand Down Expand Up @@ -392,6 +393,9 @@ class BleService : LifecycleService() {
val s = BLEUtils.byteToHex(value)
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 }
}
gattServer!!.sendResponse(
device,
requestId,
Expand All @@ -411,6 +415,8 @@ class BleService : LifecycleService() {
bleDevice = BLEScanDevice.getDevice()
bleDevice.macAddress = device.address
bleDevice.qaulId = msgObject.qaulId!!.toByteArray(Charset.defaultCharset())
bleDevice.bluetoothDevice = device
receiveList.add(bleDevice)
}
bleAdvertiseCallback!!.onMessageReceived(
bleDevice = bleDevice,
Expand All @@ -431,6 +437,8 @@ class BleService : LifecycleService() {
bleDevice = BLEScanDevice.getDevice()
bleDevice.macAddress = device.address
bleDevice.qaulId = msgObject.qaulId!!.toByteArray(Charset.defaultCharset())
bleDevice.bluetoothDevice = device
receiveList.add(bleDevice)
}
bleAdvertiseCallback!!.onMessageReceived(
bleDevice = bleDevice,
Expand Down Expand Up @@ -731,8 +739,14 @@ class BleService : LifecycleService() {
return baseBleActor
}

/**
* This Method Will Be Used to Send Data to Other Qaul-Device
*/
fun sendMessage(id: String, to: ByteArray, message: ByteArray, from: ByteArray) {
val bleDevice = ignoreList.find { it.qaulId.contentEquals(to) }
var bleDevice = ignoreList.find { it.qaulId.contentEquals(to) }
if (bleDevice == null) {
bleDevice = receiveList.find { it.qaulId.contentEquals(to) }
}
val msg = Message()
msg.message = String(message)
msg.qaulId = String(from)
Expand Down

0 comments on commit a1c459a

Please sign in to comment.