vikasvmane / AIDLServices

Demo app to showcase Sensor data using AIDL bound services.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AIDLServices

Displays data related to TYPE_ROTATION_VECTOR sensor using AIDL

  • Consist of aidlsdk module that expose sensor data
  • Sample app to show the sensor data

Usage

aidlsdk module

  • Bind Service
class SensorService : LifecycleService() //Bound Sevice with exposes sensor data using liveData

 val sensorData = MutableLiveData<FloatArray>() //Exposes rotational vector sensor data to the observer.

App usage

  • Add in your apps AndroidManifest.xml
<service
       android:name="com.vikasmane.aidlsdk.SensorService"
       android:enabled="true"
       android:exported="true" />
  • To use the service in your app bind to the service
private fun bindService() {
        rotationalServiceIntent = Intent(this, SensorService::class.java)
        rotationalServiceConnection = object : ServiceConnection {
            override fun onServiceConnected(componentName: ComponentName?, binder: IBinder?) {
                val orientationInterface = RotationalDataInterface.Stub.asInterface(binder)
                sensorDataText.text = orientationInterface?.rotationalData
                isServiceConnected = true
                updateButtonText()
            }

            override fun onServiceDisconnected(componentName: ComponentName?) {
                isServiceConnected = false
                updateButtonText()
            }
        }
        bindService(
            rotationalServiceIntent,
            rotationalServiceConnection,
            Context.BIND_AUTO_CREATE
        )
    }
  • The actual sensor listener is attached on calling rotationalData of orientationInterface after service is connected
orientationInterface?.rotationalData //Starts the sensor listener
  • This will start pushing the sensor data into the sensorData liveData
SensorService.sensorData.observe(this, {
            sensorDataText.text = it.contentToString()
        })

App screenshot

About

Demo app to showcase Sensor data using AIDL bound services.


Languages

Language:Kotlin 100.0%