Get error Invalid input class: 'float' when trying to read temperature - code is in kotlin

I'm trying to read the temperature data from my MetaMotionR, but i get and error that says:
2020-10-08 12:18:37.290 31070-1610/com.example.afgangsprojektet W/BluetoothGatt: Unhandled exception in callback
java.lang.ClassCastException: Invalid input class: 'float'
at com.mbientlab.metawear.impl.DataPrivate.value(DataPrivate.java:72)
at com.mbientlab.metawear.impl.SFloatData$1.value(SFloatData.java:94)

I have tried reading acceleration data, and that works fine, can anyone help?

This is the part of my code where i try to read temperature:

override fun onServiceConnected(componentName: ComponentName?, iBinder: IBinder) {
    serviceBinder = iBinder as LocalBinder
    Log.d(SENSOR_LOG, "Sensor connected")
    retrieveBoard(sensorResId)
}

private fun retrieveBoard(macAddress: String) {
    val btManager =
        getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
    val remoteDevice: BluetoothDevice = btManager.adapter.getRemoteDevice(macAddress)

    // Create a MetaWear board object for the Bluetooth Device
    board = serviceBinder.getMetaWearBoard(remoteDevice)

    board.connectAsync().onSuccessTask {

            Log.d(SENSOR_LOG, "Sensor connection succeeded, connected to: $macAddress")

            setUpTemperatureSensor()

    }.continueWith { task ->
        if (task.isFaulted) {
            Log.d(SENSOR_LOG, "Sensor connection is faulted error: ${task.error} ")
        } else {
            Log.d(SENSOR_LOG, "Sensor is configured")
            temperatureSensor.read()
        }
        null
    }
}

private fun setUpTemperatureSensor(): Task<Route>{
    temperature = board.getModule(Temperature::class.java)
    temperatureSensor =
        temperature.findSensors(Temperature.SensorType.PRESET_THERMISTOR)[0]

    return temperatureSensor.addRouteAsync { source ->
        source.stream { data, _ ->
            Log.d(SENSOR_LOG, "temperature data ${data.value(Float::class.java)}")
        }
    }
}

Comments

  • Have you been able to get this working?

  • I ran into this issue as well. It seems that for whatever reason the Float::class.java type is cast into a primitive float type (so not Float with a capital F) on the Java side in the mbientlab API. I've tried quite a few things but have not been able to solve this. Any progress on your side?

  • I should have waited just a little before sending the question above. I now have the answer:

    return temperatureSensor.addRouteAsync { source -> source.stream { data, _ -> Log.d(SENSOR_LOG, "temperature data ${data.value(Float::class.javaObjectType)}") } }

  • Nice!, thanks for the update @renev

Sign In or Register to comment.