-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reading multiple Settings via settingsManager returns same result for different settings #138
Comments
I was able to solve this by wrapping all suspend fun readSettingString(device: BluetoothDevice, settingName: String): String? =
suspendCoroutine { cont ->
val transport = McuMgrBleTransport(context, device)
val settingsManager = SettingsManager(transport)
val callback = object : McuMgrCallback<McuMgrSettingsReadResponse> {
override fun onResponse(response: McuMgrSettingsReadResponse) {
if (response.isSuccess) {
val byteArray = response.`val`
cont.resume(byteArray.decodeToString(0, byteArray.indexOf(0.toByte())))
}
}
override fun onError(p0: McuMgrException) {
cont.resume(null)
}
}
// Read setting asynchronously
settingsManager.read(settingName, callback)
} Now I can read one setting after another inside a LaunchedEffect(device) {
ssidState.value = readSettingString(device, "wifi/ssid")
passwordState.value = readSettingString(device, "wifi/password")
serverState.value = readSettingString(device, "lwm2m/server")
} |
Thank you ofr sharing. Out of curiosity, in your first approach, did you enable logs from Lines 286 to 298 in 6411d43
Or just always return Log.VERBOSE from the second one.
I wonder where the Strings get overriden. Does it happen on the BLE layer, or above. |
I tried with enabling all logs via
I also added the Library as Sourcecode and changed the second method you mentioned to always return Maybe the library has a problem because I am creating an |
After changing the code to use logcat I got the following log output:
|
After changing my code to create only a single instance of fun DetailsScreen(navController: NavController, bluetoothViewModel: BluetoothViewModel) {
val device = bluetoothViewModel.selectedDevice ?: return
val context = LocalContext.current
var devName = remember { mutableStateOf<String?>(null) }
var devManufacturer = remember { mutableStateOf<String?>(null) }
var devHardwareVersion = remember { mutableStateOf<String?>(null) }
var ssidState = remember { mutableStateOf<String?>(null) }
var passwordState = remember { mutableStateOf<String?>(null) }
var serverState = remember { mutableStateOf<String?>(null) }
var techState = remember { mutableStateOf<UInt>(0u) }
val transport = McuMgrBleTransport(context, device)
val settingsManager = SettingsManager(transport)
// ... some other code
fun SettingsManager.readSetting(settingName: String, state: MutableState<String?>) {
Log.d("readSetting","Start reading Setting ${settingName}")
read(
settingName,
object : McuMgrCallback<McuMgrSettingsReadResponse> {
override fun onResponse(response: McuMgrSettingsReadResponse) {
if (response.isSuccess) {
val byteArray = response.`val`
state.value = byteArray.toString(Charsets.UTF_8)
Log.d("readSetting", "onResponse called. Setting: $settingName, Response: ${state.value}")
}
}
override fun onError(p0: McuMgrException) {
Log.e("readSetting", "Could not read setting $settingName. Error: ${p0.message}")
Toast.makeText(
context,
"Konnte Setting \"$settingName\" nicht lesen: ${p0.message}, ${p0.cause}",
Toast.LENGTH_LONG
).show()
}
})
}
fun SettingsManager.readSetting(settingName: String, settingState: MutableState<UInt>) {
val callback = object : McuMgrCallback<McuMgrSettingsReadResponse> {
override fun onResponse(response: McuMgrSettingsReadResponse) {
if (response.isSuccess) {
val byteArray = response.`val`
settingState.value = byteArray.toUInt()
Log.d("readSetting", "onResponse called. Setting: $settingName, Response: ${byteArray.toHex()}")
}
}
override fun onError(p0: McuMgrException) {
Log.e("readSetting", "Could not read setting $settingName. Error: ${p0.message}")
Toast.makeText(
context,
"Konnte Setting \"$settingName\" nicht lesen: ${p0.message}, ${p0.cause}",
Toast.LENGTH_LONG
).show()
}
}
// Read setting asynchronously
read(settingName, callback)
}
// ... some other code here
LaunchedEffect(device){
settingsManager.readSetting("lwm2m/device_type", devName)
settingsManager.readSetting("lwm2m/manufacturer", devManufacturer)
settingsManager.readSetting("lwm2m/hardware_version", devHardwareVersion)
settingsManager.readSetting("wifi/ssid", ssidState)
settingsManager.readSetting("wifi/password", passwordState)
settingsManager.readSetting("lwm2m/server", serverState)
settingsManager.readSetting("tech/imp_per_kwh", techState)
}
} It should be documented somewhere that you must not create multiple instances of |
Any update on this? |
Not yet, sorry. We're busy with other tasks. |
I am trying to read multiple settings from inside a composable from my Device via the
settingsManager
. I am using the following codeThe android logs show the same string result for both requests:
I can see that my device returns the right content (printed in the logs):
Unfortunately both texts in my
Surface
show the same string (either SSID or password). How do I read multiple settings in parallel?The text was updated successfully, but these errors were encountered: