feat: decode demo characteristic
This commit is contained in:
@@ -64,6 +64,15 @@ data class DiscoverServicesRequest(
|
|||||||
) : BleOperationType()
|
) : BleOperationType()
|
||||||
|
|
||||||
|
|
||||||
|
open class BleListener {
|
||||||
|
open fun onSuccessfulCharRead(
|
||||||
|
gatt: BluetoothGatt,
|
||||||
|
characteristic: BluetoothGattCharacteristic
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private fun BluetoothGatt.printGattTable() {
|
private fun BluetoothGatt.printGattTable() {
|
||||||
if (services.isEmpty()) {
|
if (services.isEmpty()) {
|
||||||
Log.i(
|
Log.i(
|
||||||
@@ -106,6 +115,16 @@ object ConnectionManager {
|
|||||||
private val deviceGattMap = ConcurrentHashMap<BluetoothDevice, BluetoothGatt>()
|
private val deviceGattMap = ConcurrentHashMap<BluetoothDevice, BluetoothGatt>()
|
||||||
private val operationQueue = ConcurrentLinkedQueue<BleOperationType>()
|
private val operationQueue = ConcurrentLinkedQueue<BleOperationType>()
|
||||||
private var pendingOperation: BleOperationType? = null
|
private var pendingOperation: BleOperationType? = null
|
||||||
|
private var listeners = ArrayList<BleListener>()
|
||||||
|
|
||||||
|
|
||||||
|
fun register(listener: BleListener) {
|
||||||
|
listeners.add(listener)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun notifyListeners(notifier: (listener: BleListener) -> Unit) {
|
||||||
|
listeners.forEach(notifier)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
fun connect(device: BluetoothDevice, context: Context) {
|
fun connect(device: BluetoothDevice, context: Context) {
|
||||||
@@ -310,6 +329,12 @@ object ConnectionManager {
|
|||||||
"ConnectionManager",
|
"ConnectionManager",
|
||||||
"Read characteristic $uuid (service: ${service.uuid}): ${value.toHexString()}"
|
"Read characteristic $uuid (service: ${service.uuid}): ${value.toHexString()}"
|
||||||
)
|
)
|
||||||
|
notifyListeners { listener ->
|
||||||
|
listener.onSuccessfulCharRead(
|
||||||
|
gatt,
|
||||||
|
characteristic
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
BluetoothGatt.GATT_READ_NOT_PERMITTED -> {
|
BluetoothGatt.GATT_READ_NOT_PERMITTED -> {
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import android.annotation.SuppressLint
|
|||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import android.app.AlertDialog
|
import android.app.AlertDialog
|
||||||
import android.bluetooth.BluetoothAdapter
|
import android.bluetooth.BluetoothAdapter
|
||||||
|
import android.bluetooth.BluetoothGatt
|
||||||
|
import android.bluetooth.BluetoothGattCharacteristic
|
||||||
import android.bluetooth.BluetoothManager
|
import android.bluetooth.BluetoothManager
|
||||||
import android.bluetooth.le.ScanCallback
|
import android.bluetooth.le.ScanCallback
|
||||||
import android.bluetooth.le.ScanResult
|
import android.bluetooth.le.ScanResult
|
||||||
@@ -26,6 +28,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
|
|||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import androidx.recyclerview.widget.SimpleItemAnimator
|
import androidx.recyclerview.widget.SimpleItemAnimator
|
||||||
import com.example.sensortestingapp.databinding.ActivityMainBinding
|
import com.example.sensortestingapp.databinding.ActivityMainBinding
|
||||||
|
import com.punchthrough.blestarterappandroid.ble.BleListener
|
||||||
import com.punchthrough.blestarterappandroid.ble.ConnectionManager
|
import com.punchthrough.blestarterappandroid.ble.ConnectionManager
|
||||||
import java.nio.ByteBuffer
|
import java.nio.ByteBuffer
|
||||||
import java.nio.ByteOrder
|
import java.nio.ByteOrder
|
||||||
@@ -139,6 +142,20 @@ class MainActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val bleListener = object : BleListener() {
|
||||||
|
override fun onSuccessfulCharRead(
|
||||||
|
gatt: BluetoothGatt,
|
||||||
|
characteristic: BluetoothGattCharacteristic
|
||||||
|
) {
|
||||||
|
|
||||||
|
if (characteristic.service.uuid == DEMO_SERVICE_UUID && characteristic.uuid == DEMO_CHAR_UUID) {
|
||||||
|
val payload = decodeDemoPayload(characteristic.value)
|
||||||
|
Log.i("BleListener", "Demo char received: $payload")
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
WindowCompat.setDecorFitsSystemWindows(window, false)
|
WindowCompat.setDecorFitsSystemWindows(window, false)
|
||||||
@@ -164,6 +181,8 @@ class MainActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
setupRecyclerView()
|
setupRecyclerView()
|
||||||
|
|
||||||
|
ConnectionManager.register(bleListener)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setupRecyclerView() {
|
private fun setupRecyclerView() {
|
||||||
|
|||||||
Reference in New Issue
Block a user