I m building an Android application using Kotlin. In this application I need to connect with a physical device throught bluetooth. This device implement Ant+ protocol. So I need to get from it
- Cadence value
- Speed value
- Fitness Equipment device.
These are three features that I can receive from it.
So I m building the following code:
private fun setPanoramaChangeListener() {
//some code
setupAntSampler()
//some code
}
private fun setupAntSampler() {
bsdReleaseHandle = AntPlusBikeSpeedDistancePcc.requestAccess(
this, this,
mResultReceiver, mDeviceStateChangeReceiver
)
}
var mResultReceiver: IPluginAccessResultReceiver<AntPlusBikeSpeedDistancePcc> =
object :
IPluginAccessResultReceiver<AntPlusBikeSpeedDistancePcc> {
override fun onResultReceived(
result: AntPlusBikeSpeedDistancePcc?,
resultCode: RequestAccessResult?, initialDeviceState: DeviceState?
) {
when (resultCode) {
RequestAccessResult.SUCCESS -> {
HomeActivity.bsdPcc = result
// tv_status.setText(result.deviceName + ": " + initialDeviceState)
subscribeToEvents()
}
}
releaseHandle = AntPlusFitnessEquipmentPcc.requestNewOpenAccess(
this@TourActivity,
this@TourActivity, mPluginAccessResultReceiver, mDeviceStateChangeReceiver,
mFitnessEquipmentStateReceiver
)
}
private fun subscribeToEvents() {
bcPcc?.subscribeCalculatedCadenceEvent { _, _, calculatedCadence ->
runOnUiThread {
// binding?.tvCadence?.text = "Cadence value is : $calculatedCadence"
cadenceDoubleList.add(calculatedCadence.toDouble())
if (maxCadence < calculatedCadence.toDouble())
maxCadence = calculatedCadence.toDouble()
// tv_estTimestamp.setText(estTimestamp.toString())
// tv_calculatedCadence.setText(calculatedCadence.toString())
}
}
bcPcc?.subscribeRawCadenceDataEvent { _, _, _, _ ->
runOnUiThread {
// tv_estTimestamp.setText(estTimestamp.toString())
// tv_timestampOfLastEvent.setText(timestampOfLastEvent.toString())
// tv_cumulativeRevolutions.setText(cumulativeRevolutions.toString())
}
}
if (bcPcc?.isSpeedAndCadenceCombinedSensor == true) {
runOnUiThread {
bsdReleaseHandle = AntPlusBikeSpeedDistancePcc.requestAccess(
this@TourActivity, bcPcc?.antDeviceNumber ?: 0, 0, true,
mResultReceiver, mDeviceStateChangeReceiver
)
}
} else {
// Subscribe to the events available in the pure cadence profile
runOnUiThread {
// tv_isSpdAndCadCombo.setText("No")
// tv_calculatedSpeed.setText("N/A")
}
bcPcc?.subscribeCumulativeOperatingTimeEvent { _, _, _ ->
runOnUiThread {
// tv_estTimestamp.setText(estTimestamp.toString())
// tv_cumulativeOperatingTime.setText(cumulativeOperatingTime.toString())
}
}
bcPcc?.subscribeManufacturerAndSerialEvent { _, _, _, _ ->
runOnUiThread {
/* tv_estTimestamp.setText(estTimestamp.toString())
tv_manufacturerID.setText(manufacturerID.toString())
tv_serialNumber.setText(serialNumber.toString())*/
}
}
bcPcc?.subscribeVersionAndModelEvent { _, _, _, _, _ ->
runOnUiThread {
// tv_estTimestamp.setText(estTimestamp.toString())
// tv_hardwareVersion.setText(hardwareVersion.toString())
// tv_softwareVersion.setText(softwareVersion.toString())
// tv_modelNumber.setText(modelNumber.toString())
}
}
bcPcc?.subscribeBatteryStatusEvent { _, _, _, _ ->
runOnUiThread {
// tv_estTimestamp.setText(estTimestamp.toString())
// textView_BatteryVoltage.setText(if (batteryVoltage.toInt() != -1) batteryVoltage.toString() + "V" else "Invalid")
// textView_BatteryStatus.setText(batteryStatus.toString())
}
}
bcPcc?.subscribeMotionAndCadenceDataEvent { _, _, _ ->
runOnUiThread {
// tv_estTimestamp.setText(estTimestamp.toString())
// textView_IsPedallingStopped.setText(isPedallingStopped.toString())
}
}
}
}
}
With the code I must select for 3 times my device.
There is a way to select automatically my device 1 time?
To automate the device selection process in your Android application using Kotlin and the ANT+ protocol, you can modify your implementation to store the device's ANT+ ID after the first successful connection, using
SharedPreferences.Then, you would use this ID for subsequent connections to avoid manual selection each time.
Modify the
requestAccessmethod calls to use the stored ANT+ ID for direct connection attempts.You will need to integrate the device ID storage and retrieval logic in your
setupAntSamplerandonResultReceivedmethods. Make sure you handle cases where the device ID might change or when the direct connection attempt fails, prompting a fallback to the manual selection process.