-
-
Save BALUSANGEM/050425bccef32784ffb32a1fcb5eedec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open class BaseGhattCallback : BluetoothGattCallback() { | |
var mIsDeviceConnected = false | |
internal var mCommandCharacteristic: BluetoothGattCharacteristic? = null | |
open val TAG = BaseGhattCallback::class.java.simpleName | |
private val SUBSCRIBE_CHARACTERISTIC: Int = 0 | |
private val READ_CHARACTERISTIC: Int = 1 | |
private val WRITE_CHARACTERISTIC: Int = 2 | |
fun queueReadDataFromCharacteristic(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic) { | |
Log.d(TAG, "queueReadDataFromCharacteristic") | |
val ghattQueueItem = GhattQueueItem() | |
ghattQueueItem.characteristic = characteristic | |
ghattQueueItem.ghatt = gatt | |
ghattQueueItem.ghattQueueItemType = READ_CHARACTERISTIC | |
addToGhattQueue(ghattQueueItem) | |
} | |
fun queueSetNotificationForCharacteristic(gatt: BluetoothGatt, characteristic: BluetoothGattCharacteristic) { | |
Log.d(TAG, "queueSetNotificationForCharacteristic") | |
val ghattQueueItem = GhattQueueItem() | |
ghattQueueItem.characteristic = characteristic | |
ghattQueueItem.enabled = true | |
ghattQueueItem.ghatt = gatt | |
ghattQueueItem.ghattQueueItemType = SUBSCRIBE_CHARACTERISTIC | |
addToGhattQueue(ghattQueueItem) | |
} | |
fun queueWriteDataToCharacteristic(gatt: BluetoothGatt, dataToWrite: ByteArray) { | |
Log.d(TAG, "queueWriteDataToCharacteristic") | |
val txQueueItem = GhattQueueItem() | |
txQueueItem.ghatt = gatt | |
txQueueItem.dataToWrite = dataToWrite | |
txQueueItem.ghattQueueItemType = WRITE_CHARACTERISTIC | |
addToGhattQueue(txQueueItem) | |
} | |
fun sendBatteryCommand(gatt: BluetoothGatt) { | |
Log.d(TAG, "Send battery command!") | |
val olddata = byteArrayOf(FlipFsmConstants.GET_BATTERY_PER.toByte(), 0x00, 0x00, 0x00, 0x00) | |
queueWriteDataToCharacteristic(gatt, olddata) | |
} | |
private var ghattQueueProcessing: Boolean = false | |
private fun addToGhattQueue(ghattQueueItem: GhattQueueItem) { | |
ghattQueue.add(ghattQueueItem) | |
if (!ghattQueueProcessing) { | |
processTxQueue() | |
} | |
} | |
protected fun processTxQueue() { | |
Log.d(TAG, "processTxQueue isProcessing $ghattQueueProcessing and size is ${ghattQueue.size}") | |
if (ghattQueue.size <= 0) { | |
ghattQueueProcessing = false | |
return | |
} | |
ghattQueueProcessing = true | |
val ghattQueueItem = ghattQueue.remove() | |
when (ghattQueueItem.ghattQueueItemType) { | |
WRITE_CHARACTERISTIC -> writeDataToCharacteristic(ghattQueueItem) | |
SUBSCRIBE_CHARACTERISTIC -> setNotificationForCharacteristic(ghattQueueItem) | |
READ_CHARACTERISTIC -> readDataFromCharacteristic(ghattQueueItem) | |
} | |
} | |
private fun readDataFromCharacteristic(ghattQueueItem: GhattQueueItem?) { | |
Log.d(TAG, " readDataFromCharacteristic") | |
ghattQueueItem?.ghatt?.readCharacteristic(ghattQueueItem.characteristic) | |
} | |
private fun setNotificationForCharacteristic(ghattQueueItem: GhattQueueItem?) { | |
Log.d(TAG, " setNotificationForCharacteristic") | |
if (ghattQueueItem != null) { | |
val gatt = ghattQueueItem.ghatt | |
val characteristic = ghattQueueItem.characteristic | |
if (gatt != null && characteristic != null) { | |
gatt.setCharacteristicNotification(characteristic, true) | |
// This is specific to Heart Rate Measurement. | |
val descriptor = characteristic.getDescriptor( | |
UUID.fromString("DISCRIPTOR_ID")) | |
if (descriptor != null) { | |
descriptor.value = BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE; | |
gatt.writeDescriptor(descriptor) | |
} else { | |
Log.e(TAG, "descriptor is NULL") | |
} | |
} else { | |
Log.e(TAG, "Gatt or Charactertisic is NULl") | |
} | |
} else { | |
Log.d(TAG, "GhattQueueItem is NULL") | |
} | |
} | |
private fun writeDataToCharacteristic(ghattQueueItem: GhattQueueItem) { | |
Log.d(TAG, " writeDataToCharacteristic") | |
if (mCommandCharacteristic != null) { | |
mCommandCharacteristic!!.value = ghattQueueItem.dataToWrite | |
val gatt = ghattQueueItem.ghatt | |
if (gatt != null) { | |
gatt.writeCharacteristic(mCommandCharacteristic) | |
} else { | |
Log.d(TAG, "Gatt is NULL") | |
} | |
} else { | |
Log.d(TAG, "mCommandCharacteristic is null") | |
ghattQueueProcessing = false | |
} | |
} | |
private val ghattQueue = LinkedList<GhattQueueItem>() | |
private inner class GhattQueueItem { | |
internal var ghatt: BluetoothGatt? = null | |
internal var characteristic: BluetoothGattCharacteristic? = null | |
internal var dataToWrite: ByteArray? = null // Only used for characteristic write | |
internal var enabled: Boolean = false // Only used for characteristic notification subscription | |
var ghattQueueItemType: Int? = null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment