flutter sensorz
sensorz , a simple flutter android app, which displays android sensor data.
introduction
- for implementation of ui, this app uses flutter framework.
- code base is written mainly using dart and kotlin, these two languages.
- for accessing platform features, such as sensors, kotlin language is used.
- ui level coding is done using dart language.
- for receiving continuous sensor data feed from platform level, eventchannel is used.
- this android app is tested to be properly working on android pie (9.0).
- this app uses android sensors feature ( android.hardware.sensor ).
- to install apk using adb, try running following command in your computer’s console,
after downloading apk. make sure you have adb installed and path variable is properly set.
>>> adb install sensorz.apk
- for more info on adb, head to this link.
download
- you could easily download the apk file here.
screen captures
well i’ve added some screen captures of the android app, which i took while running it on a physical android device.
implementation info
lets first talk about dart portion of the app …
ui using dart
- in main.dart, i created one stateless widget, sensormain under which, initialroute was sensormainhome, which is a stateful widget.
- as i’m using one stateful widget, i need to define a class which will be holding the current state of the app.
- _sensormainstate, holds current state of app.
- overriding build method is required in _sensormainstate. so i used a scaffold widget, which is a very basic material based widget.
- well let me tell you one thing, in flutter, almost every ui component is a widget.
- for displaying sensor info and their live data feed, i’m using lisview layout manager.
- a card ( material design ui component ), is holding info regarding a certain sensor which is present on that device.
- for invoking methods which are available in platform level ( android api ), i used methodchannel.
- first need to create an instance of method channel with one unique methodchannelname (string), the same name should be used in platform level code too.
- that instance will help us to invoke methods which are defined in platform level.
- one eventchannel is also created, which helps us to gain live sensor data updates from platform level.
- remember, we need to create both, methodchannel and eventchannel from platform level too, using same channel identifier string.
- check code comments, for more info.
going to platform level implementation of the app …
platform level using kotlin :
- in mainactivity.kt, i registered methodchannel and eventchannel.
- when getsensorslist() is invoked from platform level, a list of all sensors present in that device is returned back to ui level calling function.
- and using the instance of sensormanager which i created at very beginning of app lifecycle, sensoreventlistener is registered for all of those sensors.
- for sending live sensor data feed to ui level, so that i can update ui as soon as data comes in, i wrote on class mysensorlistener, which overrides sensoreventlistener, and takes instance of eventchannel.eventsink as the only parameter.
- in onsensorchanged(), i send sensor data back to ui level, where a listening service is already registered.
- as soon as data comes in, ui level dart code starts working for extracting that data and updates ui, so that changes gets reflected.
Comments are closed.