feat: discover services
This commit is contained in:
@@ -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")
|
@SuppressLint("MissingPermission")
|
||||||
class MainActivity : AppCompatActivity() {
|
class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
@@ -89,6 +108,13 @@ class MainActivity : AppCompatActivity() {
|
|||||||
"BluetoothGattCallback",
|
"BluetoothGattCallback",
|
||||||
"Successfully connected to $deviceAddress (${deviceName})"
|
"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
|
// TODO: Store a reference to BluetoothGatt
|
||||||
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
|
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
|
||||||
Log.w(
|
Log.w(
|
||||||
@@ -105,6 +131,16 @@ class MainActivity : AppCompatActivity() {
|
|||||||
gatt.close()
|
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 {
|
private val scanResultAdapter: ScanResultAdapter by lazy {
|
||||||
|
|||||||
Reference in New Issue
Block a user