diff --git a/app/src/main/java/com/example/sensortestingapp/MainActivity.kt b/app/src/main/java/com/example/sensortestingapp/MainActivity.kt index 9b0d961..0dd4a80 100644 --- a/app/src/main/java/com/example/sensortestingapp/MainActivity.kt +++ b/app/src/main/java/com/example/sensortestingapp/MainActivity.kt @@ -50,6 +50,25 @@ fun Context.hasRequiredRuntimePermissions(): Boolean { } } +private fun BluetoothGatt.printGattTable() { + if (services.isEmpty()) { + Log.i( + "printGattTable", + "No service and characteristic available, call discoverServices() first?" + ) + return + } + services.forEach { service -> + val characteristicsTable = service.characteristics.joinToString( + separator = "\n|--", + prefix = "|--" + ) { it.uuid.toString() } + Log.i( + "printGattTable", "\nService ${service.uuid}\nCharacteristics:\n$characteristicsTable" + ) + } +} + @SuppressLint("MissingPermission") class MainActivity : AppCompatActivity() { @@ -89,6 +108,13 @@ class MainActivity : AppCompatActivity() { "BluetoothGattCallback", "Successfully connected to $deviceAddress (${deviceName})" ) + val serviceDiscoveryStarted = gatt.discoverServices() + if (!serviceDiscoveryStarted) { + Log.e( + "BluetoothGattCallback", + "Service discovery did not start" + ) + } // TODO: Store a reference to BluetoothGatt } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { Log.w( @@ -105,6 +131,16 @@ class MainActivity : AppCompatActivity() { gatt.close() } } + + override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) { + with(gatt) { + Log.i( + "BluetoothGattCallback", + "Discovered ${services.size} services for ${device.address} (${device.name})" + ) + printGattTable() + } + } } private val scanResultAdapter: ScanResultAdapter by lazy {