Upload
trinhcong
View
216
Download
2
Embed Size (px)
Citation preview
Sistemi di Elettronica Digitale Sez8
Alessandra Flamminialessandraflamminiunibsit
Ufficio 24 Dip Ingegneria dellrsquoInformazione030-3715627 Lunedigrave 1630-1830
Sistemi di elettronica digitale A Flammini AA2017-2018
Android SensorySystems SAndroidE
Sistemi di elettronica digitale A Flammini AA2017-2018
HOW ANDROID APPs MANAGE INTERNAL SENSORS
3Sistemi di Elettronica Digitale A Flammini
Most Android-powered devices have built-in sensors that measure motion orientation and various environmental conditions These sensors are capable of providing raw data with high precision and accuracy and are useful if you want to monitor three-dimensional devicemovement or positioning or you want to monitor changes in the ambient environmentnear a device
The Android platform supports three broad categories of sensorsbull Motion sensors these sensors measure acceleration forces and rotational forces along
three axes This category includes accelerometers gravity sensors gyroscopes and rotational vector sensors
bull Environmental sensors these sensors measure various environmental parameters suchas ambient air temperature and pressure illumination and humidity This categoryincludes barometers photometers and thermometers
bull Position sensors these sensors measure the physical position of a device This categoryincludes orientation sensors and magnetometers
SENSOR CATEGORIES
4Sistemi di Elettronica Digitale A Flammini
HW based sensors
Physical component built into the device
The data is directly acquired measuring specific environmental properties
SW based sensors
Emulate a HW based sensor behavior
The data are derived from one or more HW based sensors
Smart Sensors for Domitics and Health Care Alessandra Flammini UNIBS
5
Sensor Type Description Common UsesTYPE_ACCELEROMETER
Hardware Measures the acceleration force in ms
2that is applied to a device on all
three physical axes (x y and z) including the force of gravity
Motion detection (shake tilt etc)
TYPE_AMBIENT_TEMPERATURE
Hardware Measures the ambient room temperature in degrees Celsius (degC) See note below
Monitoring air temperatures
TYPE_GRAVITY
Software or Hardware
Measures the force of gravity in ms
2that is applied to a device on all
three physical axes (x y z)
Motion detection (shake tilt etc)
TYPE_GYROSCOPE
Hardware Measures a devices rate of rotation in rads around each of the three physical axes (x y and z)
Rotation detection (spin turn etc)
TYPE_LIGHT Hardware Measures the ambient light level (illumination) in lx
Controlling screen brightness
TYPE_LINEAR_ACCELERATION
Software or Hardware
Measures the acceleration force in ms
2that is applied to a device on all
three physical axes (x y and z) excluding the force of gravity
Monitoring acceleration along a single axis
ANDROID SENSORS TYPES (1)
Smart Sensors for Domitics and Health Care Alessandra Flammini UNIBS
6
TYPE_ORIENTATION
Software Measures degrees of rotation that a device makes around all three physical axes (x y z) As of API level 3 you can obtain the inclination matrix and rotation matrix for a device by using the gravity sensor and the geomagnetic field sensor in conjunction with the getRotationMatrix() method
Determining device position
TYPE_PRESSURE
Hardware Measures the ambient air pressure in hPa or mbar
Monitoring air pressure changes
TYPE_PROXIMITY
Hardware Measures the proximity of an object in cm relative to the view screen of a device This sensor is typically used to determine whether a handset is being held up to a persons ear
Phone position during a call
TYPE_RELATIVE_HUMIDITY
Hardware Measures the relative ambient humidity in percent ()
Monitoring dewpoint absolute and relative humidity
ANDROID SENSORS TYPES (2)
SENSORS Accelerometer
7Sistemi di Elettronica Digitale A Flammini
Typically usages
screen orientation
inclination for game input
vibrations measurements
SENSORS GPS
8Sistemi di Elettronica Digitale A Flammini
Typically usages
Location
LaterationTriangulation of cell towers or wifinetworks (with database of known locations for towers and networks)
Location of associated cell tower or wifi network
Need connect to 3 satellites for 2D positioning 4 satellites for 3D positioning
More visible satellites increase precision
Typical precision 20-50m
Maximum precision 10m
SENSORS Gyroscope
9Sistemi di Elettronica Digitale A Flammini
Usages
Measurements of rate of rotation (angular speed)
3 values related to the axes
Pitch value (rotation around X axis)
Roll value (rotation around Y axis)
Yaw value (rotation around Z axis)
SENSOR FRAMEWORK
10Sistemi di Elettronica Digitale A Flammini
You can access these sensors and acquire raw sensor data by using the Android sensorframework The sensor framework is part of the androidhardware package and includesthe following classes and interfaces
bull SensorManager You can use this class to create an instance of the sensor service This class provides various methods for accessing and listing sensors registering and unregistering sensor event listeners This class also provides several sensor constantsthat are used to report sensor accuracy set data acquisition rates and calibrate sensors
bull Sensor You can use this class to create an instance of a specific sensor This classprovides various methods that let you determine a sensors capabilities
bull SensorEvent The system uses this class to create a sensor event object which providesinformation about a sensor event A sensor event object includes the followinginformation the raw sensor data the type of sensor that generated the event the accuracy of the data and the timestamp for the event
bull SensorEventListener You can use this interface to create two callback methods thatreceive notifications (sensor events) when sensor values change or when sensoraccuracy changes
httpsdeveloperandroidcomguidetopicssensorssensors_overviewhtml
SENSOR FRAMEWORK how to use
11Sistemi di Elettronica Digitale A Flammini
You can access sensor information by means of several methods
bull getResolution() sensor resolutionbull getMaximumRange() sensor maximum rangebull getPower() sensors power requirements requirementsbull getMinDelay() returns the minimum time interval (in microseconds) a sensor can use to
sense data Any sensor that returns a non-zero value for the getMinDelay() method is a streaming sensor otherwise it reports data only when there is a change in the parameters it is sensing
bull getVendor() sensor manufacturer
You can set sensor parameters by registerListener() methodbull SensorManagerSENSOR_DELAY_NORMAL sensor sampling interval of a streaming
sensor is set to NORMAL (default setting) resolution NORMAL = 200ms UI = 60ms GAME = 20ms FASTEST = 0ms asynchronous)
You can access sensor data and events by implementing a callbackbull onSensorChanged() a sensor reports a new value System provides you with a
SensorEvent object containing information (accuracy source timestamp value)
ANDROID APP SENSOR LEARN BY EXAMPLE
12Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVEModify the text laquoHello Worldraquo with laquoHello World RMS = sqrt( (x^2 + y^2 + z^2)3)raquo where RMS is the Root Mean Square
Objective is to insert some code within the MainActivityjava file which subscribes for accelerometer values and calculates and visualizes the Root Mean Square value to the user
ANDROID APP SENSOR LEARN BY EXAMPLE
13Sistemi di Elettronica Digitale A Flammini
2) SUBSCRIPTION TO SENSORMANAGER
To get values from accelerometer is is necessary to subscribe for receiving accelerometer sensor events from the SensorManager which is the service providing access to internal sensors in Android
A) Create variable which will contain the SensorManager object
B) The SensorManager service is assigned to the variable
C) A subscription for accelerometer sensor is requested with delay normal (200ms) and the current activity(this) will be the target responsible for handling sensor events containing acceleration values
A
BC Warning this will be
red coloured until next step
ANDROID APP SENSOR LEARN BY EXAMPLE
14Sistemi di Elettronica Digitale A Flammini
3) IMPLEMENT RECEIVING CALLBACKS
B) Fill the onSensorChanged with the code to change the text of the TextView Warning code seems complicated because to modify UI objects it is needed to call runOnUIThread method
The important part here is contained inside tvsetText() to calculate RMS value from acceleration values
A
B
A) Write laquoimplements SensorEventListenerraquo ALT+Enter laquoImplement methodsraquo OK Callbacks onSensorChanged e onAccuracyChanged will be automatically created
ANDROID APP SENSOR LEARN BY EXAMPLE
15Sistemi di Elettronica Digitale A Flammini
4) RUN
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
16Sistemi di Elettronica Digitale A Flammini
A solution from Google Android Things Sensors (Things) are equipped with Android
(eg Smartwatch) itrsquos easy to develop APPS
for Things management
Currently available only for few devices
Unsuitable for low-cost low-power things
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
17Sistemi di Elettronica Digitale A Flammini
SAndroidE rarr External sensors = Internal ones Oriented to interface things by Bluetooth Low-Energy (BLE)
Allows to build custom-logic APPs to interact with external things
Supports devices from multiple vendors
Free and Open Source
Examples of supported devices
Official website httpes3unibsitSAndroidE
Github httpsgithubcomSAndroidEOfficial
Arduino RaspBerry Redbear Nano Flic Beacons
Sensory
systemFitbit
SAndroidE
18Sistemi di Elettronica Digitale A Flammini
SAndroidE- does not require rooting- simply use Android BLE
stack
BLE = Bluetooth Low Energy
Internal
sensors
HW HW
Driver
Input subsystem Kernel
Libraries Sensor HAL
Application level
Applications
SAndroidE
BLE
External
sensors
Android Stack
Application
Framework
Activity Manager
SAndroidE components
19Sistemi di Elettronica Digitale A Flammini
bull SAndroidE library (Java)
Virtualizes remote devices
regardless of type and
operating mode
bull Device Descriptor Files (XMLJSON)
Represent a description
of the BT Services
provided by devices
bull API description files (XMLJSON)
Represent interface
to connect and retrieve
data from Cloud
bull SAndroidE configuration application
Used to detect nearby
BT devices and give
them a unique ID
All provided in the open-source code
SAndroidE supported devices
20Sistemi di Elettronica Digitale A Flammini
A User
programmable
devices
B Beacons and non-
programmable
devices
C Vendor Locked
device - data from
vendorrsquos APP
D Vendor Locked
device - data from
vendorrsquos Cloud
SAndroidE supported devices
21Sistemi di Elettronica Digitale A Flammini
A User programmable devices
B Beacons and
non-programmable devices
Devices like Arduino Raspberry Pi RedBear BLE Nano already
integrated
Typically require device flashing
With SAndroidE no need to write custom firmware for basic
operations
Flashing is not allowed but BT protocols are known
SAndroidE declares protocols in the Device Descriptor Files
In this way these devices can be easily virtualized
NOTE a BLE beacon does not need any coupling as it continuously broadcasts BLE messages to any device in range
BLE BEACONS
22Sistemi di Elettronica Digitale A Flammini
SAndroidE supported devices
23Sistemi di Elettronica Digitale A Flammini
C Vendor locked device ndash
data from vendorrsquosAPP
D Vendor locked device ndash
data from vendorrsquos
CLOUD
Some vendors apply an encrypted
BT protocol
Example Flic buttons
In this case SAndroidE works by
receiving notifications by vendorrsquos
APP
Some vendors provide data
only by accessing the
proprietary Cloud
Example Fitbit activity
trackers
SAndroidE retrieves such data
by means of API
SANDROIDE APP LEARN BY EXAMPLE
24Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVETo replace the text laquoHello Worldraquo with laquoHello World trimmer valueraquo
Furthermore we want to connect the button and the led not by wire but laquothrough the appraquo when the button is pressed the led must light on
QUI IMMAGINE BASETTA
SANDROIDE APP LEARN BY EXAMPLE
25Sistemi di Elettronica Digitale A Flammini
2) DOWNLOAD SANDROIDE amp PHONE CONFIGURATION
a) From httpsgithubcomSAndroidEOfficialframeworkreleases download the latest SAndroidE binary package sandroide-bin-v07zip
b) Decompress the zip file
c) Connect smartphone or tablet as mass storageUSB archive and copy within the smartphone internal memory these files
a) all xml files contained in the xml folder of the decompressed zip b) the file flasher-debugapk
d) Disconnect the smartphone and in laquoPhone Settings-gtSecurityraquo enable Installation from laquoUnknown Sourcesraquo
e) Using the laquoArchiveraquo or laquoFile Explorerraquo app of your Smartphone search and execute the file flasher-debugapk to to install it Accept all with OK
SANDROIDE APP LEARN BY EXAMPLE
26Sistemi di Elettronica Digitale A Flammini
3) FLASHING THE DEVICE WITH SANDROIDE COMPATIBILE FIRMWARE
The Nano device to interact with a SAndroidE APP must be provided with a firmware implementing the same communication protocol supported by SAndroidE on the smartphone side Letrsquos see how
a) Connect Nano to the PC using the appropriate USB programmer
b) From SandroidE wiki on Github httpsgithubcomSAndroidEOfficialframeworkwiki
access the Nano tutorial by clicking on laquoUsing RedBear Nano with SAndroidEraquo
c) In the section laquoFlashing the Nanoraquo click on laquoSandroide compatible Nano firmwareraquo and save the opened page (File-gtSave) in the MBEDDAPLINK drive
PLEASE PAY ATTENTION to save as hex and not as txt by selecting laquoAll filesraquo int the Saving form instead of laquoText fileraquo and by removing the suffix txt to the end of the file name
SANDROIDE APP LEARN BY EXAMPLE
27Sistemi di Elettronica Digitale A Flammini
4) ASSIGN AN ID TO THE NANO BY USING THE FLASHER APP
A) Launche the BleembeddedFlaser app installed at step 2 and click on laquoScan discoverable devicesraquo
B) Click on MyNano If there is more than one Nano verify characters on the label of the Nano and check that they match with the last 2 characters of the MAC address shown in the list
Warning Nano mujst be powered on Reset the power it still does not appear in the list
C) Wait until the device connects and disconnects A list of bluetooth services and characteristics provided is shown
D) Click on laquoStore attribute in description fileraquo and give it the name nanoesempio
REMEMBER THIS NAME
SANDROIDE APP LEARN BY EXAMPLE
28Sistemi di Elettronica Digitale A Flammini
5) CONNECT NANO AND COMPONENTS TO BREADBOARD AND POWER IT ON
PLEASE NOTE
- Led (only on Nano) can be connected withouth resistor because current is limited
- For SAndroidE the pin labels to consider are the blue ones if the label is P0_X then in SAndroidE you will have to use X
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
Android SensorySystems SAndroidE
Sistemi di elettronica digitale A Flammini AA2017-2018
HOW ANDROID APPs MANAGE INTERNAL SENSORS
3Sistemi di Elettronica Digitale A Flammini
Most Android-powered devices have built-in sensors that measure motion orientation and various environmental conditions These sensors are capable of providing raw data with high precision and accuracy and are useful if you want to monitor three-dimensional devicemovement or positioning or you want to monitor changes in the ambient environmentnear a device
The Android platform supports three broad categories of sensorsbull Motion sensors these sensors measure acceleration forces and rotational forces along
three axes This category includes accelerometers gravity sensors gyroscopes and rotational vector sensors
bull Environmental sensors these sensors measure various environmental parameters suchas ambient air temperature and pressure illumination and humidity This categoryincludes barometers photometers and thermometers
bull Position sensors these sensors measure the physical position of a device This categoryincludes orientation sensors and magnetometers
SENSOR CATEGORIES
4Sistemi di Elettronica Digitale A Flammini
HW based sensors
Physical component built into the device
The data is directly acquired measuring specific environmental properties
SW based sensors
Emulate a HW based sensor behavior
The data are derived from one or more HW based sensors
Smart Sensors for Domitics and Health Care Alessandra Flammini UNIBS
5
Sensor Type Description Common UsesTYPE_ACCELEROMETER
Hardware Measures the acceleration force in ms
2that is applied to a device on all
three physical axes (x y and z) including the force of gravity
Motion detection (shake tilt etc)
TYPE_AMBIENT_TEMPERATURE
Hardware Measures the ambient room temperature in degrees Celsius (degC) See note below
Monitoring air temperatures
TYPE_GRAVITY
Software or Hardware
Measures the force of gravity in ms
2that is applied to a device on all
three physical axes (x y z)
Motion detection (shake tilt etc)
TYPE_GYROSCOPE
Hardware Measures a devices rate of rotation in rads around each of the three physical axes (x y and z)
Rotation detection (spin turn etc)
TYPE_LIGHT Hardware Measures the ambient light level (illumination) in lx
Controlling screen brightness
TYPE_LINEAR_ACCELERATION
Software or Hardware
Measures the acceleration force in ms
2that is applied to a device on all
three physical axes (x y and z) excluding the force of gravity
Monitoring acceleration along a single axis
ANDROID SENSORS TYPES (1)
Smart Sensors for Domitics and Health Care Alessandra Flammini UNIBS
6
TYPE_ORIENTATION
Software Measures degrees of rotation that a device makes around all three physical axes (x y z) As of API level 3 you can obtain the inclination matrix and rotation matrix for a device by using the gravity sensor and the geomagnetic field sensor in conjunction with the getRotationMatrix() method
Determining device position
TYPE_PRESSURE
Hardware Measures the ambient air pressure in hPa or mbar
Monitoring air pressure changes
TYPE_PROXIMITY
Hardware Measures the proximity of an object in cm relative to the view screen of a device This sensor is typically used to determine whether a handset is being held up to a persons ear
Phone position during a call
TYPE_RELATIVE_HUMIDITY
Hardware Measures the relative ambient humidity in percent ()
Monitoring dewpoint absolute and relative humidity
ANDROID SENSORS TYPES (2)
SENSORS Accelerometer
7Sistemi di Elettronica Digitale A Flammini
Typically usages
screen orientation
inclination for game input
vibrations measurements
SENSORS GPS
8Sistemi di Elettronica Digitale A Flammini
Typically usages
Location
LaterationTriangulation of cell towers or wifinetworks (with database of known locations for towers and networks)
Location of associated cell tower or wifi network
Need connect to 3 satellites for 2D positioning 4 satellites for 3D positioning
More visible satellites increase precision
Typical precision 20-50m
Maximum precision 10m
SENSORS Gyroscope
9Sistemi di Elettronica Digitale A Flammini
Usages
Measurements of rate of rotation (angular speed)
3 values related to the axes
Pitch value (rotation around X axis)
Roll value (rotation around Y axis)
Yaw value (rotation around Z axis)
SENSOR FRAMEWORK
10Sistemi di Elettronica Digitale A Flammini
You can access these sensors and acquire raw sensor data by using the Android sensorframework The sensor framework is part of the androidhardware package and includesthe following classes and interfaces
bull SensorManager You can use this class to create an instance of the sensor service This class provides various methods for accessing and listing sensors registering and unregistering sensor event listeners This class also provides several sensor constantsthat are used to report sensor accuracy set data acquisition rates and calibrate sensors
bull Sensor You can use this class to create an instance of a specific sensor This classprovides various methods that let you determine a sensors capabilities
bull SensorEvent The system uses this class to create a sensor event object which providesinformation about a sensor event A sensor event object includes the followinginformation the raw sensor data the type of sensor that generated the event the accuracy of the data and the timestamp for the event
bull SensorEventListener You can use this interface to create two callback methods thatreceive notifications (sensor events) when sensor values change or when sensoraccuracy changes
httpsdeveloperandroidcomguidetopicssensorssensors_overviewhtml
SENSOR FRAMEWORK how to use
11Sistemi di Elettronica Digitale A Flammini
You can access sensor information by means of several methods
bull getResolution() sensor resolutionbull getMaximumRange() sensor maximum rangebull getPower() sensors power requirements requirementsbull getMinDelay() returns the minimum time interval (in microseconds) a sensor can use to
sense data Any sensor that returns a non-zero value for the getMinDelay() method is a streaming sensor otherwise it reports data only when there is a change in the parameters it is sensing
bull getVendor() sensor manufacturer
You can set sensor parameters by registerListener() methodbull SensorManagerSENSOR_DELAY_NORMAL sensor sampling interval of a streaming
sensor is set to NORMAL (default setting) resolution NORMAL = 200ms UI = 60ms GAME = 20ms FASTEST = 0ms asynchronous)
You can access sensor data and events by implementing a callbackbull onSensorChanged() a sensor reports a new value System provides you with a
SensorEvent object containing information (accuracy source timestamp value)
ANDROID APP SENSOR LEARN BY EXAMPLE
12Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVEModify the text laquoHello Worldraquo with laquoHello World RMS = sqrt( (x^2 + y^2 + z^2)3)raquo where RMS is the Root Mean Square
Objective is to insert some code within the MainActivityjava file which subscribes for accelerometer values and calculates and visualizes the Root Mean Square value to the user
ANDROID APP SENSOR LEARN BY EXAMPLE
13Sistemi di Elettronica Digitale A Flammini
2) SUBSCRIPTION TO SENSORMANAGER
To get values from accelerometer is is necessary to subscribe for receiving accelerometer sensor events from the SensorManager which is the service providing access to internal sensors in Android
A) Create variable which will contain the SensorManager object
B) The SensorManager service is assigned to the variable
C) A subscription for accelerometer sensor is requested with delay normal (200ms) and the current activity(this) will be the target responsible for handling sensor events containing acceleration values
A
BC Warning this will be
red coloured until next step
ANDROID APP SENSOR LEARN BY EXAMPLE
14Sistemi di Elettronica Digitale A Flammini
3) IMPLEMENT RECEIVING CALLBACKS
B) Fill the onSensorChanged with the code to change the text of the TextView Warning code seems complicated because to modify UI objects it is needed to call runOnUIThread method
The important part here is contained inside tvsetText() to calculate RMS value from acceleration values
A
B
A) Write laquoimplements SensorEventListenerraquo ALT+Enter laquoImplement methodsraquo OK Callbacks onSensorChanged e onAccuracyChanged will be automatically created
ANDROID APP SENSOR LEARN BY EXAMPLE
15Sistemi di Elettronica Digitale A Flammini
4) RUN
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
16Sistemi di Elettronica Digitale A Flammini
A solution from Google Android Things Sensors (Things) are equipped with Android
(eg Smartwatch) itrsquos easy to develop APPS
for Things management
Currently available only for few devices
Unsuitable for low-cost low-power things
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
17Sistemi di Elettronica Digitale A Flammini
SAndroidE rarr External sensors = Internal ones Oriented to interface things by Bluetooth Low-Energy (BLE)
Allows to build custom-logic APPs to interact with external things
Supports devices from multiple vendors
Free and Open Source
Examples of supported devices
Official website httpes3unibsitSAndroidE
Github httpsgithubcomSAndroidEOfficial
Arduino RaspBerry Redbear Nano Flic Beacons
Sensory
systemFitbit
SAndroidE
18Sistemi di Elettronica Digitale A Flammini
SAndroidE- does not require rooting- simply use Android BLE
stack
BLE = Bluetooth Low Energy
Internal
sensors
HW HW
Driver
Input subsystem Kernel
Libraries Sensor HAL
Application level
Applications
SAndroidE
BLE
External
sensors
Android Stack
Application
Framework
Activity Manager
SAndroidE components
19Sistemi di Elettronica Digitale A Flammini
bull SAndroidE library (Java)
Virtualizes remote devices
regardless of type and
operating mode
bull Device Descriptor Files (XMLJSON)
Represent a description
of the BT Services
provided by devices
bull API description files (XMLJSON)
Represent interface
to connect and retrieve
data from Cloud
bull SAndroidE configuration application
Used to detect nearby
BT devices and give
them a unique ID
All provided in the open-source code
SAndroidE supported devices
20Sistemi di Elettronica Digitale A Flammini
A User
programmable
devices
B Beacons and non-
programmable
devices
C Vendor Locked
device - data from
vendorrsquos APP
D Vendor Locked
device - data from
vendorrsquos Cloud
SAndroidE supported devices
21Sistemi di Elettronica Digitale A Flammini
A User programmable devices
B Beacons and
non-programmable devices
Devices like Arduino Raspberry Pi RedBear BLE Nano already
integrated
Typically require device flashing
With SAndroidE no need to write custom firmware for basic
operations
Flashing is not allowed but BT protocols are known
SAndroidE declares protocols in the Device Descriptor Files
In this way these devices can be easily virtualized
NOTE a BLE beacon does not need any coupling as it continuously broadcasts BLE messages to any device in range
BLE BEACONS
22Sistemi di Elettronica Digitale A Flammini
SAndroidE supported devices
23Sistemi di Elettronica Digitale A Flammini
C Vendor locked device ndash
data from vendorrsquosAPP
D Vendor locked device ndash
data from vendorrsquos
CLOUD
Some vendors apply an encrypted
BT protocol
Example Flic buttons
In this case SAndroidE works by
receiving notifications by vendorrsquos
APP
Some vendors provide data
only by accessing the
proprietary Cloud
Example Fitbit activity
trackers
SAndroidE retrieves such data
by means of API
SANDROIDE APP LEARN BY EXAMPLE
24Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVETo replace the text laquoHello Worldraquo with laquoHello World trimmer valueraquo
Furthermore we want to connect the button and the led not by wire but laquothrough the appraquo when the button is pressed the led must light on
QUI IMMAGINE BASETTA
SANDROIDE APP LEARN BY EXAMPLE
25Sistemi di Elettronica Digitale A Flammini
2) DOWNLOAD SANDROIDE amp PHONE CONFIGURATION
a) From httpsgithubcomSAndroidEOfficialframeworkreleases download the latest SAndroidE binary package sandroide-bin-v07zip
b) Decompress the zip file
c) Connect smartphone or tablet as mass storageUSB archive and copy within the smartphone internal memory these files
a) all xml files contained in the xml folder of the decompressed zip b) the file flasher-debugapk
d) Disconnect the smartphone and in laquoPhone Settings-gtSecurityraquo enable Installation from laquoUnknown Sourcesraquo
e) Using the laquoArchiveraquo or laquoFile Explorerraquo app of your Smartphone search and execute the file flasher-debugapk to to install it Accept all with OK
SANDROIDE APP LEARN BY EXAMPLE
26Sistemi di Elettronica Digitale A Flammini
3) FLASHING THE DEVICE WITH SANDROIDE COMPATIBILE FIRMWARE
The Nano device to interact with a SAndroidE APP must be provided with a firmware implementing the same communication protocol supported by SAndroidE on the smartphone side Letrsquos see how
a) Connect Nano to the PC using the appropriate USB programmer
b) From SandroidE wiki on Github httpsgithubcomSAndroidEOfficialframeworkwiki
access the Nano tutorial by clicking on laquoUsing RedBear Nano with SAndroidEraquo
c) In the section laquoFlashing the Nanoraquo click on laquoSandroide compatible Nano firmwareraquo and save the opened page (File-gtSave) in the MBEDDAPLINK drive
PLEASE PAY ATTENTION to save as hex and not as txt by selecting laquoAll filesraquo int the Saving form instead of laquoText fileraquo and by removing the suffix txt to the end of the file name
SANDROIDE APP LEARN BY EXAMPLE
27Sistemi di Elettronica Digitale A Flammini
4) ASSIGN AN ID TO THE NANO BY USING THE FLASHER APP
A) Launche the BleembeddedFlaser app installed at step 2 and click on laquoScan discoverable devicesraquo
B) Click on MyNano If there is more than one Nano verify characters on the label of the Nano and check that they match with the last 2 characters of the MAC address shown in the list
Warning Nano mujst be powered on Reset the power it still does not appear in the list
C) Wait until the device connects and disconnects A list of bluetooth services and characteristics provided is shown
D) Click on laquoStore attribute in description fileraquo and give it the name nanoesempio
REMEMBER THIS NAME
SANDROIDE APP LEARN BY EXAMPLE
28Sistemi di Elettronica Digitale A Flammini
5) CONNECT NANO AND COMPONENTS TO BREADBOARD AND POWER IT ON
PLEASE NOTE
- Led (only on Nano) can be connected withouth resistor because current is limited
- For SAndroidE the pin labels to consider are the blue ones if the label is P0_X then in SAndroidE you will have to use X
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
HOW ANDROID APPs MANAGE INTERNAL SENSORS
3Sistemi di Elettronica Digitale A Flammini
Most Android-powered devices have built-in sensors that measure motion orientation and various environmental conditions These sensors are capable of providing raw data with high precision and accuracy and are useful if you want to monitor three-dimensional devicemovement or positioning or you want to monitor changes in the ambient environmentnear a device
The Android platform supports three broad categories of sensorsbull Motion sensors these sensors measure acceleration forces and rotational forces along
three axes This category includes accelerometers gravity sensors gyroscopes and rotational vector sensors
bull Environmental sensors these sensors measure various environmental parameters suchas ambient air temperature and pressure illumination and humidity This categoryincludes barometers photometers and thermometers
bull Position sensors these sensors measure the physical position of a device This categoryincludes orientation sensors and magnetometers
SENSOR CATEGORIES
4Sistemi di Elettronica Digitale A Flammini
HW based sensors
Physical component built into the device
The data is directly acquired measuring specific environmental properties
SW based sensors
Emulate a HW based sensor behavior
The data are derived from one or more HW based sensors
Smart Sensors for Domitics and Health Care Alessandra Flammini UNIBS
5
Sensor Type Description Common UsesTYPE_ACCELEROMETER
Hardware Measures the acceleration force in ms
2that is applied to a device on all
three physical axes (x y and z) including the force of gravity
Motion detection (shake tilt etc)
TYPE_AMBIENT_TEMPERATURE
Hardware Measures the ambient room temperature in degrees Celsius (degC) See note below
Monitoring air temperatures
TYPE_GRAVITY
Software or Hardware
Measures the force of gravity in ms
2that is applied to a device on all
three physical axes (x y z)
Motion detection (shake tilt etc)
TYPE_GYROSCOPE
Hardware Measures a devices rate of rotation in rads around each of the three physical axes (x y and z)
Rotation detection (spin turn etc)
TYPE_LIGHT Hardware Measures the ambient light level (illumination) in lx
Controlling screen brightness
TYPE_LINEAR_ACCELERATION
Software or Hardware
Measures the acceleration force in ms
2that is applied to a device on all
three physical axes (x y and z) excluding the force of gravity
Monitoring acceleration along a single axis
ANDROID SENSORS TYPES (1)
Smart Sensors for Domitics and Health Care Alessandra Flammini UNIBS
6
TYPE_ORIENTATION
Software Measures degrees of rotation that a device makes around all three physical axes (x y z) As of API level 3 you can obtain the inclination matrix and rotation matrix for a device by using the gravity sensor and the geomagnetic field sensor in conjunction with the getRotationMatrix() method
Determining device position
TYPE_PRESSURE
Hardware Measures the ambient air pressure in hPa or mbar
Monitoring air pressure changes
TYPE_PROXIMITY
Hardware Measures the proximity of an object in cm relative to the view screen of a device This sensor is typically used to determine whether a handset is being held up to a persons ear
Phone position during a call
TYPE_RELATIVE_HUMIDITY
Hardware Measures the relative ambient humidity in percent ()
Monitoring dewpoint absolute and relative humidity
ANDROID SENSORS TYPES (2)
SENSORS Accelerometer
7Sistemi di Elettronica Digitale A Flammini
Typically usages
screen orientation
inclination for game input
vibrations measurements
SENSORS GPS
8Sistemi di Elettronica Digitale A Flammini
Typically usages
Location
LaterationTriangulation of cell towers or wifinetworks (with database of known locations for towers and networks)
Location of associated cell tower or wifi network
Need connect to 3 satellites for 2D positioning 4 satellites for 3D positioning
More visible satellites increase precision
Typical precision 20-50m
Maximum precision 10m
SENSORS Gyroscope
9Sistemi di Elettronica Digitale A Flammini
Usages
Measurements of rate of rotation (angular speed)
3 values related to the axes
Pitch value (rotation around X axis)
Roll value (rotation around Y axis)
Yaw value (rotation around Z axis)
SENSOR FRAMEWORK
10Sistemi di Elettronica Digitale A Flammini
You can access these sensors and acquire raw sensor data by using the Android sensorframework The sensor framework is part of the androidhardware package and includesthe following classes and interfaces
bull SensorManager You can use this class to create an instance of the sensor service This class provides various methods for accessing and listing sensors registering and unregistering sensor event listeners This class also provides several sensor constantsthat are used to report sensor accuracy set data acquisition rates and calibrate sensors
bull Sensor You can use this class to create an instance of a specific sensor This classprovides various methods that let you determine a sensors capabilities
bull SensorEvent The system uses this class to create a sensor event object which providesinformation about a sensor event A sensor event object includes the followinginformation the raw sensor data the type of sensor that generated the event the accuracy of the data and the timestamp for the event
bull SensorEventListener You can use this interface to create two callback methods thatreceive notifications (sensor events) when sensor values change or when sensoraccuracy changes
httpsdeveloperandroidcomguidetopicssensorssensors_overviewhtml
SENSOR FRAMEWORK how to use
11Sistemi di Elettronica Digitale A Flammini
You can access sensor information by means of several methods
bull getResolution() sensor resolutionbull getMaximumRange() sensor maximum rangebull getPower() sensors power requirements requirementsbull getMinDelay() returns the minimum time interval (in microseconds) a sensor can use to
sense data Any sensor that returns a non-zero value for the getMinDelay() method is a streaming sensor otherwise it reports data only when there is a change in the parameters it is sensing
bull getVendor() sensor manufacturer
You can set sensor parameters by registerListener() methodbull SensorManagerSENSOR_DELAY_NORMAL sensor sampling interval of a streaming
sensor is set to NORMAL (default setting) resolution NORMAL = 200ms UI = 60ms GAME = 20ms FASTEST = 0ms asynchronous)
You can access sensor data and events by implementing a callbackbull onSensorChanged() a sensor reports a new value System provides you with a
SensorEvent object containing information (accuracy source timestamp value)
ANDROID APP SENSOR LEARN BY EXAMPLE
12Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVEModify the text laquoHello Worldraquo with laquoHello World RMS = sqrt( (x^2 + y^2 + z^2)3)raquo where RMS is the Root Mean Square
Objective is to insert some code within the MainActivityjava file which subscribes for accelerometer values and calculates and visualizes the Root Mean Square value to the user
ANDROID APP SENSOR LEARN BY EXAMPLE
13Sistemi di Elettronica Digitale A Flammini
2) SUBSCRIPTION TO SENSORMANAGER
To get values from accelerometer is is necessary to subscribe for receiving accelerometer sensor events from the SensorManager which is the service providing access to internal sensors in Android
A) Create variable which will contain the SensorManager object
B) The SensorManager service is assigned to the variable
C) A subscription for accelerometer sensor is requested with delay normal (200ms) and the current activity(this) will be the target responsible for handling sensor events containing acceleration values
A
BC Warning this will be
red coloured until next step
ANDROID APP SENSOR LEARN BY EXAMPLE
14Sistemi di Elettronica Digitale A Flammini
3) IMPLEMENT RECEIVING CALLBACKS
B) Fill the onSensorChanged with the code to change the text of the TextView Warning code seems complicated because to modify UI objects it is needed to call runOnUIThread method
The important part here is contained inside tvsetText() to calculate RMS value from acceleration values
A
B
A) Write laquoimplements SensorEventListenerraquo ALT+Enter laquoImplement methodsraquo OK Callbacks onSensorChanged e onAccuracyChanged will be automatically created
ANDROID APP SENSOR LEARN BY EXAMPLE
15Sistemi di Elettronica Digitale A Flammini
4) RUN
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
16Sistemi di Elettronica Digitale A Flammini
A solution from Google Android Things Sensors (Things) are equipped with Android
(eg Smartwatch) itrsquos easy to develop APPS
for Things management
Currently available only for few devices
Unsuitable for low-cost low-power things
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
17Sistemi di Elettronica Digitale A Flammini
SAndroidE rarr External sensors = Internal ones Oriented to interface things by Bluetooth Low-Energy (BLE)
Allows to build custom-logic APPs to interact with external things
Supports devices from multiple vendors
Free and Open Source
Examples of supported devices
Official website httpes3unibsitSAndroidE
Github httpsgithubcomSAndroidEOfficial
Arduino RaspBerry Redbear Nano Flic Beacons
Sensory
systemFitbit
SAndroidE
18Sistemi di Elettronica Digitale A Flammini
SAndroidE- does not require rooting- simply use Android BLE
stack
BLE = Bluetooth Low Energy
Internal
sensors
HW HW
Driver
Input subsystem Kernel
Libraries Sensor HAL
Application level
Applications
SAndroidE
BLE
External
sensors
Android Stack
Application
Framework
Activity Manager
SAndroidE components
19Sistemi di Elettronica Digitale A Flammini
bull SAndroidE library (Java)
Virtualizes remote devices
regardless of type and
operating mode
bull Device Descriptor Files (XMLJSON)
Represent a description
of the BT Services
provided by devices
bull API description files (XMLJSON)
Represent interface
to connect and retrieve
data from Cloud
bull SAndroidE configuration application
Used to detect nearby
BT devices and give
them a unique ID
All provided in the open-source code
SAndroidE supported devices
20Sistemi di Elettronica Digitale A Flammini
A User
programmable
devices
B Beacons and non-
programmable
devices
C Vendor Locked
device - data from
vendorrsquos APP
D Vendor Locked
device - data from
vendorrsquos Cloud
SAndroidE supported devices
21Sistemi di Elettronica Digitale A Flammini
A User programmable devices
B Beacons and
non-programmable devices
Devices like Arduino Raspberry Pi RedBear BLE Nano already
integrated
Typically require device flashing
With SAndroidE no need to write custom firmware for basic
operations
Flashing is not allowed but BT protocols are known
SAndroidE declares protocols in the Device Descriptor Files
In this way these devices can be easily virtualized
NOTE a BLE beacon does not need any coupling as it continuously broadcasts BLE messages to any device in range
BLE BEACONS
22Sistemi di Elettronica Digitale A Flammini
SAndroidE supported devices
23Sistemi di Elettronica Digitale A Flammini
C Vendor locked device ndash
data from vendorrsquosAPP
D Vendor locked device ndash
data from vendorrsquos
CLOUD
Some vendors apply an encrypted
BT protocol
Example Flic buttons
In this case SAndroidE works by
receiving notifications by vendorrsquos
APP
Some vendors provide data
only by accessing the
proprietary Cloud
Example Fitbit activity
trackers
SAndroidE retrieves such data
by means of API
SANDROIDE APP LEARN BY EXAMPLE
24Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVETo replace the text laquoHello Worldraquo with laquoHello World trimmer valueraquo
Furthermore we want to connect the button and the led not by wire but laquothrough the appraquo when the button is pressed the led must light on
QUI IMMAGINE BASETTA
SANDROIDE APP LEARN BY EXAMPLE
25Sistemi di Elettronica Digitale A Flammini
2) DOWNLOAD SANDROIDE amp PHONE CONFIGURATION
a) From httpsgithubcomSAndroidEOfficialframeworkreleases download the latest SAndroidE binary package sandroide-bin-v07zip
b) Decompress the zip file
c) Connect smartphone or tablet as mass storageUSB archive and copy within the smartphone internal memory these files
a) all xml files contained in the xml folder of the decompressed zip b) the file flasher-debugapk
d) Disconnect the smartphone and in laquoPhone Settings-gtSecurityraquo enable Installation from laquoUnknown Sourcesraquo
e) Using the laquoArchiveraquo or laquoFile Explorerraquo app of your Smartphone search and execute the file flasher-debugapk to to install it Accept all with OK
SANDROIDE APP LEARN BY EXAMPLE
26Sistemi di Elettronica Digitale A Flammini
3) FLASHING THE DEVICE WITH SANDROIDE COMPATIBILE FIRMWARE
The Nano device to interact with a SAndroidE APP must be provided with a firmware implementing the same communication protocol supported by SAndroidE on the smartphone side Letrsquos see how
a) Connect Nano to the PC using the appropriate USB programmer
b) From SandroidE wiki on Github httpsgithubcomSAndroidEOfficialframeworkwiki
access the Nano tutorial by clicking on laquoUsing RedBear Nano with SAndroidEraquo
c) In the section laquoFlashing the Nanoraquo click on laquoSandroide compatible Nano firmwareraquo and save the opened page (File-gtSave) in the MBEDDAPLINK drive
PLEASE PAY ATTENTION to save as hex and not as txt by selecting laquoAll filesraquo int the Saving form instead of laquoText fileraquo and by removing the suffix txt to the end of the file name
SANDROIDE APP LEARN BY EXAMPLE
27Sistemi di Elettronica Digitale A Flammini
4) ASSIGN AN ID TO THE NANO BY USING THE FLASHER APP
A) Launche the BleembeddedFlaser app installed at step 2 and click on laquoScan discoverable devicesraquo
B) Click on MyNano If there is more than one Nano verify characters on the label of the Nano and check that they match with the last 2 characters of the MAC address shown in the list
Warning Nano mujst be powered on Reset the power it still does not appear in the list
C) Wait until the device connects and disconnects A list of bluetooth services and characteristics provided is shown
D) Click on laquoStore attribute in description fileraquo and give it the name nanoesempio
REMEMBER THIS NAME
SANDROIDE APP LEARN BY EXAMPLE
28Sistemi di Elettronica Digitale A Flammini
5) CONNECT NANO AND COMPONENTS TO BREADBOARD AND POWER IT ON
PLEASE NOTE
- Led (only on Nano) can be connected withouth resistor because current is limited
- For SAndroidE the pin labels to consider are the blue ones if the label is P0_X then in SAndroidE you will have to use X
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SENSOR CATEGORIES
4Sistemi di Elettronica Digitale A Flammini
HW based sensors
Physical component built into the device
The data is directly acquired measuring specific environmental properties
SW based sensors
Emulate a HW based sensor behavior
The data are derived from one or more HW based sensors
Smart Sensors for Domitics and Health Care Alessandra Flammini UNIBS
5
Sensor Type Description Common UsesTYPE_ACCELEROMETER
Hardware Measures the acceleration force in ms
2that is applied to a device on all
three physical axes (x y and z) including the force of gravity
Motion detection (shake tilt etc)
TYPE_AMBIENT_TEMPERATURE
Hardware Measures the ambient room temperature in degrees Celsius (degC) See note below
Monitoring air temperatures
TYPE_GRAVITY
Software or Hardware
Measures the force of gravity in ms
2that is applied to a device on all
three physical axes (x y z)
Motion detection (shake tilt etc)
TYPE_GYROSCOPE
Hardware Measures a devices rate of rotation in rads around each of the three physical axes (x y and z)
Rotation detection (spin turn etc)
TYPE_LIGHT Hardware Measures the ambient light level (illumination) in lx
Controlling screen brightness
TYPE_LINEAR_ACCELERATION
Software or Hardware
Measures the acceleration force in ms
2that is applied to a device on all
three physical axes (x y and z) excluding the force of gravity
Monitoring acceleration along a single axis
ANDROID SENSORS TYPES (1)
Smart Sensors for Domitics and Health Care Alessandra Flammini UNIBS
6
TYPE_ORIENTATION
Software Measures degrees of rotation that a device makes around all three physical axes (x y z) As of API level 3 you can obtain the inclination matrix and rotation matrix for a device by using the gravity sensor and the geomagnetic field sensor in conjunction with the getRotationMatrix() method
Determining device position
TYPE_PRESSURE
Hardware Measures the ambient air pressure in hPa or mbar
Monitoring air pressure changes
TYPE_PROXIMITY
Hardware Measures the proximity of an object in cm relative to the view screen of a device This sensor is typically used to determine whether a handset is being held up to a persons ear
Phone position during a call
TYPE_RELATIVE_HUMIDITY
Hardware Measures the relative ambient humidity in percent ()
Monitoring dewpoint absolute and relative humidity
ANDROID SENSORS TYPES (2)
SENSORS Accelerometer
7Sistemi di Elettronica Digitale A Flammini
Typically usages
screen orientation
inclination for game input
vibrations measurements
SENSORS GPS
8Sistemi di Elettronica Digitale A Flammini
Typically usages
Location
LaterationTriangulation of cell towers or wifinetworks (with database of known locations for towers and networks)
Location of associated cell tower or wifi network
Need connect to 3 satellites for 2D positioning 4 satellites for 3D positioning
More visible satellites increase precision
Typical precision 20-50m
Maximum precision 10m
SENSORS Gyroscope
9Sistemi di Elettronica Digitale A Flammini
Usages
Measurements of rate of rotation (angular speed)
3 values related to the axes
Pitch value (rotation around X axis)
Roll value (rotation around Y axis)
Yaw value (rotation around Z axis)
SENSOR FRAMEWORK
10Sistemi di Elettronica Digitale A Flammini
You can access these sensors and acquire raw sensor data by using the Android sensorframework The sensor framework is part of the androidhardware package and includesthe following classes and interfaces
bull SensorManager You can use this class to create an instance of the sensor service This class provides various methods for accessing and listing sensors registering and unregistering sensor event listeners This class also provides several sensor constantsthat are used to report sensor accuracy set data acquisition rates and calibrate sensors
bull Sensor You can use this class to create an instance of a specific sensor This classprovides various methods that let you determine a sensors capabilities
bull SensorEvent The system uses this class to create a sensor event object which providesinformation about a sensor event A sensor event object includes the followinginformation the raw sensor data the type of sensor that generated the event the accuracy of the data and the timestamp for the event
bull SensorEventListener You can use this interface to create two callback methods thatreceive notifications (sensor events) when sensor values change or when sensoraccuracy changes
httpsdeveloperandroidcomguidetopicssensorssensors_overviewhtml
SENSOR FRAMEWORK how to use
11Sistemi di Elettronica Digitale A Flammini
You can access sensor information by means of several methods
bull getResolution() sensor resolutionbull getMaximumRange() sensor maximum rangebull getPower() sensors power requirements requirementsbull getMinDelay() returns the minimum time interval (in microseconds) a sensor can use to
sense data Any sensor that returns a non-zero value for the getMinDelay() method is a streaming sensor otherwise it reports data only when there is a change in the parameters it is sensing
bull getVendor() sensor manufacturer
You can set sensor parameters by registerListener() methodbull SensorManagerSENSOR_DELAY_NORMAL sensor sampling interval of a streaming
sensor is set to NORMAL (default setting) resolution NORMAL = 200ms UI = 60ms GAME = 20ms FASTEST = 0ms asynchronous)
You can access sensor data and events by implementing a callbackbull onSensorChanged() a sensor reports a new value System provides you with a
SensorEvent object containing information (accuracy source timestamp value)
ANDROID APP SENSOR LEARN BY EXAMPLE
12Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVEModify the text laquoHello Worldraquo with laquoHello World RMS = sqrt( (x^2 + y^2 + z^2)3)raquo where RMS is the Root Mean Square
Objective is to insert some code within the MainActivityjava file which subscribes for accelerometer values and calculates and visualizes the Root Mean Square value to the user
ANDROID APP SENSOR LEARN BY EXAMPLE
13Sistemi di Elettronica Digitale A Flammini
2) SUBSCRIPTION TO SENSORMANAGER
To get values from accelerometer is is necessary to subscribe for receiving accelerometer sensor events from the SensorManager which is the service providing access to internal sensors in Android
A) Create variable which will contain the SensorManager object
B) The SensorManager service is assigned to the variable
C) A subscription for accelerometer sensor is requested with delay normal (200ms) and the current activity(this) will be the target responsible for handling sensor events containing acceleration values
A
BC Warning this will be
red coloured until next step
ANDROID APP SENSOR LEARN BY EXAMPLE
14Sistemi di Elettronica Digitale A Flammini
3) IMPLEMENT RECEIVING CALLBACKS
B) Fill the onSensorChanged with the code to change the text of the TextView Warning code seems complicated because to modify UI objects it is needed to call runOnUIThread method
The important part here is contained inside tvsetText() to calculate RMS value from acceleration values
A
B
A) Write laquoimplements SensorEventListenerraquo ALT+Enter laquoImplement methodsraquo OK Callbacks onSensorChanged e onAccuracyChanged will be automatically created
ANDROID APP SENSOR LEARN BY EXAMPLE
15Sistemi di Elettronica Digitale A Flammini
4) RUN
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
16Sistemi di Elettronica Digitale A Flammini
A solution from Google Android Things Sensors (Things) are equipped with Android
(eg Smartwatch) itrsquos easy to develop APPS
for Things management
Currently available only for few devices
Unsuitable for low-cost low-power things
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
17Sistemi di Elettronica Digitale A Flammini
SAndroidE rarr External sensors = Internal ones Oriented to interface things by Bluetooth Low-Energy (BLE)
Allows to build custom-logic APPs to interact with external things
Supports devices from multiple vendors
Free and Open Source
Examples of supported devices
Official website httpes3unibsitSAndroidE
Github httpsgithubcomSAndroidEOfficial
Arduino RaspBerry Redbear Nano Flic Beacons
Sensory
systemFitbit
SAndroidE
18Sistemi di Elettronica Digitale A Flammini
SAndroidE- does not require rooting- simply use Android BLE
stack
BLE = Bluetooth Low Energy
Internal
sensors
HW HW
Driver
Input subsystem Kernel
Libraries Sensor HAL
Application level
Applications
SAndroidE
BLE
External
sensors
Android Stack
Application
Framework
Activity Manager
SAndroidE components
19Sistemi di Elettronica Digitale A Flammini
bull SAndroidE library (Java)
Virtualizes remote devices
regardless of type and
operating mode
bull Device Descriptor Files (XMLJSON)
Represent a description
of the BT Services
provided by devices
bull API description files (XMLJSON)
Represent interface
to connect and retrieve
data from Cloud
bull SAndroidE configuration application
Used to detect nearby
BT devices and give
them a unique ID
All provided in the open-source code
SAndroidE supported devices
20Sistemi di Elettronica Digitale A Flammini
A User
programmable
devices
B Beacons and non-
programmable
devices
C Vendor Locked
device - data from
vendorrsquos APP
D Vendor Locked
device - data from
vendorrsquos Cloud
SAndroidE supported devices
21Sistemi di Elettronica Digitale A Flammini
A User programmable devices
B Beacons and
non-programmable devices
Devices like Arduino Raspberry Pi RedBear BLE Nano already
integrated
Typically require device flashing
With SAndroidE no need to write custom firmware for basic
operations
Flashing is not allowed but BT protocols are known
SAndroidE declares protocols in the Device Descriptor Files
In this way these devices can be easily virtualized
NOTE a BLE beacon does not need any coupling as it continuously broadcasts BLE messages to any device in range
BLE BEACONS
22Sistemi di Elettronica Digitale A Flammini
SAndroidE supported devices
23Sistemi di Elettronica Digitale A Flammini
C Vendor locked device ndash
data from vendorrsquosAPP
D Vendor locked device ndash
data from vendorrsquos
CLOUD
Some vendors apply an encrypted
BT protocol
Example Flic buttons
In this case SAndroidE works by
receiving notifications by vendorrsquos
APP
Some vendors provide data
only by accessing the
proprietary Cloud
Example Fitbit activity
trackers
SAndroidE retrieves such data
by means of API
SANDROIDE APP LEARN BY EXAMPLE
24Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVETo replace the text laquoHello Worldraquo with laquoHello World trimmer valueraquo
Furthermore we want to connect the button and the led not by wire but laquothrough the appraquo when the button is pressed the led must light on
QUI IMMAGINE BASETTA
SANDROIDE APP LEARN BY EXAMPLE
25Sistemi di Elettronica Digitale A Flammini
2) DOWNLOAD SANDROIDE amp PHONE CONFIGURATION
a) From httpsgithubcomSAndroidEOfficialframeworkreleases download the latest SAndroidE binary package sandroide-bin-v07zip
b) Decompress the zip file
c) Connect smartphone or tablet as mass storageUSB archive and copy within the smartphone internal memory these files
a) all xml files contained in the xml folder of the decompressed zip b) the file flasher-debugapk
d) Disconnect the smartphone and in laquoPhone Settings-gtSecurityraquo enable Installation from laquoUnknown Sourcesraquo
e) Using the laquoArchiveraquo or laquoFile Explorerraquo app of your Smartphone search and execute the file flasher-debugapk to to install it Accept all with OK
SANDROIDE APP LEARN BY EXAMPLE
26Sistemi di Elettronica Digitale A Flammini
3) FLASHING THE DEVICE WITH SANDROIDE COMPATIBILE FIRMWARE
The Nano device to interact with a SAndroidE APP must be provided with a firmware implementing the same communication protocol supported by SAndroidE on the smartphone side Letrsquos see how
a) Connect Nano to the PC using the appropriate USB programmer
b) From SandroidE wiki on Github httpsgithubcomSAndroidEOfficialframeworkwiki
access the Nano tutorial by clicking on laquoUsing RedBear Nano with SAndroidEraquo
c) In the section laquoFlashing the Nanoraquo click on laquoSandroide compatible Nano firmwareraquo and save the opened page (File-gtSave) in the MBEDDAPLINK drive
PLEASE PAY ATTENTION to save as hex and not as txt by selecting laquoAll filesraquo int the Saving form instead of laquoText fileraquo and by removing the suffix txt to the end of the file name
SANDROIDE APP LEARN BY EXAMPLE
27Sistemi di Elettronica Digitale A Flammini
4) ASSIGN AN ID TO THE NANO BY USING THE FLASHER APP
A) Launche the BleembeddedFlaser app installed at step 2 and click on laquoScan discoverable devicesraquo
B) Click on MyNano If there is more than one Nano verify characters on the label of the Nano and check that they match with the last 2 characters of the MAC address shown in the list
Warning Nano mujst be powered on Reset the power it still does not appear in the list
C) Wait until the device connects and disconnects A list of bluetooth services and characteristics provided is shown
D) Click on laquoStore attribute in description fileraquo and give it the name nanoesempio
REMEMBER THIS NAME
SANDROIDE APP LEARN BY EXAMPLE
28Sistemi di Elettronica Digitale A Flammini
5) CONNECT NANO AND COMPONENTS TO BREADBOARD AND POWER IT ON
PLEASE NOTE
- Led (only on Nano) can be connected withouth resistor because current is limited
- For SAndroidE the pin labels to consider are the blue ones if the label is P0_X then in SAndroidE you will have to use X
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
Smart Sensors for Domitics and Health Care Alessandra Flammini UNIBS
5
Sensor Type Description Common UsesTYPE_ACCELEROMETER
Hardware Measures the acceleration force in ms
2that is applied to a device on all
three physical axes (x y and z) including the force of gravity
Motion detection (shake tilt etc)
TYPE_AMBIENT_TEMPERATURE
Hardware Measures the ambient room temperature in degrees Celsius (degC) See note below
Monitoring air temperatures
TYPE_GRAVITY
Software or Hardware
Measures the force of gravity in ms
2that is applied to a device on all
three physical axes (x y z)
Motion detection (shake tilt etc)
TYPE_GYROSCOPE
Hardware Measures a devices rate of rotation in rads around each of the three physical axes (x y and z)
Rotation detection (spin turn etc)
TYPE_LIGHT Hardware Measures the ambient light level (illumination) in lx
Controlling screen brightness
TYPE_LINEAR_ACCELERATION
Software or Hardware
Measures the acceleration force in ms
2that is applied to a device on all
three physical axes (x y and z) excluding the force of gravity
Monitoring acceleration along a single axis
ANDROID SENSORS TYPES (1)
Smart Sensors for Domitics and Health Care Alessandra Flammini UNIBS
6
TYPE_ORIENTATION
Software Measures degrees of rotation that a device makes around all three physical axes (x y z) As of API level 3 you can obtain the inclination matrix and rotation matrix for a device by using the gravity sensor and the geomagnetic field sensor in conjunction with the getRotationMatrix() method
Determining device position
TYPE_PRESSURE
Hardware Measures the ambient air pressure in hPa or mbar
Monitoring air pressure changes
TYPE_PROXIMITY
Hardware Measures the proximity of an object in cm relative to the view screen of a device This sensor is typically used to determine whether a handset is being held up to a persons ear
Phone position during a call
TYPE_RELATIVE_HUMIDITY
Hardware Measures the relative ambient humidity in percent ()
Monitoring dewpoint absolute and relative humidity
ANDROID SENSORS TYPES (2)
SENSORS Accelerometer
7Sistemi di Elettronica Digitale A Flammini
Typically usages
screen orientation
inclination for game input
vibrations measurements
SENSORS GPS
8Sistemi di Elettronica Digitale A Flammini
Typically usages
Location
LaterationTriangulation of cell towers or wifinetworks (with database of known locations for towers and networks)
Location of associated cell tower or wifi network
Need connect to 3 satellites for 2D positioning 4 satellites for 3D positioning
More visible satellites increase precision
Typical precision 20-50m
Maximum precision 10m
SENSORS Gyroscope
9Sistemi di Elettronica Digitale A Flammini
Usages
Measurements of rate of rotation (angular speed)
3 values related to the axes
Pitch value (rotation around X axis)
Roll value (rotation around Y axis)
Yaw value (rotation around Z axis)
SENSOR FRAMEWORK
10Sistemi di Elettronica Digitale A Flammini
You can access these sensors and acquire raw sensor data by using the Android sensorframework The sensor framework is part of the androidhardware package and includesthe following classes and interfaces
bull SensorManager You can use this class to create an instance of the sensor service This class provides various methods for accessing and listing sensors registering and unregistering sensor event listeners This class also provides several sensor constantsthat are used to report sensor accuracy set data acquisition rates and calibrate sensors
bull Sensor You can use this class to create an instance of a specific sensor This classprovides various methods that let you determine a sensors capabilities
bull SensorEvent The system uses this class to create a sensor event object which providesinformation about a sensor event A sensor event object includes the followinginformation the raw sensor data the type of sensor that generated the event the accuracy of the data and the timestamp for the event
bull SensorEventListener You can use this interface to create two callback methods thatreceive notifications (sensor events) when sensor values change or when sensoraccuracy changes
httpsdeveloperandroidcomguidetopicssensorssensors_overviewhtml
SENSOR FRAMEWORK how to use
11Sistemi di Elettronica Digitale A Flammini
You can access sensor information by means of several methods
bull getResolution() sensor resolutionbull getMaximumRange() sensor maximum rangebull getPower() sensors power requirements requirementsbull getMinDelay() returns the minimum time interval (in microseconds) a sensor can use to
sense data Any sensor that returns a non-zero value for the getMinDelay() method is a streaming sensor otherwise it reports data only when there is a change in the parameters it is sensing
bull getVendor() sensor manufacturer
You can set sensor parameters by registerListener() methodbull SensorManagerSENSOR_DELAY_NORMAL sensor sampling interval of a streaming
sensor is set to NORMAL (default setting) resolution NORMAL = 200ms UI = 60ms GAME = 20ms FASTEST = 0ms asynchronous)
You can access sensor data and events by implementing a callbackbull onSensorChanged() a sensor reports a new value System provides you with a
SensorEvent object containing information (accuracy source timestamp value)
ANDROID APP SENSOR LEARN BY EXAMPLE
12Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVEModify the text laquoHello Worldraquo with laquoHello World RMS = sqrt( (x^2 + y^2 + z^2)3)raquo where RMS is the Root Mean Square
Objective is to insert some code within the MainActivityjava file which subscribes for accelerometer values and calculates and visualizes the Root Mean Square value to the user
ANDROID APP SENSOR LEARN BY EXAMPLE
13Sistemi di Elettronica Digitale A Flammini
2) SUBSCRIPTION TO SENSORMANAGER
To get values from accelerometer is is necessary to subscribe for receiving accelerometer sensor events from the SensorManager which is the service providing access to internal sensors in Android
A) Create variable which will contain the SensorManager object
B) The SensorManager service is assigned to the variable
C) A subscription for accelerometer sensor is requested with delay normal (200ms) and the current activity(this) will be the target responsible for handling sensor events containing acceleration values
A
BC Warning this will be
red coloured until next step
ANDROID APP SENSOR LEARN BY EXAMPLE
14Sistemi di Elettronica Digitale A Flammini
3) IMPLEMENT RECEIVING CALLBACKS
B) Fill the onSensorChanged with the code to change the text of the TextView Warning code seems complicated because to modify UI objects it is needed to call runOnUIThread method
The important part here is contained inside tvsetText() to calculate RMS value from acceleration values
A
B
A) Write laquoimplements SensorEventListenerraquo ALT+Enter laquoImplement methodsraquo OK Callbacks onSensorChanged e onAccuracyChanged will be automatically created
ANDROID APP SENSOR LEARN BY EXAMPLE
15Sistemi di Elettronica Digitale A Flammini
4) RUN
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
16Sistemi di Elettronica Digitale A Flammini
A solution from Google Android Things Sensors (Things) are equipped with Android
(eg Smartwatch) itrsquos easy to develop APPS
for Things management
Currently available only for few devices
Unsuitable for low-cost low-power things
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
17Sistemi di Elettronica Digitale A Flammini
SAndroidE rarr External sensors = Internal ones Oriented to interface things by Bluetooth Low-Energy (BLE)
Allows to build custom-logic APPs to interact with external things
Supports devices from multiple vendors
Free and Open Source
Examples of supported devices
Official website httpes3unibsitSAndroidE
Github httpsgithubcomSAndroidEOfficial
Arduino RaspBerry Redbear Nano Flic Beacons
Sensory
systemFitbit
SAndroidE
18Sistemi di Elettronica Digitale A Flammini
SAndroidE- does not require rooting- simply use Android BLE
stack
BLE = Bluetooth Low Energy
Internal
sensors
HW HW
Driver
Input subsystem Kernel
Libraries Sensor HAL
Application level
Applications
SAndroidE
BLE
External
sensors
Android Stack
Application
Framework
Activity Manager
SAndroidE components
19Sistemi di Elettronica Digitale A Flammini
bull SAndroidE library (Java)
Virtualizes remote devices
regardless of type and
operating mode
bull Device Descriptor Files (XMLJSON)
Represent a description
of the BT Services
provided by devices
bull API description files (XMLJSON)
Represent interface
to connect and retrieve
data from Cloud
bull SAndroidE configuration application
Used to detect nearby
BT devices and give
them a unique ID
All provided in the open-source code
SAndroidE supported devices
20Sistemi di Elettronica Digitale A Flammini
A User
programmable
devices
B Beacons and non-
programmable
devices
C Vendor Locked
device - data from
vendorrsquos APP
D Vendor Locked
device - data from
vendorrsquos Cloud
SAndroidE supported devices
21Sistemi di Elettronica Digitale A Flammini
A User programmable devices
B Beacons and
non-programmable devices
Devices like Arduino Raspberry Pi RedBear BLE Nano already
integrated
Typically require device flashing
With SAndroidE no need to write custom firmware for basic
operations
Flashing is not allowed but BT protocols are known
SAndroidE declares protocols in the Device Descriptor Files
In this way these devices can be easily virtualized
NOTE a BLE beacon does not need any coupling as it continuously broadcasts BLE messages to any device in range
BLE BEACONS
22Sistemi di Elettronica Digitale A Flammini
SAndroidE supported devices
23Sistemi di Elettronica Digitale A Flammini
C Vendor locked device ndash
data from vendorrsquosAPP
D Vendor locked device ndash
data from vendorrsquos
CLOUD
Some vendors apply an encrypted
BT protocol
Example Flic buttons
In this case SAndroidE works by
receiving notifications by vendorrsquos
APP
Some vendors provide data
only by accessing the
proprietary Cloud
Example Fitbit activity
trackers
SAndroidE retrieves such data
by means of API
SANDROIDE APP LEARN BY EXAMPLE
24Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVETo replace the text laquoHello Worldraquo with laquoHello World trimmer valueraquo
Furthermore we want to connect the button and the led not by wire but laquothrough the appraquo when the button is pressed the led must light on
QUI IMMAGINE BASETTA
SANDROIDE APP LEARN BY EXAMPLE
25Sistemi di Elettronica Digitale A Flammini
2) DOWNLOAD SANDROIDE amp PHONE CONFIGURATION
a) From httpsgithubcomSAndroidEOfficialframeworkreleases download the latest SAndroidE binary package sandroide-bin-v07zip
b) Decompress the zip file
c) Connect smartphone or tablet as mass storageUSB archive and copy within the smartphone internal memory these files
a) all xml files contained in the xml folder of the decompressed zip b) the file flasher-debugapk
d) Disconnect the smartphone and in laquoPhone Settings-gtSecurityraquo enable Installation from laquoUnknown Sourcesraquo
e) Using the laquoArchiveraquo or laquoFile Explorerraquo app of your Smartphone search and execute the file flasher-debugapk to to install it Accept all with OK
SANDROIDE APP LEARN BY EXAMPLE
26Sistemi di Elettronica Digitale A Flammini
3) FLASHING THE DEVICE WITH SANDROIDE COMPATIBILE FIRMWARE
The Nano device to interact with a SAndroidE APP must be provided with a firmware implementing the same communication protocol supported by SAndroidE on the smartphone side Letrsquos see how
a) Connect Nano to the PC using the appropriate USB programmer
b) From SandroidE wiki on Github httpsgithubcomSAndroidEOfficialframeworkwiki
access the Nano tutorial by clicking on laquoUsing RedBear Nano with SAndroidEraquo
c) In the section laquoFlashing the Nanoraquo click on laquoSandroide compatible Nano firmwareraquo and save the opened page (File-gtSave) in the MBEDDAPLINK drive
PLEASE PAY ATTENTION to save as hex and not as txt by selecting laquoAll filesraquo int the Saving form instead of laquoText fileraquo and by removing the suffix txt to the end of the file name
SANDROIDE APP LEARN BY EXAMPLE
27Sistemi di Elettronica Digitale A Flammini
4) ASSIGN AN ID TO THE NANO BY USING THE FLASHER APP
A) Launche the BleembeddedFlaser app installed at step 2 and click on laquoScan discoverable devicesraquo
B) Click on MyNano If there is more than one Nano verify characters on the label of the Nano and check that they match with the last 2 characters of the MAC address shown in the list
Warning Nano mujst be powered on Reset the power it still does not appear in the list
C) Wait until the device connects and disconnects A list of bluetooth services and characteristics provided is shown
D) Click on laquoStore attribute in description fileraquo and give it the name nanoesempio
REMEMBER THIS NAME
SANDROIDE APP LEARN BY EXAMPLE
28Sistemi di Elettronica Digitale A Flammini
5) CONNECT NANO AND COMPONENTS TO BREADBOARD AND POWER IT ON
PLEASE NOTE
- Led (only on Nano) can be connected withouth resistor because current is limited
- For SAndroidE the pin labels to consider are the blue ones if the label is P0_X then in SAndroidE you will have to use X
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
Smart Sensors for Domitics and Health Care Alessandra Flammini UNIBS
6
TYPE_ORIENTATION
Software Measures degrees of rotation that a device makes around all three physical axes (x y z) As of API level 3 you can obtain the inclination matrix and rotation matrix for a device by using the gravity sensor and the geomagnetic field sensor in conjunction with the getRotationMatrix() method
Determining device position
TYPE_PRESSURE
Hardware Measures the ambient air pressure in hPa or mbar
Monitoring air pressure changes
TYPE_PROXIMITY
Hardware Measures the proximity of an object in cm relative to the view screen of a device This sensor is typically used to determine whether a handset is being held up to a persons ear
Phone position during a call
TYPE_RELATIVE_HUMIDITY
Hardware Measures the relative ambient humidity in percent ()
Monitoring dewpoint absolute and relative humidity
ANDROID SENSORS TYPES (2)
SENSORS Accelerometer
7Sistemi di Elettronica Digitale A Flammini
Typically usages
screen orientation
inclination for game input
vibrations measurements
SENSORS GPS
8Sistemi di Elettronica Digitale A Flammini
Typically usages
Location
LaterationTriangulation of cell towers or wifinetworks (with database of known locations for towers and networks)
Location of associated cell tower or wifi network
Need connect to 3 satellites for 2D positioning 4 satellites for 3D positioning
More visible satellites increase precision
Typical precision 20-50m
Maximum precision 10m
SENSORS Gyroscope
9Sistemi di Elettronica Digitale A Flammini
Usages
Measurements of rate of rotation (angular speed)
3 values related to the axes
Pitch value (rotation around X axis)
Roll value (rotation around Y axis)
Yaw value (rotation around Z axis)
SENSOR FRAMEWORK
10Sistemi di Elettronica Digitale A Flammini
You can access these sensors and acquire raw sensor data by using the Android sensorframework The sensor framework is part of the androidhardware package and includesthe following classes and interfaces
bull SensorManager You can use this class to create an instance of the sensor service This class provides various methods for accessing and listing sensors registering and unregistering sensor event listeners This class also provides several sensor constantsthat are used to report sensor accuracy set data acquisition rates and calibrate sensors
bull Sensor You can use this class to create an instance of a specific sensor This classprovides various methods that let you determine a sensors capabilities
bull SensorEvent The system uses this class to create a sensor event object which providesinformation about a sensor event A sensor event object includes the followinginformation the raw sensor data the type of sensor that generated the event the accuracy of the data and the timestamp for the event
bull SensorEventListener You can use this interface to create two callback methods thatreceive notifications (sensor events) when sensor values change or when sensoraccuracy changes
httpsdeveloperandroidcomguidetopicssensorssensors_overviewhtml
SENSOR FRAMEWORK how to use
11Sistemi di Elettronica Digitale A Flammini
You can access sensor information by means of several methods
bull getResolution() sensor resolutionbull getMaximumRange() sensor maximum rangebull getPower() sensors power requirements requirementsbull getMinDelay() returns the minimum time interval (in microseconds) a sensor can use to
sense data Any sensor that returns a non-zero value for the getMinDelay() method is a streaming sensor otherwise it reports data only when there is a change in the parameters it is sensing
bull getVendor() sensor manufacturer
You can set sensor parameters by registerListener() methodbull SensorManagerSENSOR_DELAY_NORMAL sensor sampling interval of a streaming
sensor is set to NORMAL (default setting) resolution NORMAL = 200ms UI = 60ms GAME = 20ms FASTEST = 0ms asynchronous)
You can access sensor data and events by implementing a callbackbull onSensorChanged() a sensor reports a new value System provides you with a
SensorEvent object containing information (accuracy source timestamp value)
ANDROID APP SENSOR LEARN BY EXAMPLE
12Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVEModify the text laquoHello Worldraquo with laquoHello World RMS = sqrt( (x^2 + y^2 + z^2)3)raquo where RMS is the Root Mean Square
Objective is to insert some code within the MainActivityjava file which subscribes for accelerometer values and calculates and visualizes the Root Mean Square value to the user
ANDROID APP SENSOR LEARN BY EXAMPLE
13Sistemi di Elettronica Digitale A Flammini
2) SUBSCRIPTION TO SENSORMANAGER
To get values from accelerometer is is necessary to subscribe for receiving accelerometer sensor events from the SensorManager which is the service providing access to internal sensors in Android
A) Create variable which will contain the SensorManager object
B) The SensorManager service is assigned to the variable
C) A subscription for accelerometer sensor is requested with delay normal (200ms) and the current activity(this) will be the target responsible for handling sensor events containing acceleration values
A
BC Warning this will be
red coloured until next step
ANDROID APP SENSOR LEARN BY EXAMPLE
14Sistemi di Elettronica Digitale A Flammini
3) IMPLEMENT RECEIVING CALLBACKS
B) Fill the onSensorChanged with the code to change the text of the TextView Warning code seems complicated because to modify UI objects it is needed to call runOnUIThread method
The important part here is contained inside tvsetText() to calculate RMS value from acceleration values
A
B
A) Write laquoimplements SensorEventListenerraquo ALT+Enter laquoImplement methodsraquo OK Callbacks onSensorChanged e onAccuracyChanged will be automatically created
ANDROID APP SENSOR LEARN BY EXAMPLE
15Sistemi di Elettronica Digitale A Flammini
4) RUN
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
16Sistemi di Elettronica Digitale A Flammini
A solution from Google Android Things Sensors (Things) are equipped with Android
(eg Smartwatch) itrsquos easy to develop APPS
for Things management
Currently available only for few devices
Unsuitable for low-cost low-power things
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
17Sistemi di Elettronica Digitale A Flammini
SAndroidE rarr External sensors = Internal ones Oriented to interface things by Bluetooth Low-Energy (BLE)
Allows to build custom-logic APPs to interact with external things
Supports devices from multiple vendors
Free and Open Source
Examples of supported devices
Official website httpes3unibsitSAndroidE
Github httpsgithubcomSAndroidEOfficial
Arduino RaspBerry Redbear Nano Flic Beacons
Sensory
systemFitbit
SAndroidE
18Sistemi di Elettronica Digitale A Flammini
SAndroidE- does not require rooting- simply use Android BLE
stack
BLE = Bluetooth Low Energy
Internal
sensors
HW HW
Driver
Input subsystem Kernel
Libraries Sensor HAL
Application level
Applications
SAndroidE
BLE
External
sensors
Android Stack
Application
Framework
Activity Manager
SAndroidE components
19Sistemi di Elettronica Digitale A Flammini
bull SAndroidE library (Java)
Virtualizes remote devices
regardless of type and
operating mode
bull Device Descriptor Files (XMLJSON)
Represent a description
of the BT Services
provided by devices
bull API description files (XMLJSON)
Represent interface
to connect and retrieve
data from Cloud
bull SAndroidE configuration application
Used to detect nearby
BT devices and give
them a unique ID
All provided in the open-source code
SAndroidE supported devices
20Sistemi di Elettronica Digitale A Flammini
A User
programmable
devices
B Beacons and non-
programmable
devices
C Vendor Locked
device - data from
vendorrsquos APP
D Vendor Locked
device - data from
vendorrsquos Cloud
SAndroidE supported devices
21Sistemi di Elettronica Digitale A Flammini
A User programmable devices
B Beacons and
non-programmable devices
Devices like Arduino Raspberry Pi RedBear BLE Nano already
integrated
Typically require device flashing
With SAndroidE no need to write custom firmware for basic
operations
Flashing is not allowed but BT protocols are known
SAndroidE declares protocols in the Device Descriptor Files
In this way these devices can be easily virtualized
NOTE a BLE beacon does not need any coupling as it continuously broadcasts BLE messages to any device in range
BLE BEACONS
22Sistemi di Elettronica Digitale A Flammini
SAndroidE supported devices
23Sistemi di Elettronica Digitale A Flammini
C Vendor locked device ndash
data from vendorrsquosAPP
D Vendor locked device ndash
data from vendorrsquos
CLOUD
Some vendors apply an encrypted
BT protocol
Example Flic buttons
In this case SAndroidE works by
receiving notifications by vendorrsquos
APP
Some vendors provide data
only by accessing the
proprietary Cloud
Example Fitbit activity
trackers
SAndroidE retrieves such data
by means of API
SANDROIDE APP LEARN BY EXAMPLE
24Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVETo replace the text laquoHello Worldraquo with laquoHello World trimmer valueraquo
Furthermore we want to connect the button and the led not by wire but laquothrough the appraquo when the button is pressed the led must light on
QUI IMMAGINE BASETTA
SANDROIDE APP LEARN BY EXAMPLE
25Sistemi di Elettronica Digitale A Flammini
2) DOWNLOAD SANDROIDE amp PHONE CONFIGURATION
a) From httpsgithubcomSAndroidEOfficialframeworkreleases download the latest SAndroidE binary package sandroide-bin-v07zip
b) Decompress the zip file
c) Connect smartphone or tablet as mass storageUSB archive and copy within the smartphone internal memory these files
a) all xml files contained in the xml folder of the decompressed zip b) the file flasher-debugapk
d) Disconnect the smartphone and in laquoPhone Settings-gtSecurityraquo enable Installation from laquoUnknown Sourcesraquo
e) Using the laquoArchiveraquo or laquoFile Explorerraquo app of your Smartphone search and execute the file flasher-debugapk to to install it Accept all with OK
SANDROIDE APP LEARN BY EXAMPLE
26Sistemi di Elettronica Digitale A Flammini
3) FLASHING THE DEVICE WITH SANDROIDE COMPATIBILE FIRMWARE
The Nano device to interact with a SAndroidE APP must be provided with a firmware implementing the same communication protocol supported by SAndroidE on the smartphone side Letrsquos see how
a) Connect Nano to the PC using the appropriate USB programmer
b) From SandroidE wiki on Github httpsgithubcomSAndroidEOfficialframeworkwiki
access the Nano tutorial by clicking on laquoUsing RedBear Nano with SAndroidEraquo
c) In the section laquoFlashing the Nanoraquo click on laquoSandroide compatible Nano firmwareraquo and save the opened page (File-gtSave) in the MBEDDAPLINK drive
PLEASE PAY ATTENTION to save as hex and not as txt by selecting laquoAll filesraquo int the Saving form instead of laquoText fileraquo and by removing the suffix txt to the end of the file name
SANDROIDE APP LEARN BY EXAMPLE
27Sistemi di Elettronica Digitale A Flammini
4) ASSIGN AN ID TO THE NANO BY USING THE FLASHER APP
A) Launche the BleembeddedFlaser app installed at step 2 and click on laquoScan discoverable devicesraquo
B) Click on MyNano If there is more than one Nano verify characters on the label of the Nano and check that they match with the last 2 characters of the MAC address shown in the list
Warning Nano mujst be powered on Reset the power it still does not appear in the list
C) Wait until the device connects and disconnects A list of bluetooth services and characteristics provided is shown
D) Click on laquoStore attribute in description fileraquo and give it the name nanoesempio
REMEMBER THIS NAME
SANDROIDE APP LEARN BY EXAMPLE
28Sistemi di Elettronica Digitale A Flammini
5) CONNECT NANO AND COMPONENTS TO BREADBOARD AND POWER IT ON
PLEASE NOTE
- Led (only on Nano) can be connected withouth resistor because current is limited
- For SAndroidE the pin labels to consider are the blue ones if the label is P0_X then in SAndroidE you will have to use X
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SENSORS Accelerometer
7Sistemi di Elettronica Digitale A Flammini
Typically usages
screen orientation
inclination for game input
vibrations measurements
SENSORS GPS
8Sistemi di Elettronica Digitale A Flammini
Typically usages
Location
LaterationTriangulation of cell towers or wifinetworks (with database of known locations for towers and networks)
Location of associated cell tower or wifi network
Need connect to 3 satellites for 2D positioning 4 satellites for 3D positioning
More visible satellites increase precision
Typical precision 20-50m
Maximum precision 10m
SENSORS Gyroscope
9Sistemi di Elettronica Digitale A Flammini
Usages
Measurements of rate of rotation (angular speed)
3 values related to the axes
Pitch value (rotation around X axis)
Roll value (rotation around Y axis)
Yaw value (rotation around Z axis)
SENSOR FRAMEWORK
10Sistemi di Elettronica Digitale A Flammini
You can access these sensors and acquire raw sensor data by using the Android sensorframework The sensor framework is part of the androidhardware package and includesthe following classes and interfaces
bull SensorManager You can use this class to create an instance of the sensor service This class provides various methods for accessing and listing sensors registering and unregistering sensor event listeners This class also provides several sensor constantsthat are used to report sensor accuracy set data acquisition rates and calibrate sensors
bull Sensor You can use this class to create an instance of a specific sensor This classprovides various methods that let you determine a sensors capabilities
bull SensorEvent The system uses this class to create a sensor event object which providesinformation about a sensor event A sensor event object includes the followinginformation the raw sensor data the type of sensor that generated the event the accuracy of the data and the timestamp for the event
bull SensorEventListener You can use this interface to create two callback methods thatreceive notifications (sensor events) when sensor values change or when sensoraccuracy changes
httpsdeveloperandroidcomguidetopicssensorssensors_overviewhtml
SENSOR FRAMEWORK how to use
11Sistemi di Elettronica Digitale A Flammini
You can access sensor information by means of several methods
bull getResolution() sensor resolutionbull getMaximumRange() sensor maximum rangebull getPower() sensors power requirements requirementsbull getMinDelay() returns the minimum time interval (in microseconds) a sensor can use to
sense data Any sensor that returns a non-zero value for the getMinDelay() method is a streaming sensor otherwise it reports data only when there is a change in the parameters it is sensing
bull getVendor() sensor manufacturer
You can set sensor parameters by registerListener() methodbull SensorManagerSENSOR_DELAY_NORMAL sensor sampling interval of a streaming
sensor is set to NORMAL (default setting) resolution NORMAL = 200ms UI = 60ms GAME = 20ms FASTEST = 0ms asynchronous)
You can access sensor data and events by implementing a callbackbull onSensorChanged() a sensor reports a new value System provides you with a
SensorEvent object containing information (accuracy source timestamp value)
ANDROID APP SENSOR LEARN BY EXAMPLE
12Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVEModify the text laquoHello Worldraquo with laquoHello World RMS = sqrt( (x^2 + y^2 + z^2)3)raquo where RMS is the Root Mean Square
Objective is to insert some code within the MainActivityjava file which subscribes for accelerometer values and calculates and visualizes the Root Mean Square value to the user
ANDROID APP SENSOR LEARN BY EXAMPLE
13Sistemi di Elettronica Digitale A Flammini
2) SUBSCRIPTION TO SENSORMANAGER
To get values from accelerometer is is necessary to subscribe for receiving accelerometer sensor events from the SensorManager which is the service providing access to internal sensors in Android
A) Create variable which will contain the SensorManager object
B) The SensorManager service is assigned to the variable
C) A subscription for accelerometer sensor is requested with delay normal (200ms) and the current activity(this) will be the target responsible for handling sensor events containing acceleration values
A
BC Warning this will be
red coloured until next step
ANDROID APP SENSOR LEARN BY EXAMPLE
14Sistemi di Elettronica Digitale A Flammini
3) IMPLEMENT RECEIVING CALLBACKS
B) Fill the onSensorChanged with the code to change the text of the TextView Warning code seems complicated because to modify UI objects it is needed to call runOnUIThread method
The important part here is contained inside tvsetText() to calculate RMS value from acceleration values
A
B
A) Write laquoimplements SensorEventListenerraquo ALT+Enter laquoImplement methodsraquo OK Callbacks onSensorChanged e onAccuracyChanged will be automatically created
ANDROID APP SENSOR LEARN BY EXAMPLE
15Sistemi di Elettronica Digitale A Flammini
4) RUN
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
16Sistemi di Elettronica Digitale A Flammini
A solution from Google Android Things Sensors (Things) are equipped with Android
(eg Smartwatch) itrsquos easy to develop APPS
for Things management
Currently available only for few devices
Unsuitable for low-cost low-power things
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
17Sistemi di Elettronica Digitale A Flammini
SAndroidE rarr External sensors = Internal ones Oriented to interface things by Bluetooth Low-Energy (BLE)
Allows to build custom-logic APPs to interact with external things
Supports devices from multiple vendors
Free and Open Source
Examples of supported devices
Official website httpes3unibsitSAndroidE
Github httpsgithubcomSAndroidEOfficial
Arduino RaspBerry Redbear Nano Flic Beacons
Sensory
systemFitbit
SAndroidE
18Sistemi di Elettronica Digitale A Flammini
SAndroidE- does not require rooting- simply use Android BLE
stack
BLE = Bluetooth Low Energy
Internal
sensors
HW HW
Driver
Input subsystem Kernel
Libraries Sensor HAL
Application level
Applications
SAndroidE
BLE
External
sensors
Android Stack
Application
Framework
Activity Manager
SAndroidE components
19Sistemi di Elettronica Digitale A Flammini
bull SAndroidE library (Java)
Virtualizes remote devices
regardless of type and
operating mode
bull Device Descriptor Files (XMLJSON)
Represent a description
of the BT Services
provided by devices
bull API description files (XMLJSON)
Represent interface
to connect and retrieve
data from Cloud
bull SAndroidE configuration application
Used to detect nearby
BT devices and give
them a unique ID
All provided in the open-source code
SAndroidE supported devices
20Sistemi di Elettronica Digitale A Flammini
A User
programmable
devices
B Beacons and non-
programmable
devices
C Vendor Locked
device - data from
vendorrsquos APP
D Vendor Locked
device - data from
vendorrsquos Cloud
SAndroidE supported devices
21Sistemi di Elettronica Digitale A Flammini
A User programmable devices
B Beacons and
non-programmable devices
Devices like Arduino Raspberry Pi RedBear BLE Nano already
integrated
Typically require device flashing
With SAndroidE no need to write custom firmware for basic
operations
Flashing is not allowed but BT protocols are known
SAndroidE declares protocols in the Device Descriptor Files
In this way these devices can be easily virtualized
NOTE a BLE beacon does not need any coupling as it continuously broadcasts BLE messages to any device in range
BLE BEACONS
22Sistemi di Elettronica Digitale A Flammini
SAndroidE supported devices
23Sistemi di Elettronica Digitale A Flammini
C Vendor locked device ndash
data from vendorrsquosAPP
D Vendor locked device ndash
data from vendorrsquos
CLOUD
Some vendors apply an encrypted
BT protocol
Example Flic buttons
In this case SAndroidE works by
receiving notifications by vendorrsquos
APP
Some vendors provide data
only by accessing the
proprietary Cloud
Example Fitbit activity
trackers
SAndroidE retrieves such data
by means of API
SANDROIDE APP LEARN BY EXAMPLE
24Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVETo replace the text laquoHello Worldraquo with laquoHello World trimmer valueraquo
Furthermore we want to connect the button and the led not by wire but laquothrough the appraquo when the button is pressed the led must light on
QUI IMMAGINE BASETTA
SANDROIDE APP LEARN BY EXAMPLE
25Sistemi di Elettronica Digitale A Flammini
2) DOWNLOAD SANDROIDE amp PHONE CONFIGURATION
a) From httpsgithubcomSAndroidEOfficialframeworkreleases download the latest SAndroidE binary package sandroide-bin-v07zip
b) Decompress the zip file
c) Connect smartphone or tablet as mass storageUSB archive and copy within the smartphone internal memory these files
a) all xml files contained in the xml folder of the decompressed zip b) the file flasher-debugapk
d) Disconnect the smartphone and in laquoPhone Settings-gtSecurityraquo enable Installation from laquoUnknown Sourcesraquo
e) Using the laquoArchiveraquo or laquoFile Explorerraquo app of your Smartphone search and execute the file flasher-debugapk to to install it Accept all with OK
SANDROIDE APP LEARN BY EXAMPLE
26Sistemi di Elettronica Digitale A Flammini
3) FLASHING THE DEVICE WITH SANDROIDE COMPATIBILE FIRMWARE
The Nano device to interact with a SAndroidE APP must be provided with a firmware implementing the same communication protocol supported by SAndroidE on the smartphone side Letrsquos see how
a) Connect Nano to the PC using the appropriate USB programmer
b) From SandroidE wiki on Github httpsgithubcomSAndroidEOfficialframeworkwiki
access the Nano tutorial by clicking on laquoUsing RedBear Nano with SAndroidEraquo
c) In the section laquoFlashing the Nanoraquo click on laquoSandroide compatible Nano firmwareraquo and save the opened page (File-gtSave) in the MBEDDAPLINK drive
PLEASE PAY ATTENTION to save as hex and not as txt by selecting laquoAll filesraquo int the Saving form instead of laquoText fileraquo and by removing the suffix txt to the end of the file name
SANDROIDE APP LEARN BY EXAMPLE
27Sistemi di Elettronica Digitale A Flammini
4) ASSIGN AN ID TO THE NANO BY USING THE FLASHER APP
A) Launche the BleembeddedFlaser app installed at step 2 and click on laquoScan discoverable devicesraquo
B) Click on MyNano If there is more than one Nano verify characters on the label of the Nano and check that they match with the last 2 characters of the MAC address shown in the list
Warning Nano mujst be powered on Reset the power it still does not appear in the list
C) Wait until the device connects and disconnects A list of bluetooth services and characteristics provided is shown
D) Click on laquoStore attribute in description fileraquo and give it the name nanoesempio
REMEMBER THIS NAME
SANDROIDE APP LEARN BY EXAMPLE
28Sistemi di Elettronica Digitale A Flammini
5) CONNECT NANO AND COMPONENTS TO BREADBOARD AND POWER IT ON
PLEASE NOTE
- Led (only on Nano) can be connected withouth resistor because current is limited
- For SAndroidE the pin labels to consider are the blue ones if the label is P0_X then in SAndroidE you will have to use X
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SENSORS GPS
8Sistemi di Elettronica Digitale A Flammini
Typically usages
Location
LaterationTriangulation of cell towers or wifinetworks (with database of known locations for towers and networks)
Location of associated cell tower or wifi network
Need connect to 3 satellites for 2D positioning 4 satellites for 3D positioning
More visible satellites increase precision
Typical precision 20-50m
Maximum precision 10m
SENSORS Gyroscope
9Sistemi di Elettronica Digitale A Flammini
Usages
Measurements of rate of rotation (angular speed)
3 values related to the axes
Pitch value (rotation around X axis)
Roll value (rotation around Y axis)
Yaw value (rotation around Z axis)
SENSOR FRAMEWORK
10Sistemi di Elettronica Digitale A Flammini
You can access these sensors and acquire raw sensor data by using the Android sensorframework The sensor framework is part of the androidhardware package and includesthe following classes and interfaces
bull SensorManager You can use this class to create an instance of the sensor service This class provides various methods for accessing and listing sensors registering and unregistering sensor event listeners This class also provides several sensor constantsthat are used to report sensor accuracy set data acquisition rates and calibrate sensors
bull Sensor You can use this class to create an instance of a specific sensor This classprovides various methods that let you determine a sensors capabilities
bull SensorEvent The system uses this class to create a sensor event object which providesinformation about a sensor event A sensor event object includes the followinginformation the raw sensor data the type of sensor that generated the event the accuracy of the data and the timestamp for the event
bull SensorEventListener You can use this interface to create two callback methods thatreceive notifications (sensor events) when sensor values change or when sensoraccuracy changes
httpsdeveloperandroidcomguidetopicssensorssensors_overviewhtml
SENSOR FRAMEWORK how to use
11Sistemi di Elettronica Digitale A Flammini
You can access sensor information by means of several methods
bull getResolution() sensor resolutionbull getMaximumRange() sensor maximum rangebull getPower() sensors power requirements requirementsbull getMinDelay() returns the minimum time interval (in microseconds) a sensor can use to
sense data Any sensor that returns a non-zero value for the getMinDelay() method is a streaming sensor otherwise it reports data only when there is a change in the parameters it is sensing
bull getVendor() sensor manufacturer
You can set sensor parameters by registerListener() methodbull SensorManagerSENSOR_DELAY_NORMAL sensor sampling interval of a streaming
sensor is set to NORMAL (default setting) resolution NORMAL = 200ms UI = 60ms GAME = 20ms FASTEST = 0ms asynchronous)
You can access sensor data and events by implementing a callbackbull onSensorChanged() a sensor reports a new value System provides you with a
SensorEvent object containing information (accuracy source timestamp value)
ANDROID APP SENSOR LEARN BY EXAMPLE
12Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVEModify the text laquoHello Worldraquo with laquoHello World RMS = sqrt( (x^2 + y^2 + z^2)3)raquo where RMS is the Root Mean Square
Objective is to insert some code within the MainActivityjava file which subscribes for accelerometer values and calculates and visualizes the Root Mean Square value to the user
ANDROID APP SENSOR LEARN BY EXAMPLE
13Sistemi di Elettronica Digitale A Flammini
2) SUBSCRIPTION TO SENSORMANAGER
To get values from accelerometer is is necessary to subscribe for receiving accelerometer sensor events from the SensorManager which is the service providing access to internal sensors in Android
A) Create variable which will contain the SensorManager object
B) The SensorManager service is assigned to the variable
C) A subscription for accelerometer sensor is requested with delay normal (200ms) and the current activity(this) will be the target responsible for handling sensor events containing acceleration values
A
BC Warning this will be
red coloured until next step
ANDROID APP SENSOR LEARN BY EXAMPLE
14Sistemi di Elettronica Digitale A Flammini
3) IMPLEMENT RECEIVING CALLBACKS
B) Fill the onSensorChanged with the code to change the text of the TextView Warning code seems complicated because to modify UI objects it is needed to call runOnUIThread method
The important part here is contained inside tvsetText() to calculate RMS value from acceleration values
A
B
A) Write laquoimplements SensorEventListenerraquo ALT+Enter laquoImplement methodsraquo OK Callbacks onSensorChanged e onAccuracyChanged will be automatically created
ANDROID APP SENSOR LEARN BY EXAMPLE
15Sistemi di Elettronica Digitale A Flammini
4) RUN
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
16Sistemi di Elettronica Digitale A Flammini
A solution from Google Android Things Sensors (Things) are equipped with Android
(eg Smartwatch) itrsquos easy to develop APPS
for Things management
Currently available only for few devices
Unsuitable for low-cost low-power things
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
17Sistemi di Elettronica Digitale A Flammini
SAndroidE rarr External sensors = Internal ones Oriented to interface things by Bluetooth Low-Energy (BLE)
Allows to build custom-logic APPs to interact with external things
Supports devices from multiple vendors
Free and Open Source
Examples of supported devices
Official website httpes3unibsitSAndroidE
Github httpsgithubcomSAndroidEOfficial
Arduino RaspBerry Redbear Nano Flic Beacons
Sensory
systemFitbit
SAndroidE
18Sistemi di Elettronica Digitale A Flammini
SAndroidE- does not require rooting- simply use Android BLE
stack
BLE = Bluetooth Low Energy
Internal
sensors
HW HW
Driver
Input subsystem Kernel
Libraries Sensor HAL
Application level
Applications
SAndroidE
BLE
External
sensors
Android Stack
Application
Framework
Activity Manager
SAndroidE components
19Sistemi di Elettronica Digitale A Flammini
bull SAndroidE library (Java)
Virtualizes remote devices
regardless of type and
operating mode
bull Device Descriptor Files (XMLJSON)
Represent a description
of the BT Services
provided by devices
bull API description files (XMLJSON)
Represent interface
to connect and retrieve
data from Cloud
bull SAndroidE configuration application
Used to detect nearby
BT devices and give
them a unique ID
All provided in the open-source code
SAndroidE supported devices
20Sistemi di Elettronica Digitale A Flammini
A User
programmable
devices
B Beacons and non-
programmable
devices
C Vendor Locked
device - data from
vendorrsquos APP
D Vendor Locked
device - data from
vendorrsquos Cloud
SAndroidE supported devices
21Sistemi di Elettronica Digitale A Flammini
A User programmable devices
B Beacons and
non-programmable devices
Devices like Arduino Raspberry Pi RedBear BLE Nano already
integrated
Typically require device flashing
With SAndroidE no need to write custom firmware for basic
operations
Flashing is not allowed but BT protocols are known
SAndroidE declares protocols in the Device Descriptor Files
In this way these devices can be easily virtualized
NOTE a BLE beacon does not need any coupling as it continuously broadcasts BLE messages to any device in range
BLE BEACONS
22Sistemi di Elettronica Digitale A Flammini
SAndroidE supported devices
23Sistemi di Elettronica Digitale A Flammini
C Vendor locked device ndash
data from vendorrsquosAPP
D Vendor locked device ndash
data from vendorrsquos
CLOUD
Some vendors apply an encrypted
BT protocol
Example Flic buttons
In this case SAndroidE works by
receiving notifications by vendorrsquos
APP
Some vendors provide data
only by accessing the
proprietary Cloud
Example Fitbit activity
trackers
SAndroidE retrieves such data
by means of API
SANDROIDE APP LEARN BY EXAMPLE
24Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVETo replace the text laquoHello Worldraquo with laquoHello World trimmer valueraquo
Furthermore we want to connect the button and the led not by wire but laquothrough the appraquo when the button is pressed the led must light on
QUI IMMAGINE BASETTA
SANDROIDE APP LEARN BY EXAMPLE
25Sistemi di Elettronica Digitale A Flammini
2) DOWNLOAD SANDROIDE amp PHONE CONFIGURATION
a) From httpsgithubcomSAndroidEOfficialframeworkreleases download the latest SAndroidE binary package sandroide-bin-v07zip
b) Decompress the zip file
c) Connect smartphone or tablet as mass storageUSB archive and copy within the smartphone internal memory these files
a) all xml files contained in the xml folder of the decompressed zip b) the file flasher-debugapk
d) Disconnect the smartphone and in laquoPhone Settings-gtSecurityraquo enable Installation from laquoUnknown Sourcesraquo
e) Using the laquoArchiveraquo or laquoFile Explorerraquo app of your Smartphone search and execute the file flasher-debugapk to to install it Accept all with OK
SANDROIDE APP LEARN BY EXAMPLE
26Sistemi di Elettronica Digitale A Flammini
3) FLASHING THE DEVICE WITH SANDROIDE COMPATIBILE FIRMWARE
The Nano device to interact with a SAndroidE APP must be provided with a firmware implementing the same communication protocol supported by SAndroidE on the smartphone side Letrsquos see how
a) Connect Nano to the PC using the appropriate USB programmer
b) From SandroidE wiki on Github httpsgithubcomSAndroidEOfficialframeworkwiki
access the Nano tutorial by clicking on laquoUsing RedBear Nano with SAndroidEraquo
c) In the section laquoFlashing the Nanoraquo click on laquoSandroide compatible Nano firmwareraquo and save the opened page (File-gtSave) in the MBEDDAPLINK drive
PLEASE PAY ATTENTION to save as hex and not as txt by selecting laquoAll filesraquo int the Saving form instead of laquoText fileraquo and by removing the suffix txt to the end of the file name
SANDROIDE APP LEARN BY EXAMPLE
27Sistemi di Elettronica Digitale A Flammini
4) ASSIGN AN ID TO THE NANO BY USING THE FLASHER APP
A) Launche the BleembeddedFlaser app installed at step 2 and click on laquoScan discoverable devicesraquo
B) Click on MyNano If there is more than one Nano verify characters on the label of the Nano and check that they match with the last 2 characters of the MAC address shown in the list
Warning Nano mujst be powered on Reset the power it still does not appear in the list
C) Wait until the device connects and disconnects A list of bluetooth services and characteristics provided is shown
D) Click on laquoStore attribute in description fileraquo and give it the name nanoesempio
REMEMBER THIS NAME
SANDROIDE APP LEARN BY EXAMPLE
28Sistemi di Elettronica Digitale A Flammini
5) CONNECT NANO AND COMPONENTS TO BREADBOARD AND POWER IT ON
PLEASE NOTE
- Led (only on Nano) can be connected withouth resistor because current is limited
- For SAndroidE the pin labels to consider are the blue ones if the label is P0_X then in SAndroidE you will have to use X
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SENSORS Gyroscope
9Sistemi di Elettronica Digitale A Flammini
Usages
Measurements of rate of rotation (angular speed)
3 values related to the axes
Pitch value (rotation around X axis)
Roll value (rotation around Y axis)
Yaw value (rotation around Z axis)
SENSOR FRAMEWORK
10Sistemi di Elettronica Digitale A Flammini
You can access these sensors and acquire raw sensor data by using the Android sensorframework The sensor framework is part of the androidhardware package and includesthe following classes and interfaces
bull SensorManager You can use this class to create an instance of the sensor service This class provides various methods for accessing and listing sensors registering and unregistering sensor event listeners This class also provides several sensor constantsthat are used to report sensor accuracy set data acquisition rates and calibrate sensors
bull Sensor You can use this class to create an instance of a specific sensor This classprovides various methods that let you determine a sensors capabilities
bull SensorEvent The system uses this class to create a sensor event object which providesinformation about a sensor event A sensor event object includes the followinginformation the raw sensor data the type of sensor that generated the event the accuracy of the data and the timestamp for the event
bull SensorEventListener You can use this interface to create two callback methods thatreceive notifications (sensor events) when sensor values change or when sensoraccuracy changes
httpsdeveloperandroidcomguidetopicssensorssensors_overviewhtml
SENSOR FRAMEWORK how to use
11Sistemi di Elettronica Digitale A Flammini
You can access sensor information by means of several methods
bull getResolution() sensor resolutionbull getMaximumRange() sensor maximum rangebull getPower() sensors power requirements requirementsbull getMinDelay() returns the minimum time interval (in microseconds) a sensor can use to
sense data Any sensor that returns a non-zero value for the getMinDelay() method is a streaming sensor otherwise it reports data only when there is a change in the parameters it is sensing
bull getVendor() sensor manufacturer
You can set sensor parameters by registerListener() methodbull SensorManagerSENSOR_DELAY_NORMAL sensor sampling interval of a streaming
sensor is set to NORMAL (default setting) resolution NORMAL = 200ms UI = 60ms GAME = 20ms FASTEST = 0ms asynchronous)
You can access sensor data and events by implementing a callbackbull onSensorChanged() a sensor reports a new value System provides you with a
SensorEvent object containing information (accuracy source timestamp value)
ANDROID APP SENSOR LEARN BY EXAMPLE
12Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVEModify the text laquoHello Worldraquo with laquoHello World RMS = sqrt( (x^2 + y^2 + z^2)3)raquo where RMS is the Root Mean Square
Objective is to insert some code within the MainActivityjava file which subscribes for accelerometer values and calculates and visualizes the Root Mean Square value to the user
ANDROID APP SENSOR LEARN BY EXAMPLE
13Sistemi di Elettronica Digitale A Flammini
2) SUBSCRIPTION TO SENSORMANAGER
To get values from accelerometer is is necessary to subscribe for receiving accelerometer sensor events from the SensorManager which is the service providing access to internal sensors in Android
A) Create variable which will contain the SensorManager object
B) The SensorManager service is assigned to the variable
C) A subscription for accelerometer sensor is requested with delay normal (200ms) and the current activity(this) will be the target responsible for handling sensor events containing acceleration values
A
BC Warning this will be
red coloured until next step
ANDROID APP SENSOR LEARN BY EXAMPLE
14Sistemi di Elettronica Digitale A Flammini
3) IMPLEMENT RECEIVING CALLBACKS
B) Fill the onSensorChanged with the code to change the text of the TextView Warning code seems complicated because to modify UI objects it is needed to call runOnUIThread method
The important part here is contained inside tvsetText() to calculate RMS value from acceleration values
A
B
A) Write laquoimplements SensorEventListenerraquo ALT+Enter laquoImplement methodsraquo OK Callbacks onSensorChanged e onAccuracyChanged will be automatically created
ANDROID APP SENSOR LEARN BY EXAMPLE
15Sistemi di Elettronica Digitale A Flammini
4) RUN
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
16Sistemi di Elettronica Digitale A Flammini
A solution from Google Android Things Sensors (Things) are equipped with Android
(eg Smartwatch) itrsquos easy to develop APPS
for Things management
Currently available only for few devices
Unsuitable for low-cost low-power things
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
17Sistemi di Elettronica Digitale A Flammini
SAndroidE rarr External sensors = Internal ones Oriented to interface things by Bluetooth Low-Energy (BLE)
Allows to build custom-logic APPs to interact with external things
Supports devices from multiple vendors
Free and Open Source
Examples of supported devices
Official website httpes3unibsitSAndroidE
Github httpsgithubcomSAndroidEOfficial
Arduino RaspBerry Redbear Nano Flic Beacons
Sensory
systemFitbit
SAndroidE
18Sistemi di Elettronica Digitale A Flammini
SAndroidE- does not require rooting- simply use Android BLE
stack
BLE = Bluetooth Low Energy
Internal
sensors
HW HW
Driver
Input subsystem Kernel
Libraries Sensor HAL
Application level
Applications
SAndroidE
BLE
External
sensors
Android Stack
Application
Framework
Activity Manager
SAndroidE components
19Sistemi di Elettronica Digitale A Flammini
bull SAndroidE library (Java)
Virtualizes remote devices
regardless of type and
operating mode
bull Device Descriptor Files (XMLJSON)
Represent a description
of the BT Services
provided by devices
bull API description files (XMLJSON)
Represent interface
to connect and retrieve
data from Cloud
bull SAndroidE configuration application
Used to detect nearby
BT devices and give
them a unique ID
All provided in the open-source code
SAndroidE supported devices
20Sistemi di Elettronica Digitale A Flammini
A User
programmable
devices
B Beacons and non-
programmable
devices
C Vendor Locked
device - data from
vendorrsquos APP
D Vendor Locked
device - data from
vendorrsquos Cloud
SAndroidE supported devices
21Sistemi di Elettronica Digitale A Flammini
A User programmable devices
B Beacons and
non-programmable devices
Devices like Arduino Raspberry Pi RedBear BLE Nano already
integrated
Typically require device flashing
With SAndroidE no need to write custom firmware for basic
operations
Flashing is not allowed but BT protocols are known
SAndroidE declares protocols in the Device Descriptor Files
In this way these devices can be easily virtualized
NOTE a BLE beacon does not need any coupling as it continuously broadcasts BLE messages to any device in range
BLE BEACONS
22Sistemi di Elettronica Digitale A Flammini
SAndroidE supported devices
23Sistemi di Elettronica Digitale A Flammini
C Vendor locked device ndash
data from vendorrsquosAPP
D Vendor locked device ndash
data from vendorrsquos
CLOUD
Some vendors apply an encrypted
BT protocol
Example Flic buttons
In this case SAndroidE works by
receiving notifications by vendorrsquos
APP
Some vendors provide data
only by accessing the
proprietary Cloud
Example Fitbit activity
trackers
SAndroidE retrieves such data
by means of API
SANDROIDE APP LEARN BY EXAMPLE
24Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVETo replace the text laquoHello Worldraquo with laquoHello World trimmer valueraquo
Furthermore we want to connect the button and the led not by wire but laquothrough the appraquo when the button is pressed the led must light on
QUI IMMAGINE BASETTA
SANDROIDE APP LEARN BY EXAMPLE
25Sistemi di Elettronica Digitale A Flammini
2) DOWNLOAD SANDROIDE amp PHONE CONFIGURATION
a) From httpsgithubcomSAndroidEOfficialframeworkreleases download the latest SAndroidE binary package sandroide-bin-v07zip
b) Decompress the zip file
c) Connect smartphone or tablet as mass storageUSB archive and copy within the smartphone internal memory these files
a) all xml files contained in the xml folder of the decompressed zip b) the file flasher-debugapk
d) Disconnect the smartphone and in laquoPhone Settings-gtSecurityraquo enable Installation from laquoUnknown Sourcesraquo
e) Using the laquoArchiveraquo or laquoFile Explorerraquo app of your Smartphone search and execute the file flasher-debugapk to to install it Accept all with OK
SANDROIDE APP LEARN BY EXAMPLE
26Sistemi di Elettronica Digitale A Flammini
3) FLASHING THE DEVICE WITH SANDROIDE COMPATIBILE FIRMWARE
The Nano device to interact with a SAndroidE APP must be provided with a firmware implementing the same communication protocol supported by SAndroidE on the smartphone side Letrsquos see how
a) Connect Nano to the PC using the appropriate USB programmer
b) From SandroidE wiki on Github httpsgithubcomSAndroidEOfficialframeworkwiki
access the Nano tutorial by clicking on laquoUsing RedBear Nano with SAndroidEraquo
c) In the section laquoFlashing the Nanoraquo click on laquoSandroide compatible Nano firmwareraquo and save the opened page (File-gtSave) in the MBEDDAPLINK drive
PLEASE PAY ATTENTION to save as hex and not as txt by selecting laquoAll filesraquo int the Saving form instead of laquoText fileraquo and by removing the suffix txt to the end of the file name
SANDROIDE APP LEARN BY EXAMPLE
27Sistemi di Elettronica Digitale A Flammini
4) ASSIGN AN ID TO THE NANO BY USING THE FLASHER APP
A) Launche the BleembeddedFlaser app installed at step 2 and click on laquoScan discoverable devicesraquo
B) Click on MyNano If there is more than one Nano verify characters on the label of the Nano and check that they match with the last 2 characters of the MAC address shown in the list
Warning Nano mujst be powered on Reset the power it still does not appear in the list
C) Wait until the device connects and disconnects A list of bluetooth services and characteristics provided is shown
D) Click on laquoStore attribute in description fileraquo and give it the name nanoesempio
REMEMBER THIS NAME
SANDROIDE APP LEARN BY EXAMPLE
28Sistemi di Elettronica Digitale A Flammini
5) CONNECT NANO AND COMPONENTS TO BREADBOARD AND POWER IT ON
PLEASE NOTE
- Led (only on Nano) can be connected withouth resistor because current is limited
- For SAndroidE the pin labels to consider are the blue ones if the label is P0_X then in SAndroidE you will have to use X
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SENSOR FRAMEWORK
10Sistemi di Elettronica Digitale A Flammini
You can access these sensors and acquire raw sensor data by using the Android sensorframework The sensor framework is part of the androidhardware package and includesthe following classes and interfaces
bull SensorManager You can use this class to create an instance of the sensor service This class provides various methods for accessing and listing sensors registering and unregistering sensor event listeners This class also provides several sensor constantsthat are used to report sensor accuracy set data acquisition rates and calibrate sensors
bull Sensor You can use this class to create an instance of a specific sensor This classprovides various methods that let you determine a sensors capabilities
bull SensorEvent The system uses this class to create a sensor event object which providesinformation about a sensor event A sensor event object includes the followinginformation the raw sensor data the type of sensor that generated the event the accuracy of the data and the timestamp for the event
bull SensorEventListener You can use this interface to create two callback methods thatreceive notifications (sensor events) when sensor values change or when sensoraccuracy changes
httpsdeveloperandroidcomguidetopicssensorssensors_overviewhtml
SENSOR FRAMEWORK how to use
11Sistemi di Elettronica Digitale A Flammini
You can access sensor information by means of several methods
bull getResolution() sensor resolutionbull getMaximumRange() sensor maximum rangebull getPower() sensors power requirements requirementsbull getMinDelay() returns the minimum time interval (in microseconds) a sensor can use to
sense data Any sensor that returns a non-zero value for the getMinDelay() method is a streaming sensor otherwise it reports data only when there is a change in the parameters it is sensing
bull getVendor() sensor manufacturer
You can set sensor parameters by registerListener() methodbull SensorManagerSENSOR_DELAY_NORMAL sensor sampling interval of a streaming
sensor is set to NORMAL (default setting) resolution NORMAL = 200ms UI = 60ms GAME = 20ms FASTEST = 0ms asynchronous)
You can access sensor data and events by implementing a callbackbull onSensorChanged() a sensor reports a new value System provides you with a
SensorEvent object containing information (accuracy source timestamp value)
ANDROID APP SENSOR LEARN BY EXAMPLE
12Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVEModify the text laquoHello Worldraquo with laquoHello World RMS = sqrt( (x^2 + y^2 + z^2)3)raquo where RMS is the Root Mean Square
Objective is to insert some code within the MainActivityjava file which subscribes for accelerometer values and calculates and visualizes the Root Mean Square value to the user
ANDROID APP SENSOR LEARN BY EXAMPLE
13Sistemi di Elettronica Digitale A Flammini
2) SUBSCRIPTION TO SENSORMANAGER
To get values from accelerometer is is necessary to subscribe for receiving accelerometer sensor events from the SensorManager which is the service providing access to internal sensors in Android
A) Create variable which will contain the SensorManager object
B) The SensorManager service is assigned to the variable
C) A subscription for accelerometer sensor is requested with delay normal (200ms) and the current activity(this) will be the target responsible for handling sensor events containing acceleration values
A
BC Warning this will be
red coloured until next step
ANDROID APP SENSOR LEARN BY EXAMPLE
14Sistemi di Elettronica Digitale A Flammini
3) IMPLEMENT RECEIVING CALLBACKS
B) Fill the onSensorChanged with the code to change the text of the TextView Warning code seems complicated because to modify UI objects it is needed to call runOnUIThread method
The important part here is contained inside tvsetText() to calculate RMS value from acceleration values
A
B
A) Write laquoimplements SensorEventListenerraquo ALT+Enter laquoImplement methodsraquo OK Callbacks onSensorChanged e onAccuracyChanged will be automatically created
ANDROID APP SENSOR LEARN BY EXAMPLE
15Sistemi di Elettronica Digitale A Flammini
4) RUN
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
16Sistemi di Elettronica Digitale A Flammini
A solution from Google Android Things Sensors (Things) are equipped with Android
(eg Smartwatch) itrsquos easy to develop APPS
for Things management
Currently available only for few devices
Unsuitable for low-cost low-power things
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
17Sistemi di Elettronica Digitale A Flammini
SAndroidE rarr External sensors = Internal ones Oriented to interface things by Bluetooth Low-Energy (BLE)
Allows to build custom-logic APPs to interact with external things
Supports devices from multiple vendors
Free and Open Source
Examples of supported devices
Official website httpes3unibsitSAndroidE
Github httpsgithubcomSAndroidEOfficial
Arduino RaspBerry Redbear Nano Flic Beacons
Sensory
systemFitbit
SAndroidE
18Sistemi di Elettronica Digitale A Flammini
SAndroidE- does not require rooting- simply use Android BLE
stack
BLE = Bluetooth Low Energy
Internal
sensors
HW HW
Driver
Input subsystem Kernel
Libraries Sensor HAL
Application level
Applications
SAndroidE
BLE
External
sensors
Android Stack
Application
Framework
Activity Manager
SAndroidE components
19Sistemi di Elettronica Digitale A Flammini
bull SAndroidE library (Java)
Virtualizes remote devices
regardless of type and
operating mode
bull Device Descriptor Files (XMLJSON)
Represent a description
of the BT Services
provided by devices
bull API description files (XMLJSON)
Represent interface
to connect and retrieve
data from Cloud
bull SAndroidE configuration application
Used to detect nearby
BT devices and give
them a unique ID
All provided in the open-source code
SAndroidE supported devices
20Sistemi di Elettronica Digitale A Flammini
A User
programmable
devices
B Beacons and non-
programmable
devices
C Vendor Locked
device - data from
vendorrsquos APP
D Vendor Locked
device - data from
vendorrsquos Cloud
SAndroidE supported devices
21Sistemi di Elettronica Digitale A Flammini
A User programmable devices
B Beacons and
non-programmable devices
Devices like Arduino Raspberry Pi RedBear BLE Nano already
integrated
Typically require device flashing
With SAndroidE no need to write custom firmware for basic
operations
Flashing is not allowed but BT protocols are known
SAndroidE declares protocols in the Device Descriptor Files
In this way these devices can be easily virtualized
NOTE a BLE beacon does not need any coupling as it continuously broadcasts BLE messages to any device in range
BLE BEACONS
22Sistemi di Elettronica Digitale A Flammini
SAndroidE supported devices
23Sistemi di Elettronica Digitale A Flammini
C Vendor locked device ndash
data from vendorrsquosAPP
D Vendor locked device ndash
data from vendorrsquos
CLOUD
Some vendors apply an encrypted
BT protocol
Example Flic buttons
In this case SAndroidE works by
receiving notifications by vendorrsquos
APP
Some vendors provide data
only by accessing the
proprietary Cloud
Example Fitbit activity
trackers
SAndroidE retrieves such data
by means of API
SANDROIDE APP LEARN BY EXAMPLE
24Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVETo replace the text laquoHello Worldraquo with laquoHello World trimmer valueraquo
Furthermore we want to connect the button and the led not by wire but laquothrough the appraquo when the button is pressed the led must light on
QUI IMMAGINE BASETTA
SANDROIDE APP LEARN BY EXAMPLE
25Sistemi di Elettronica Digitale A Flammini
2) DOWNLOAD SANDROIDE amp PHONE CONFIGURATION
a) From httpsgithubcomSAndroidEOfficialframeworkreleases download the latest SAndroidE binary package sandroide-bin-v07zip
b) Decompress the zip file
c) Connect smartphone or tablet as mass storageUSB archive and copy within the smartphone internal memory these files
a) all xml files contained in the xml folder of the decompressed zip b) the file flasher-debugapk
d) Disconnect the smartphone and in laquoPhone Settings-gtSecurityraquo enable Installation from laquoUnknown Sourcesraquo
e) Using the laquoArchiveraquo or laquoFile Explorerraquo app of your Smartphone search and execute the file flasher-debugapk to to install it Accept all with OK
SANDROIDE APP LEARN BY EXAMPLE
26Sistemi di Elettronica Digitale A Flammini
3) FLASHING THE DEVICE WITH SANDROIDE COMPATIBILE FIRMWARE
The Nano device to interact with a SAndroidE APP must be provided with a firmware implementing the same communication protocol supported by SAndroidE on the smartphone side Letrsquos see how
a) Connect Nano to the PC using the appropriate USB programmer
b) From SandroidE wiki on Github httpsgithubcomSAndroidEOfficialframeworkwiki
access the Nano tutorial by clicking on laquoUsing RedBear Nano with SAndroidEraquo
c) In the section laquoFlashing the Nanoraquo click on laquoSandroide compatible Nano firmwareraquo and save the opened page (File-gtSave) in the MBEDDAPLINK drive
PLEASE PAY ATTENTION to save as hex and not as txt by selecting laquoAll filesraquo int the Saving form instead of laquoText fileraquo and by removing the suffix txt to the end of the file name
SANDROIDE APP LEARN BY EXAMPLE
27Sistemi di Elettronica Digitale A Flammini
4) ASSIGN AN ID TO THE NANO BY USING THE FLASHER APP
A) Launche the BleembeddedFlaser app installed at step 2 and click on laquoScan discoverable devicesraquo
B) Click on MyNano If there is more than one Nano verify characters on the label of the Nano and check that they match with the last 2 characters of the MAC address shown in the list
Warning Nano mujst be powered on Reset the power it still does not appear in the list
C) Wait until the device connects and disconnects A list of bluetooth services and characteristics provided is shown
D) Click on laquoStore attribute in description fileraquo and give it the name nanoesempio
REMEMBER THIS NAME
SANDROIDE APP LEARN BY EXAMPLE
28Sistemi di Elettronica Digitale A Flammini
5) CONNECT NANO AND COMPONENTS TO BREADBOARD AND POWER IT ON
PLEASE NOTE
- Led (only on Nano) can be connected withouth resistor because current is limited
- For SAndroidE the pin labels to consider are the blue ones if the label is P0_X then in SAndroidE you will have to use X
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SENSOR FRAMEWORK how to use
11Sistemi di Elettronica Digitale A Flammini
You can access sensor information by means of several methods
bull getResolution() sensor resolutionbull getMaximumRange() sensor maximum rangebull getPower() sensors power requirements requirementsbull getMinDelay() returns the minimum time interval (in microseconds) a sensor can use to
sense data Any sensor that returns a non-zero value for the getMinDelay() method is a streaming sensor otherwise it reports data only when there is a change in the parameters it is sensing
bull getVendor() sensor manufacturer
You can set sensor parameters by registerListener() methodbull SensorManagerSENSOR_DELAY_NORMAL sensor sampling interval of a streaming
sensor is set to NORMAL (default setting) resolution NORMAL = 200ms UI = 60ms GAME = 20ms FASTEST = 0ms asynchronous)
You can access sensor data and events by implementing a callbackbull onSensorChanged() a sensor reports a new value System provides you with a
SensorEvent object containing information (accuracy source timestamp value)
ANDROID APP SENSOR LEARN BY EXAMPLE
12Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVEModify the text laquoHello Worldraquo with laquoHello World RMS = sqrt( (x^2 + y^2 + z^2)3)raquo where RMS is the Root Mean Square
Objective is to insert some code within the MainActivityjava file which subscribes for accelerometer values and calculates and visualizes the Root Mean Square value to the user
ANDROID APP SENSOR LEARN BY EXAMPLE
13Sistemi di Elettronica Digitale A Flammini
2) SUBSCRIPTION TO SENSORMANAGER
To get values from accelerometer is is necessary to subscribe for receiving accelerometer sensor events from the SensorManager which is the service providing access to internal sensors in Android
A) Create variable which will contain the SensorManager object
B) The SensorManager service is assigned to the variable
C) A subscription for accelerometer sensor is requested with delay normal (200ms) and the current activity(this) will be the target responsible for handling sensor events containing acceleration values
A
BC Warning this will be
red coloured until next step
ANDROID APP SENSOR LEARN BY EXAMPLE
14Sistemi di Elettronica Digitale A Flammini
3) IMPLEMENT RECEIVING CALLBACKS
B) Fill the onSensorChanged with the code to change the text of the TextView Warning code seems complicated because to modify UI objects it is needed to call runOnUIThread method
The important part here is contained inside tvsetText() to calculate RMS value from acceleration values
A
B
A) Write laquoimplements SensorEventListenerraquo ALT+Enter laquoImplement methodsraquo OK Callbacks onSensorChanged e onAccuracyChanged will be automatically created
ANDROID APP SENSOR LEARN BY EXAMPLE
15Sistemi di Elettronica Digitale A Flammini
4) RUN
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
16Sistemi di Elettronica Digitale A Flammini
A solution from Google Android Things Sensors (Things) are equipped with Android
(eg Smartwatch) itrsquos easy to develop APPS
for Things management
Currently available only for few devices
Unsuitable for low-cost low-power things
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
17Sistemi di Elettronica Digitale A Flammini
SAndroidE rarr External sensors = Internal ones Oriented to interface things by Bluetooth Low-Energy (BLE)
Allows to build custom-logic APPs to interact with external things
Supports devices from multiple vendors
Free and Open Source
Examples of supported devices
Official website httpes3unibsitSAndroidE
Github httpsgithubcomSAndroidEOfficial
Arduino RaspBerry Redbear Nano Flic Beacons
Sensory
systemFitbit
SAndroidE
18Sistemi di Elettronica Digitale A Flammini
SAndroidE- does not require rooting- simply use Android BLE
stack
BLE = Bluetooth Low Energy
Internal
sensors
HW HW
Driver
Input subsystem Kernel
Libraries Sensor HAL
Application level
Applications
SAndroidE
BLE
External
sensors
Android Stack
Application
Framework
Activity Manager
SAndroidE components
19Sistemi di Elettronica Digitale A Flammini
bull SAndroidE library (Java)
Virtualizes remote devices
regardless of type and
operating mode
bull Device Descriptor Files (XMLJSON)
Represent a description
of the BT Services
provided by devices
bull API description files (XMLJSON)
Represent interface
to connect and retrieve
data from Cloud
bull SAndroidE configuration application
Used to detect nearby
BT devices and give
them a unique ID
All provided in the open-source code
SAndroidE supported devices
20Sistemi di Elettronica Digitale A Flammini
A User
programmable
devices
B Beacons and non-
programmable
devices
C Vendor Locked
device - data from
vendorrsquos APP
D Vendor Locked
device - data from
vendorrsquos Cloud
SAndroidE supported devices
21Sistemi di Elettronica Digitale A Flammini
A User programmable devices
B Beacons and
non-programmable devices
Devices like Arduino Raspberry Pi RedBear BLE Nano already
integrated
Typically require device flashing
With SAndroidE no need to write custom firmware for basic
operations
Flashing is not allowed but BT protocols are known
SAndroidE declares protocols in the Device Descriptor Files
In this way these devices can be easily virtualized
NOTE a BLE beacon does not need any coupling as it continuously broadcasts BLE messages to any device in range
BLE BEACONS
22Sistemi di Elettronica Digitale A Flammini
SAndroidE supported devices
23Sistemi di Elettronica Digitale A Flammini
C Vendor locked device ndash
data from vendorrsquosAPP
D Vendor locked device ndash
data from vendorrsquos
CLOUD
Some vendors apply an encrypted
BT protocol
Example Flic buttons
In this case SAndroidE works by
receiving notifications by vendorrsquos
APP
Some vendors provide data
only by accessing the
proprietary Cloud
Example Fitbit activity
trackers
SAndroidE retrieves such data
by means of API
SANDROIDE APP LEARN BY EXAMPLE
24Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVETo replace the text laquoHello Worldraquo with laquoHello World trimmer valueraquo
Furthermore we want to connect the button and the led not by wire but laquothrough the appraquo when the button is pressed the led must light on
QUI IMMAGINE BASETTA
SANDROIDE APP LEARN BY EXAMPLE
25Sistemi di Elettronica Digitale A Flammini
2) DOWNLOAD SANDROIDE amp PHONE CONFIGURATION
a) From httpsgithubcomSAndroidEOfficialframeworkreleases download the latest SAndroidE binary package sandroide-bin-v07zip
b) Decompress the zip file
c) Connect smartphone or tablet as mass storageUSB archive and copy within the smartphone internal memory these files
a) all xml files contained in the xml folder of the decompressed zip b) the file flasher-debugapk
d) Disconnect the smartphone and in laquoPhone Settings-gtSecurityraquo enable Installation from laquoUnknown Sourcesraquo
e) Using the laquoArchiveraquo or laquoFile Explorerraquo app of your Smartphone search and execute the file flasher-debugapk to to install it Accept all with OK
SANDROIDE APP LEARN BY EXAMPLE
26Sistemi di Elettronica Digitale A Flammini
3) FLASHING THE DEVICE WITH SANDROIDE COMPATIBILE FIRMWARE
The Nano device to interact with a SAndroidE APP must be provided with a firmware implementing the same communication protocol supported by SAndroidE on the smartphone side Letrsquos see how
a) Connect Nano to the PC using the appropriate USB programmer
b) From SandroidE wiki on Github httpsgithubcomSAndroidEOfficialframeworkwiki
access the Nano tutorial by clicking on laquoUsing RedBear Nano with SAndroidEraquo
c) In the section laquoFlashing the Nanoraquo click on laquoSandroide compatible Nano firmwareraquo and save the opened page (File-gtSave) in the MBEDDAPLINK drive
PLEASE PAY ATTENTION to save as hex and not as txt by selecting laquoAll filesraquo int the Saving form instead of laquoText fileraquo and by removing the suffix txt to the end of the file name
SANDROIDE APP LEARN BY EXAMPLE
27Sistemi di Elettronica Digitale A Flammini
4) ASSIGN AN ID TO THE NANO BY USING THE FLASHER APP
A) Launche the BleembeddedFlaser app installed at step 2 and click on laquoScan discoverable devicesraquo
B) Click on MyNano If there is more than one Nano verify characters on the label of the Nano and check that they match with the last 2 characters of the MAC address shown in the list
Warning Nano mujst be powered on Reset the power it still does not appear in the list
C) Wait until the device connects and disconnects A list of bluetooth services and characteristics provided is shown
D) Click on laquoStore attribute in description fileraquo and give it the name nanoesempio
REMEMBER THIS NAME
SANDROIDE APP LEARN BY EXAMPLE
28Sistemi di Elettronica Digitale A Flammini
5) CONNECT NANO AND COMPONENTS TO BREADBOARD AND POWER IT ON
PLEASE NOTE
- Led (only on Nano) can be connected withouth resistor because current is limited
- For SAndroidE the pin labels to consider are the blue ones if the label is P0_X then in SAndroidE you will have to use X
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
ANDROID APP SENSOR LEARN BY EXAMPLE
12Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVEModify the text laquoHello Worldraquo with laquoHello World RMS = sqrt( (x^2 + y^2 + z^2)3)raquo where RMS is the Root Mean Square
Objective is to insert some code within the MainActivityjava file which subscribes for accelerometer values and calculates and visualizes the Root Mean Square value to the user
ANDROID APP SENSOR LEARN BY EXAMPLE
13Sistemi di Elettronica Digitale A Flammini
2) SUBSCRIPTION TO SENSORMANAGER
To get values from accelerometer is is necessary to subscribe for receiving accelerometer sensor events from the SensorManager which is the service providing access to internal sensors in Android
A) Create variable which will contain the SensorManager object
B) The SensorManager service is assigned to the variable
C) A subscription for accelerometer sensor is requested with delay normal (200ms) and the current activity(this) will be the target responsible for handling sensor events containing acceleration values
A
BC Warning this will be
red coloured until next step
ANDROID APP SENSOR LEARN BY EXAMPLE
14Sistemi di Elettronica Digitale A Flammini
3) IMPLEMENT RECEIVING CALLBACKS
B) Fill the onSensorChanged with the code to change the text of the TextView Warning code seems complicated because to modify UI objects it is needed to call runOnUIThread method
The important part here is contained inside tvsetText() to calculate RMS value from acceleration values
A
B
A) Write laquoimplements SensorEventListenerraquo ALT+Enter laquoImplement methodsraquo OK Callbacks onSensorChanged e onAccuracyChanged will be automatically created
ANDROID APP SENSOR LEARN BY EXAMPLE
15Sistemi di Elettronica Digitale A Flammini
4) RUN
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
16Sistemi di Elettronica Digitale A Flammini
A solution from Google Android Things Sensors (Things) are equipped with Android
(eg Smartwatch) itrsquos easy to develop APPS
for Things management
Currently available only for few devices
Unsuitable for low-cost low-power things
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
17Sistemi di Elettronica Digitale A Flammini
SAndroidE rarr External sensors = Internal ones Oriented to interface things by Bluetooth Low-Energy (BLE)
Allows to build custom-logic APPs to interact with external things
Supports devices from multiple vendors
Free and Open Source
Examples of supported devices
Official website httpes3unibsitSAndroidE
Github httpsgithubcomSAndroidEOfficial
Arduino RaspBerry Redbear Nano Flic Beacons
Sensory
systemFitbit
SAndroidE
18Sistemi di Elettronica Digitale A Flammini
SAndroidE- does not require rooting- simply use Android BLE
stack
BLE = Bluetooth Low Energy
Internal
sensors
HW HW
Driver
Input subsystem Kernel
Libraries Sensor HAL
Application level
Applications
SAndroidE
BLE
External
sensors
Android Stack
Application
Framework
Activity Manager
SAndroidE components
19Sistemi di Elettronica Digitale A Flammini
bull SAndroidE library (Java)
Virtualizes remote devices
regardless of type and
operating mode
bull Device Descriptor Files (XMLJSON)
Represent a description
of the BT Services
provided by devices
bull API description files (XMLJSON)
Represent interface
to connect and retrieve
data from Cloud
bull SAndroidE configuration application
Used to detect nearby
BT devices and give
them a unique ID
All provided in the open-source code
SAndroidE supported devices
20Sistemi di Elettronica Digitale A Flammini
A User
programmable
devices
B Beacons and non-
programmable
devices
C Vendor Locked
device - data from
vendorrsquos APP
D Vendor Locked
device - data from
vendorrsquos Cloud
SAndroidE supported devices
21Sistemi di Elettronica Digitale A Flammini
A User programmable devices
B Beacons and
non-programmable devices
Devices like Arduino Raspberry Pi RedBear BLE Nano already
integrated
Typically require device flashing
With SAndroidE no need to write custom firmware for basic
operations
Flashing is not allowed but BT protocols are known
SAndroidE declares protocols in the Device Descriptor Files
In this way these devices can be easily virtualized
NOTE a BLE beacon does not need any coupling as it continuously broadcasts BLE messages to any device in range
BLE BEACONS
22Sistemi di Elettronica Digitale A Flammini
SAndroidE supported devices
23Sistemi di Elettronica Digitale A Flammini
C Vendor locked device ndash
data from vendorrsquosAPP
D Vendor locked device ndash
data from vendorrsquos
CLOUD
Some vendors apply an encrypted
BT protocol
Example Flic buttons
In this case SAndroidE works by
receiving notifications by vendorrsquos
APP
Some vendors provide data
only by accessing the
proprietary Cloud
Example Fitbit activity
trackers
SAndroidE retrieves such data
by means of API
SANDROIDE APP LEARN BY EXAMPLE
24Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVETo replace the text laquoHello Worldraquo with laquoHello World trimmer valueraquo
Furthermore we want to connect the button and the led not by wire but laquothrough the appraquo when the button is pressed the led must light on
QUI IMMAGINE BASETTA
SANDROIDE APP LEARN BY EXAMPLE
25Sistemi di Elettronica Digitale A Flammini
2) DOWNLOAD SANDROIDE amp PHONE CONFIGURATION
a) From httpsgithubcomSAndroidEOfficialframeworkreleases download the latest SAndroidE binary package sandroide-bin-v07zip
b) Decompress the zip file
c) Connect smartphone or tablet as mass storageUSB archive and copy within the smartphone internal memory these files
a) all xml files contained in the xml folder of the decompressed zip b) the file flasher-debugapk
d) Disconnect the smartphone and in laquoPhone Settings-gtSecurityraquo enable Installation from laquoUnknown Sourcesraquo
e) Using the laquoArchiveraquo or laquoFile Explorerraquo app of your Smartphone search and execute the file flasher-debugapk to to install it Accept all with OK
SANDROIDE APP LEARN BY EXAMPLE
26Sistemi di Elettronica Digitale A Flammini
3) FLASHING THE DEVICE WITH SANDROIDE COMPATIBILE FIRMWARE
The Nano device to interact with a SAndroidE APP must be provided with a firmware implementing the same communication protocol supported by SAndroidE on the smartphone side Letrsquos see how
a) Connect Nano to the PC using the appropriate USB programmer
b) From SandroidE wiki on Github httpsgithubcomSAndroidEOfficialframeworkwiki
access the Nano tutorial by clicking on laquoUsing RedBear Nano with SAndroidEraquo
c) In the section laquoFlashing the Nanoraquo click on laquoSandroide compatible Nano firmwareraquo and save the opened page (File-gtSave) in the MBEDDAPLINK drive
PLEASE PAY ATTENTION to save as hex and not as txt by selecting laquoAll filesraquo int the Saving form instead of laquoText fileraquo and by removing the suffix txt to the end of the file name
SANDROIDE APP LEARN BY EXAMPLE
27Sistemi di Elettronica Digitale A Flammini
4) ASSIGN AN ID TO THE NANO BY USING THE FLASHER APP
A) Launche the BleembeddedFlaser app installed at step 2 and click on laquoScan discoverable devicesraquo
B) Click on MyNano If there is more than one Nano verify characters on the label of the Nano and check that they match with the last 2 characters of the MAC address shown in the list
Warning Nano mujst be powered on Reset the power it still does not appear in the list
C) Wait until the device connects and disconnects A list of bluetooth services and characteristics provided is shown
D) Click on laquoStore attribute in description fileraquo and give it the name nanoesempio
REMEMBER THIS NAME
SANDROIDE APP LEARN BY EXAMPLE
28Sistemi di Elettronica Digitale A Flammini
5) CONNECT NANO AND COMPONENTS TO BREADBOARD AND POWER IT ON
PLEASE NOTE
- Led (only on Nano) can be connected withouth resistor because current is limited
- For SAndroidE the pin labels to consider are the blue ones if the label is P0_X then in SAndroidE you will have to use X
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
ANDROID APP SENSOR LEARN BY EXAMPLE
13Sistemi di Elettronica Digitale A Flammini
2) SUBSCRIPTION TO SENSORMANAGER
To get values from accelerometer is is necessary to subscribe for receiving accelerometer sensor events from the SensorManager which is the service providing access to internal sensors in Android
A) Create variable which will contain the SensorManager object
B) The SensorManager service is assigned to the variable
C) A subscription for accelerometer sensor is requested with delay normal (200ms) and the current activity(this) will be the target responsible for handling sensor events containing acceleration values
A
BC Warning this will be
red coloured until next step
ANDROID APP SENSOR LEARN BY EXAMPLE
14Sistemi di Elettronica Digitale A Flammini
3) IMPLEMENT RECEIVING CALLBACKS
B) Fill the onSensorChanged with the code to change the text of the TextView Warning code seems complicated because to modify UI objects it is needed to call runOnUIThread method
The important part here is contained inside tvsetText() to calculate RMS value from acceleration values
A
B
A) Write laquoimplements SensorEventListenerraquo ALT+Enter laquoImplement methodsraquo OK Callbacks onSensorChanged e onAccuracyChanged will be automatically created
ANDROID APP SENSOR LEARN BY EXAMPLE
15Sistemi di Elettronica Digitale A Flammini
4) RUN
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
16Sistemi di Elettronica Digitale A Flammini
A solution from Google Android Things Sensors (Things) are equipped with Android
(eg Smartwatch) itrsquos easy to develop APPS
for Things management
Currently available only for few devices
Unsuitable for low-cost low-power things
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
17Sistemi di Elettronica Digitale A Flammini
SAndroidE rarr External sensors = Internal ones Oriented to interface things by Bluetooth Low-Energy (BLE)
Allows to build custom-logic APPs to interact with external things
Supports devices from multiple vendors
Free and Open Source
Examples of supported devices
Official website httpes3unibsitSAndroidE
Github httpsgithubcomSAndroidEOfficial
Arduino RaspBerry Redbear Nano Flic Beacons
Sensory
systemFitbit
SAndroidE
18Sistemi di Elettronica Digitale A Flammini
SAndroidE- does not require rooting- simply use Android BLE
stack
BLE = Bluetooth Low Energy
Internal
sensors
HW HW
Driver
Input subsystem Kernel
Libraries Sensor HAL
Application level
Applications
SAndroidE
BLE
External
sensors
Android Stack
Application
Framework
Activity Manager
SAndroidE components
19Sistemi di Elettronica Digitale A Flammini
bull SAndroidE library (Java)
Virtualizes remote devices
regardless of type and
operating mode
bull Device Descriptor Files (XMLJSON)
Represent a description
of the BT Services
provided by devices
bull API description files (XMLJSON)
Represent interface
to connect and retrieve
data from Cloud
bull SAndroidE configuration application
Used to detect nearby
BT devices and give
them a unique ID
All provided in the open-source code
SAndroidE supported devices
20Sistemi di Elettronica Digitale A Flammini
A User
programmable
devices
B Beacons and non-
programmable
devices
C Vendor Locked
device - data from
vendorrsquos APP
D Vendor Locked
device - data from
vendorrsquos Cloud
SAndroidE supported devices
21Sistemi di Elettronica Digitale A Flammini
A User programmable devices
B Beacons and
non-programmable devices
Devices like Arduino Raspberry Pi RedBear BLE Nano already
integrated
Typically require device flashing
With SAndroidE no need to write custom firmware for basic
operations
Flashing is not allowed but BT protocols are known
SAndroidE declares protocols in the Device Descriptor Files
In this way these devices can be easily virtualized
NOTE a BLE beacon does not need any coupling as it continuously broadcasts BLE messages to any device in range
BLE BEACONS
22Sistemi di Elettronica Digitale A Flammini
SAndroidE supported devices
23Sistemi di Elettronica Digitale A Flammini
C Vendor locked device ndash
data from vendorrsquosAPP
D Vendor locked device ndash
data from vendorrsquos
CLOUD
Some vendors apply an encrypted
BT protocol
Example Flic buttons
In this case SAndroidE works by
receiving notifications by vendorrsquos
APP
Some vendors provide data
only by accessing the
proprietary Cloud
Example Fitbit activity
trackers
SAndroidE retrieves such data
by means of API
SANDROIDE APP LEARN BY EXAMPLE
24Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVETo replace the text laquoHello Worldraquo with laquoHello World trimmer valueraquo
Furthermore we want to connect the button and the led not by wire but laquothrough the appraquo when the button is pressed the led must light on
QUI IMMAGINE BASETTA
SANDROIDE APP LEARN BY EXAMPLE
25Sistemi di Elettronica Digitale A Flammini
2) DOWNLOAD SANDROIDE amp PHONE CONFIGURATION
a) From httpsgithubcomSAndroidEOfficialframeworkreleases download the latest SAndroidE binary package sandroide-bin-v07zip
b) Decompress the zip file
c) Connect smartphone or tablet as mass storageUSB archive and copy within the smartphone internal memory these files
a) all xml files contained in the xml folder of the decompressed zip b) the file flasher-debugapk
d) Disconnect the smartphone and in laquoPhone Settings-gtSecurityraquo enable Installation from laquoUnknown Sourcesraquo
e) Using the laquoArchiveraquo or laquoFile Explorerraquo app of your Smartphone search and execute the file flasher-debugapk to to install it Accept all with OK
SANDROIDE APP LEARN BY EXAMPLE
26Sistemi di Elettronica Digitale A Flammini
3) FLASHING THE DEVICE WITH SANDROIDE COMPATIBILE FIRMWARE
The Nano device to interact with a SAndroidE APP must be provided with a firmware implementing the same communication protocol supported by SAndroidE on the smartphone side Letrsquos see how
a) Connect Nano to the PC using the appropriate USB programmer
b) From SandroidE wiki on Github httpsgithubcomSAndroidEOfficialframeworkwiki
access the Nano tutorial by clicking on laquoUsing RedBear Nano with SAndroidEraquo
c) In the section laquoFlashing the Nanoraquo click on laquoSandroide compatible Nano firmwareraquo and save the opened page (File-gtSave) in the MBEDDAPLINK drive
PLEASE PAY ATTENTION to save as hex and not as txt by selecting laquoAll filesraquo int the Saving form instead of laquoText fileraquo and by removing the suffix txt to the end of the file name
SANDROIDE APP LEARN BY EXAMPLE
27Sistemi di Elettronica Digitale A Flammini
4) ASSIGN AN ID TO THE NANO BY USING THE FLASHER APP
A) Launche the BleembeddedFlaser app installed at step 2 and click on laquoScan discoverable devicesraquo
B) Click on MyNano If there is more than one Nano verify characters on the label of the Nano and check that they match with the last 2 characters of the MAC address shown in the list
Warning Nano mujst be powered on Reset the power it still does not appear in the list
C) Wait until the device connects and disconnects A list of bluetooth services and characteristics provided is shown
D) Click on laquoStore attribute in description fileraquo and give it the name nanoesempio
REMEMBER THIS NAME
SANDROIDE APP LEARN BY EXAMPLE
28Sistemi di Elettronica Digitale A Flammini
5) CONNECT NANO AND COMPONENTS TO BREADBOARD AND POWER IT ON
PLEASE NOTE
- Led (only on Nano) can be connected withouth resistor because current is limited
- For SAndroidE the pin labels to consider are the blue ones if the label is P0_X then in SAndroidE you will have to use X
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
ANDROID APP SENSOR LEARN BY EXAMPLE
14Sistemi di Elettronica Digitale A Flammini
3) IMPLEMENT RECEIVING CALLBACKS
B) Fill the onSensorChanged with the code to change the text of the TextView Warning code seems complicated because to modify UI objects it is needed to call runOnUIThread method
The important part here is contained inside tvsetText() to calculate RMS value from acceleration values
A
B
A) Write laquoimplements SensorEventListenerraquo ALT+Enter laquoImplement methodsraquo OK Callbacks onSensorChanged e onAccuracyChanged will be automatically created
ANDROID APP SENSOR LEARN BY EXAMPLE
15Sistemi di Elettronica Digitale A Flammini
4) RUN
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
16Sistemi di Elettronica Digitale A Flammini
A solution from Google Android Things Sensors (Things) are equipped with Android
(eg Smartwatch) itrsquos easy to develop APPS
for Things management
Currently available only for few devices
Unsuitable for low-cost low-power things
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
17Sistemi di Elettronica Digitale A Flammini
SAndroidE rarr External sensors = Internal ones Oriented to interface things by Bluetooth Low-Energy (BLE)
Allows to build custom-logic APPs to interact with external things
Supports devices from multiple vendors
Free and Open Source
Examples of supported devices
Official website httpes3unibsitSAndroidE
Github httpsgithubcomSAndroidEOfficial
Arduino RaspBerry Redbear Nano Flic Beacons
Sensory
systemFitbit
SAndroidE
18Sistemi di Elettronica Digitale A Flammini
SAndroidE- does not require rooting- simply use Android BLE
stack
BLE = Bluetooth Low Energy
Internal
sensors
HW HW
Driver
Input subsystem Kernel
Libraries Sensor HAL
Application level
Applications
SAndroidE
BLE
External
sensors
Android Stack
Application
Framework
Activity Manager
SAndroidE components
19Sistemi di Elettronica Digitale A Flammini
bull SAndroidE library (Java)
Virtualizes remote devices
regardless of type and
operating mode
bull Device Descriptor Files (XMLJSON)
Represent a description
of the BT Services
provided by devices
bull API description files (XMLJSON)
Represent interface
to connect and retrieve
data from Cloud
bull SAndroidE configuration application
Used to detect nearby
BT devices and give
them a unique ID
All provided in the open-source code
SAndroidE supported devices
20Sistemi di Elettronica Digitale A Flammini
A User
programmable
devices
B Beacons and non-
programmable
devices
C Vendor Locked
device - data from
vendorrsquos APP
D Vendor Locked
device - data from
vendorrsquos Cloud
SAndroidE supported devices
21Sistemi di Elettronica Digitale A Flammini
A User programmable devices
B Beacons and
non-programmable devices
Devices like Arduino Raspberry Pi RedBear BLE Nano already
integrated
Typically require device flashing
With SAndroidE no need to write custom firmware for basic
operations
Flashing is not allowed but BT protocols are known
SAndroidE declares protocols in the Device Descriptor Files
In this way these devices can be easily virtualized
NOTE a BLE beacon does not need any coupling as it continuously broadcasts BLE messages to any device in range
BLE BEACONS
22Sistemi di Elettronica Digitale A Flammini
SAndroidE supported devices
23Sistemi di Elettronica Digitale A Flammini
C Vendor locked device ndash
data from vendorrsquosAPP
D Vendor locked device ndash
data from vendorrsquos
CLOUD
Some vendors apply an encrypted
BT protocol
Example Flic buttons
In this case SAndroidE works by
receiving notifications by vendorrsquos
APP
Some vendors provide data
only by accessing the
proprietary Cloud
Example Fitbit activity
trackers
SAndroidE retrieves such data
by means of API
SANDROIDE APP LEARN BY EXAMPLE
24Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVETo replace the text laquoHello Worldraquo with laquoHello World trimmer valueraquo
Furthermore we want to connect the button and the led not by wire but laquothrough the appraquo when the button is pressed the led must light on
QUI IMMAGINE BASETTA
SANDROIDE APP LEARN BY EXAMPLE
25Sistemi di Elettronica Digitale A Flammini
2) DOWNLOAD SANDROIDE amp PHONE CONFIGURATION
a) From httpsgithubcomSAndroidEOfficialframeworkreleases download the latest SAndroidE binary package sandroide-bin-v07zip
b) Decompress the zip file
c) Connect smartphone or tablet as mass storageUSB archive and copy within the smartphone internal memory these files
a) all xml files contained in the xml folder of the decompressed zip b) the file flasher-debugapk
d) Disconnect the smartphone and in laquoPhone Settings-gtSecurityraquo enable Installation from laquoUnknown Sourcesraquo
e) Using the laquoArchiveraquo or laquoFile Explorerraquo app of your Smartphone search and execute the file flasher-debugapk to to install it Accept all with OK
SANDROIDE APP LEARN BY EXAMPLE
26Sistemi di Elettronica Digitale A Flammini
3) FLASHING THE DEVICE WITH SANDROIDE COMPATIBILE FIRMWARE
The Nano device to interact with a SAndroidE APP must be provided with a firmware implementing the same communication protocol supported by SAndroidE on the smartphone side Letrsquos see how
a) Connect Nano to the PC using the appropriate USB programmer
b) From SandroidE wiki on Github httpsgithubcomSAndroidEOfficialframeworkwiki
access the Nano tutorial by clicking on laquoUsing RedBear Nano with SAndroidEraquo
c) In the section laquoFlashing the Nanoraquo click on laquoSandroide compatible Nano firmwareraquo and save the opened page (File-gtSave) in the MBEDDAPLINK drive
PLEASE PAY ATTENTION to save as hex and not as txt by selecting laquoAll filesraquo int the Saving form instead of laquoText fileraquo and by removing the suffix txt to the end of the file name
SANDROIDE APP LEARN BY EXAMPLE
27Sistemi di Elettronica Digitale A Flammini
4) ASSIGN AN ID TO THE NANO BY USING THE FLASHER APP
A) Launche the BleembeddedFlaser app installed at step 2 and click on laquoScan discoverable devicesraquo
B) Click on MyNano If there is more than one Nano verify characters on the label of the Nano and check that they match with the last 2 characters of the MAC address shown in the list
Warning Nano mujst be powered on Reset the power it still does not appear in the list
C) Wait until the device connects and disconnects A list of bluetooth services and characteristics provided is shown
D) Click on laquoStore attribute in description fileraquo and give it the name nanoesempio
REMEMBER THIS NAME
SANDROIDE APP LEARN BY EXAMPLE
28Sistemi di Elettronica Digitale A Flammini
5) CONNECT NANO AND COMPONENTS TO BREADBOARD AND POWER IT ON
PLEASE NOTE
- Led (only on Nano) can be connected withouth resistor because current is limited
- For SAndroidE the pin labels to consider are the blue ones if the label is P0_X then in SAndroidE you will have to use X
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
ANDROID APP SENSOR LEARN BY EXAMPLE
15Sistemi di Elettronica Digitale A Flammini
4) RUN
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
16Sistemi di Elettronica Digitale A Flammini
A solution from Google Android Things Sensors (Things) are equipped with Android
(eg Smartwatch) itrsquos easy to develop APPS
for Things management
Currently available only for few devices
Unsuitable for low-cost low-power things
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
17Sistemi di Elettronica Digitale A Flammini
SAndroidE rarr External sensors = Internal ones Oriented to interface things by Bluetooth Low-Energy (BLE)
Allows to build custom-logic APPs to interact with external things
Supports devices from multiple vendors
Free and Open Source
Examples of supported devices
Official website httpes3unibsitSAndroidE
Github httpsgithubcomSAndroidEOfficial
Arduino RaspBerry Redbear Nano Flic Beacons
Sensory
systemFitbit
SAndroidE
18Sistemi di Elettronica Digitale A Flammini
SAndroidE- does not require rooting- simply use Android BLE
stack
BLE = Bluetooth Low Energy
Internal
sensors
HW HW
Driver
Input subsystem Kernel
Libraries Sensor HAL
Application level
Applications
SAndroidE
BLE
External
sensors
Android Stack
Application
Framework
Activity Manager
SAndroidE components
19Sistemi di Elettronica Digitale A Flammini
bull SAndroidE library (Java)
Virtualizes remote devices
regardless of type and
operating mode
bull Device Descriptor Files (XMLJSON)
Represent a description
of the BT Services
provided by devices
bull API description files (XMLJSON)
Represent interface
to connect and retrieve
data from Cloud
bull SAndroidE configuration application
Used to detect nearby
BT devices and give
them a unique ID
All provided in the open-source code
SAndroidE supported devices
20Sistemi di Elettronica Digitale A Flammini
A User
programmable
devices
B Beacons and non-
programmable
devices
C Vendor Locked
device - data from
vendorrsquos APP
D Vendor Locked
device - data from
vendorrsquos Cloud
SAndroidE supported devices
21Sistemi di Elettronica Digitale A Flammini
A User programmable devices
B Beacons and
non-programmable devices
Devices like Arduino Raspberry Pi RedBear BLE Nano already
integrated
Typically require device flashing
With SAndroidE no need to write custom firmware for basic
operations
Flashing is not allowed but BT protocols are known
SAndroidE declares protocols in the Device Descriptor Files
In this way these devices can be easily virtualized
NOTE a BLE beacon does not need any coupling as it continuously broadcasts BLE messages to any device in range
BLE BEACONS
22Sistemi di Elettronica Digitale A Flammini
SAndroidE supported devices
23Sistemi di Elettronica Digitale A Flammini
C Vendor locked device ndash
data from vendorrsquosAPP
D Vendor locked device ndash
data from vendorrsquos
CLOUD
Some vendors apply an encrypted
BT protocol
Example Flic buttons
In this case SAndroidE works by
receiving notifications by vendorrsquos
APP
Some vendors provide data
only by accessing the
proprietary Cloud
Example Fitbit activity
trackers
SAndroidE retrieves such data
by means of API
SANDROIDE APP LEARN BY EXAMPLE
24Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVETo replace the text laquoHello Worldraquo with laquoHello World trimmer valueraquo
Furthermore we want to connect the button and the led not by wire but laquothrough the appraquo when the button is pressed the led must light on
QUI IMMAGINE BASETTA
SANDROIDE APP LEARN BY EXAMPLE
25Sistemi di Elettronica Digitale A Flammini
2) DOWNLOAD SANDROIDE amp PHONE CONFIGURATION
a) From httpsgithubcomSAndroidEOfficialframeworkreleases download the latest SAndroidE binary package sandroide-bin-v07zip
b) Decompress the zip file
c) Connect smartphone or tablet as mass storageUSB archive and copy within the smartphone internal memory these files
a) all xml files contained in the xml folder of the decompressed zip b) the file flasher-debugapk
d) Disconnect the smartphone and in laquoPhone Settings-gtSecurityraquo enable Installation from laquoUnknown Sourcesraquo
e) Using the laquoArchiveraquo or laquoFile Explorerraquo app of your Smartphone search and execute the file flasher-debugapk to to install it Accept all with OK
SANDROIDE APP LEARN BY EXAMPLE
26Sistemi di Elettronica Digitale A Flammini
3) FLASHING THE DEVICE WITH SANDROIDE COMPATIBILE FIRMWARE
The Nano device to interact with a SAndroidE APP must be provided with a firmware implementing the same communication protocol supported by SAndroidE on the smartphone side Letrsquos see how
a) Connect Nano to the PC using the appropriate USB programmer
b) From SandroidE wiki on Github httpsgithubcomSAndroidEOfficialframeworkwiki
access the Nano tutorial by clicking on laquoUsing RedBear Nano with SAndroidEraquo
c) In the section laquoFlashing the Nanoraquo click on laquoSandroide compatible Nano firmwareraquo and save the opened page (File-gtSave) in the MBEDDAPLINK drive
PLEASE PAY ATTENTION to save as hex and not as txt by selecting laquoAll filesraquo int the Saving form instead of laquoText fileraquo and by removing the suffix txt to the end of the file name
SANDROIDE APP LEARN BY EXAMPLE
27Sistemi di Elettronica Digitale A Flammini
4) ASSIGN AN ID TO THE NANO BY USING THE FLASHER APP
A) Launche the BleembeddedFlaser app installed at step 2 and click on laquoScan discoverable devicesraquo
B) Click on MyNano If there is more than one Nano verify characters on the label of the Nano and check that they match with the last 2 characters of the MAC address shown in the list
Warning Nano mujst be powered on Reset the power it still does not appear in the list
C) Wait until the device connects and disconnects A list of bluetooth services and characteristics provided is shown
D) Click on laquoStore attribute in description fileraquo and give it the name nanoesempio
REMEMBER THIS NAME
SANDROIDE APP LEARN BY EXAMPLE
28Sistemi di Elettronica Digitale A Flammini
5) CONNECT NANO AND COMPONENTS TO BREADBOARD AND POWER IT ON
PLEASE NOTE
- Led (only on Nano) can be connected withouth resistor because current is limited
- For SAndroidE the pin labels to consider are the blue ones if the label is P0_X then in SAndroidE you will have to use X
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
16Sistemi di Elettronica Digitale A Flammini
A solution from Google Android Things Sensors (Things) are equipped with Android
(eg Smartwatch) itrsquos easy to develop APPS
for Things management
Currently available only for few devices
Unsuitable for low-cost low-power things
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
17Sistemi di Elettronica Digitale A Flammini
SAndroidE rarr External sensors = Internal ones Oriented to interface things by Bluetooth Low-Energy (BLE)
Allows to build custom-logic APPs to interact with external things
Supports devices from multiple vendors
Free and Open Source
Examples of supported devices
Official website httpes3unibsitSAndroidE
Github httpsgithubcomSAndroidEOfficial
Arduino RaspBerry Redbear Nano Flic Beacons
Sensory
systemFitbit
SAndroidE
18Sistemi di Elettronica Digitale A Flammini
SAndroidE- does not require rooting- simply use Android BLE
stack
BLE = Bluetooth Low Energy
Internal
sensors
HW HW
Driver
Input subsystem Kernel
Libraries Sensor HAL
Application level
Applications
SAndroidE
BLE
External
sensors
Android Stack
Application
Framework
Activity Manager
SAndroidE components
19Sistemi di Elettronica Digitale A Flammini
bull SAndroidE library (Java)
Virtualizes remote devices
regardless of type and
operating mode
bull Device Descriptor Files (XMLJSON)
Represent a description
of the BT Services
provided by devices
bull API description files (XMLJSON)
Represent interface
to connect and retrieve
data from Cloud
bull SAndroidE configuration application
Used to detect nearby
BT devices and give
them a unique ID
All provided in the open-source code
SAndroidE supported devices
20Sistemi di Elettronica Digitale A Flammini
A User
programmable
devices
B Beacons and non-
programmable
devices
C Vendor Locked
device - data from
vendorrsquos APP
D Vendor Locked
device - data from
vendorrsquos Cloud
SAndroidE supported devices
21Sistemi di Elettronica Digitale A Flammini
A User programmable devices
B Beacons and
non-programmable devices
Devices like Arduino Raspberry Pi RedBear BLE Nano already
integrated
Typically require device flashing
With SAndroidE no need to write custom firmware for basic
operations
Flashing is not allowed but BT protocols are known
SAndroidE declares protocols in the Device Descriptor Files
In this way these devices can be easily virtualized
NOTE a BLE beacon does not need any coupling as it continuously broadcasts BLE messages to any device in range
BLE BEACONS
22Sistemi di Elettronica Digitale A Flammini
SAndroidE supported devices
23Sistemi di Elettronica Digitale A Flammini
C Vendor locked device ndash
data from vendorrsquosAPP
D Vendor locked device ndash
data from vendorrsquos
CLOUD
Some vendors apply an encrypted
BT protocol
Example Flic buttons
In this case SAndroidE works by
receiving notifications by vendorrsquos
APP
Some vendors provide data
only by accessing the
proprietary Cloud
Example Fitbit activity
trackers
SAndroidE retrieves such data
by means of API
SANDROIDE APP LEARN BY EXAMPLE
24Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVETo replace the text laquoHello Worldraquo with laquoHello World trimmer valueraquo
Furthermore we want to connect the button and the led not by wire but laquothrough the appraquo when the button is pressed the led must light on
QUI IMMAGINE BASETTA
SANDROIDE APP LEARN BY EXAMPLE
25Sistemi di Elettronica Digitale A Flammini
2) DOWNLOAD SANDROIDE amp PHONE CONFIGURATION
a) From httpsgithubcomSAndroidEOfficialframeworkreleases download the latest SAndroidE binary package sandroide-bin-v07zip
b) Decompress the zip file
c) Connect smartphone or tablet as mass storageUSB archive and copy within the smartphone internal memory these files
a) all xml files contained in the xml folder of the decompressed zip b) the file flasher-debugapk
d) Disconnect the smartphone and in laquoPhone Settings-gtSecurityraquo enable Installation from laquoUnknown Sourcesraquo
e) Using the laquoArchiveraquo or laquoFile Explorerraquo app of your Smartphone search and execute the file flasher-debugapk to to install it Accept all with OK
SANDROIDE APP LEARN BY EXAMPLE
26Sistemi di Elettronica Digitale A Flammini
3) FLASHING THE DEVICE WITH SANDROIDE COMPATIBILE FIRMWARE
The Nano device to interact with a SAndroidE APP must be provided with a firmware implementing the same communication protocol supported by SAndroidE on the smartphone side Letrsquos see how
a) Connect Nano to the PC using the appropriate USB programmer
b) From SandroidE wiki on Github httpsgithubcomSAndroidEOfficialframeworkwiki
access the Nano tutorial by clicking on laquoUsing RedBear Nano with SAndroidEraquo
c) In the section laquoFlashing the Nanoraquo click on laquoSandroide compatible Nano firmwareraquo and save the opened page (File-gtSave) in the MBEDDAPLINK drive
PLEASE PAY ATTENTION to save as hex and not as txt by selecting laquoAll filesraquo int the Saving form instead of laquoText fileraquo and by removing the suffix txt to the end of the file name
SANDROIDE APP LEARN BY EXAMPLE
27Sistemi di Elettronica Digitale A Flammini
4) ASSIGN AN ID TO THE NANO BY USING THE FLASHER APP
A) Launche the BleembeddedFlaser app installed at step 2 and click on laquoScan discoverable devicesraquo
B) Click on MyNano If there is more than one Nano verify characters on the label of the Nano and check that they match with the last 2 characters of the MAC address shown in the list
Warning Nano mujst be powered on Reset the power it still does not appear in the list
C) Wait until the device connects and disconnects A list of bluetooth services and characteristics provided is shown
D) Click on laquoStore attribute in description fileraquo and give it the name nanoesempio
REMEMBER THIS NAME
SANDROIDE APP LEARN BY EXAMPLE
28Sistemi di Elettronica Digitale A Flammini
5) CONNECT NANO AND COMPONENTS TO BREADBOARD AND POWER IT ON
PLEASE NOTE
- Led (only on Nano) can be connected withouth resistor because current is limited
- For SAndroidE the pin labels to consider are the blue ones if the label is P0_X then in SAndroidE you will have to use X
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
INTERNAL SENSORS IS EASY EXTERNAL SENSORS
17Sistemi di Elettronica Digitale A Flammini
SAndroidE rarr External sensors = Internal ones Oriented to interface things by Bluetooth Low-Energy (BLE)
Allows to build custom-logic APPs to interact with external things
Supports devices from multiple vendors
Free and Open Source
Examples of supported devices
Official website httpes3unibsitSAndroidE
Github httpsgithubcomSAndroidEOfficial
Arduino RaspBerry Redbear Nano Flic Beacons
Sensory
systemFitbit
SAndroidE
18Sistemi di Elettronica Digitale A Flammini
SAndroidE- does not require rooting- simply use Android BLE
stack
BLE = Bluetooth Low Energy
Internal
sensors
HW HW
Driver
Input subsystem Kernel
Libraries Sensor HAL
Application level
Applications
SAndroidE
BLE
External
sensors
Android Stack
Application
Framework
Activity Manager
SAndroidE components
19Sistemi di Elettronica Digitale A Flammini
bull SAndroidE library (Java)
Virtualizes remote devices
regardless of type and
operating mode
bull Device Descriptor Files (XMLJSON)
Represent a description
of the BT Services
provided by devices
bull API description files (XMLJSON)
Represent interface
to connect and retrieve
data from Cloud
bull SAndroidE configuration application
Used to detect nearby
BT devices and give
them a unique ID
All provided in the open-source code
SAndroidE supported devices
20Sistemi di Elettronica Digitale A Flammini
A User
programmable
devices
B Beacons and non-
programmable
devices
C Vendor Locked
device - data from
vendorrsquos APP
D Vendor Locked
device - data from
vendorrsquos Cloud
SAndroidE supported devices
21Sistemi di Elettronica Digitale A Flammini
A User programmable devices
B Beacons and
non-programmable devices
Devices like Arduino Raspberry Pi RedBear BLE Nano already
integrated
Typically require device flashing
With SAndroidE no need to write custom firmware for basic
operations
Flashing is not allowed but BT protocols are known
SAndroidE declares protocols in the Device Descriptor Files
In this way these devices can be easily virtualized
NOTE a BLE beacon does not need any coupling as it continuously broadcasts BLE messages to any device in range
BLE BEACONS
22Sistemi di Elettronica Digitale A Flammini
SAndroidE supported devices
23Sistemi di Elettronica Digitale A Flammini
C Vendor locked device ndash
data from vendorrsquosAPP
D Vendor locked device ndash
data from vendorrsquos
CLOUD
Some vendors apply an encrypted
BT protocol
Example Flic buttons
In this case SAndroidE works by
receiving notifications by vendorrsquos
APP
Some vendors provide data
only by accessing the
proprietary Cloud
Example Fitbit activity
trackers
SAndroidE retrieves such data
by means of API
SANDROIDE APP LEARN BY EXAMPLE
24Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVETo replace the text laquoHello Worldraquo with laquoHello World trimmer valueraquo
Furthermore we want to connect the button and the led not by wire but laquothrough the appraquo when the button is pressed the led must light on
QUI IMMAGINE BASETTA
SANDROIDE APP LEARN BY EXAMPLE
25Sistemi di Elettronica Digitale A Flammini
2) DOWNLOAD SANDROIDE amp PHONE CONFIGURATION
a) From httpsgithubcomSAndroidEOfficialframeworkreleases download the latest SAndroidE binary package sandroide-bin-v07zip
b) Decompress the zip file
c) Connect smartphone or tablet as mass storageUSB archive and copy within the smartphone internal memory these files
a) all xml files contained in the xml folder of the decompressed zip b) the file flasher-debugapk
d) Disconnect the smartphone and in laquoPhone Settings-gtSecurityraquo enable Installation from laquoUnknown Sourcesraquo
e) Using the laquoArchiveraquo or laquoFile Explorerraquo app of your Smartphone search and execute the file flasher-debugapk to to install it Accept all with OK
SANDROIDE APP LEARN BY EXAMPLE
26Sistemi di Elettronica Digitale A Flammini
3) FLASHING THE DEVICE WITH SANDROIDE COMPATIBILE FIRMWARE
The Nano device to interact with a SAndroidE APP must be provided with a firmware implementing the same communication protocol supported by SAndroidE on the smartphone side Letrsquos see how
a) Connect Nano to the PC using the appropriate USB programmer
b) From SandroidE wiki on Github httpsgithubcomSAndroidEOfficialframeworkwiki
access the Nano tutorial by clicking on laquoUsing RedBear Nano with SAndroidEraquo
c) In the section laquoFlashing the Nanoraquo click on laquoSandroide compatible Nano firmwareraquo and save the opened page (File-gtSave) in the MBEDDAPLINK drive
PLEASE PAY ATTENTION to save as hex and not as txt by selecting laquoAll filesraquo int the Saving form instead of laquoText fileraquo and by removing the suffix txt to the end of the file name
SANDROIDE APP LEARN BY EXAMPLE
27Sistemi di Elettronica Digitale A Flammini
4) ASSIGN AN ID TO THE NANO BY USING THE FLASHER APP
A) Launche the BleembeddedFlaser app installed at step 2 and click on laquoScan discoverable devicesraquo
B) Click on MyNano If there is more than one Nano verify characters on the label of the Nano and check that they match with the last 2 characters of the MAC address shown in the list
Warning Nano mujst be powered on Reset the power it still does not appear in the list
C) Wait until the device connects and disconnects A list of bluetooth services and characteristics provided is shown
D) Click on laquoStore attribute in description fileraquo and give it the name nanoesempio
REMEMBER THIS NAME
SANDROIDE APP LEARN BY EXAMPLE
28Sistemi di Elettronica Digitale A Flammini
5) CONNECT NANO AND COMPONENTS TO BREADBOARD AND POWER IT ON
PLEASE NOTE
- Led (only on Nano) can be connected withouth resistor because current is limited
- For SAndroidE the pin labels to consider are the blue ones if the label is P0_X then in SAndroidE you will have to use X
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SAndroidE
18Sistemi di Elettronica Digitale A Flammini
SAndroidE- does not require rooting- simply use Android BLE
stack
BLE = Bluetooth Low Energy
Internal
sensors
HW HW
Driver
Input subsystem Kernel
Libraries Sensor HAL
Application level
Applications
SAndroidE
BLE
External
sensors
Android Stack
Application
Framework
Activity Manager
SAndroidE components
19Sistemi di Elettronica Digitale A Flammini
bull SAndroidE library (Java)
Virtualizes remote devices
regardless of type and
operating mode
bull Device Descriptor Files (XMLJSON)
Represent a description
of the BT Services
provided by devices
bull API description files (XMLJSON)
Represent interface
to connect and retrieve
data from Cloud
bull SAndroidE configuration application
Used to detect nearby
BT devices and give
them a unique ID
All provided in the open-source code
SAndroidE supported devices
20Sistemi di Elettronica Digitale A Flammini
A User
programmable
devices
B Beacons and non-
programmable
devices
C Vendor Locked
device - data from
vendorrsquos APP
D Vendor Locked
device - data from
vendorrsquos Cloud
SAndroidE supported devices
21Sistemi di Elettronica Digitale A Flammini
A User programmable devices
B Beacons and
non-programmable devices
Devices like Arduino Raspberry Pi RedBear BLE Nano already
integrated
Typically require device flashing
With SAndroidE no need to write custom firmware for basic
operations
Flashing is not allowed but BT protocols are known
SAndroidE declares protocols in the Device Descriptor Files
In this way these devices can be easily virtualized
NOTE a BLE beacon does not need any coupling as it continuously broadcasts BLE messages to any device in range
BLE BEACONS
22Sistemi di Elettronica Digitale A Flammini
SAndroidE supported devices
23Sistemi di Elettronica Digitale A Flammini
C Vendor locked device ndash
data from vendorrsquosAPP
D Vendor locked device ndash
data from vendorrsquos
CLOUD
Some vendors apply an encrypted
BT protocol
Example Flic buttons
In this case SAndroidE works by
receiving notifications by vendorrsquos
APP
Some vendors provide data
only by accessing the
proprietary Cloud
Example Fitbit activity
trackers
SAndroidE retrieves such data
by means of API
SANDROIDE APP LEARN BY EXAMPLE
24Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVETo replace the text laquoHello Worldraquo with laquoHello World trimmer valueraquo
Furthermore we want to connect the button and the led not by wire but laquothrough the appraquo when the button is pressed the led must light on
QUI IMMAGINE BASETTA
SANDROIDE APP LEARN BY EXAMPLE
25Sistemi di Elettronica Digitale A Flammini
2) DOWNLOAD SANDROIDE amp PHONE CONFIGURATION
a) From httpsgithubcomSAndroidEOfficialframeworkreleases download the latest SAndroidE binary package sandroide-bin-v07zip
b) Decompress the zip file
c) Connect smartphone or tablet as mass storageUSB archive and copy within the smartphone internal memory these files
a) all xml files contained in the xml folder of the decompressed zip b) the file flasher-debugapk
d) Disconnect the smartphone and in laquoPhone Settings-gtSecurityraquo enable Installation from laquoUnknown Sourcesraquo
e) Using the laquoArchiveraquo or laquoFile Explorerraquo app of your Smartphone search and execute the file flasher-debugapk to to install it Accept all with OK
SANDROIDE APP LEARN BY EXAMPLE
26Sistemi di Elettronica Digitale A Flammini
3) FLASHING THE DEVICE WITH SANDROIDE COMPATIBILE FIRMWARE
The Nano device to interact with a SAndroidE APP must be provided with a firmware implementing the same communication protocol supported by SAndroidE on the smartphone side Letrsquos see how
a) Connect Nano to the PC using the appropriate USB programmer
b) From SandroidE wiki on Github httpsgithubcomSAndroidEOfficialframeworkwiki
access the Nano tutorial by clicking on laquoUsing RedBear Nano with SAndroidEraquo
c) In the section laquoFlashing the Nanoraquo click on laquoSandroide compatible Nano firmwareraquo and save the opened page (File-gtSave) in the MBEDDAPLINK drive
PLEASE PAY ATTENTION to save as hex and not as txt by selecting laquoAll filesraquo int the Saving form instead of laquoText fileraquo and by removing the suffix txt to the end of the file name
SANDROIDE APP LEARN BY EXAMPLE
27Sistemi di Elettronica Digitale A Flammini
4) ASSIGN AN ID TO THE NANO BY USING THE FLASHER APP
A) Launche the BleembeddedFlaser app installed at step 2 and click on laquoScan discoverable devicesraquo
B) Click on MyNano If there is more than one Nano verify characters on the label of the Nano and check that they match with the last 2 characters of the MAC address shown in the list
Warning Nano mujst be powered on Reset the power it still does not appear in the list
C) Wait until the device connects and disconnects A list of bluetooth services and characteristics provided is shown
D) Click on laquoStore attribute in description fileraquo and give it the name nanoesempio
REMEMBER THIS NAME
SANDROIDE APP LEARN BY EXAMPLE
28Sistemi di Elettronica Digitale A Flammini
5) CONNECT NANO AND COMPONENTS TO BREADBOARD AND POWER IT ON
PLEASE NOTE
- Led (only on Nano) can be connected withouth resistor because current is limited
- For SAndroidE the pin labels to consider are the blue ones if the label is P0_X then in SAndroidE you will have to use X
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SAndroidE components
19Sistemi di Elettronica Digitale A Flammini
bull SAndroidE library (Java)
Virtualizes remote devices
regardless of type and
operating mode
bull Device Descriptor Files (XMLJSON)
Represent a description
of the BT Services
provided by devices
bull API description files (XMLJSON)
Represent interface
to connect and retrieve
data from Cloud
bull SAndroidE configuration application
Used to detect nearby
BT devices and give
them a unique ID
All provided in the open-source code
SAndroidE supported devices
20Sistemi di Elettronica Digitale A Flammini
A User
programmable
devices
B Beacons and non-
programmable
devices
C Vendor Locked
device - data from
vendorrsquos APP
D Vendor Locked
device - data from
vendorrsquos Cloud
SAndroidE supported devices
21Sistemi di Elettronica Digitale A Flammini
A User programmable devices
B Beacons and
non-programmable devices
Devices like Arduino Raspberry Pi RedBear BLE Nano already
integrated
Typically require device flashing
With SAndroidE no need to write custom firmware for basic
operations
Flashing is not allowed but BT protocols are known
SAndroidE declares protocols in the Device Descriptor Files
In this way these devices can be easily virtualized
NOTE a BLE beacon does not need any coupling as it continuously broadcasts BLE messages to any device in range
BLE BEACONS
22Sistemi di Elettronica Digitale A Flammini
SAndroidE supported devices
23Sistemi di Elettronica Digitale A Flammini
C Vendor locked device ndash
data from vendorrsquosAPP
D Vendor locked device ndash
data from vendorrsquos
CLOUD
Some vendors apply an encrypted
BT protocol
Example Flic buttons
In this case SAndroidE works by
receiving notifications by vendorrsquos
APP
Some vendors provide data
only by accessing the
proprietary Cloud
Example Fitbit activity
trackers
SAndroidE retrieves such data
by means of API
SANDROIDE APP LEARN BY EXAMPLE
24Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVETo replace the text laquoHello Worldraquo with laquoHello World trimmer valueraquo
Furthermore we want to connect the button and the led not by wire but laquothrough the appraquo when the button is pressed the led must light on
QUI IMMAGINE BASETTA
SANDROIDE APP LEARN BY EXAMPLE
25Sistemi di Elettronica Digitale A Flammini
2) DOWNLOAD SANDROIDE amp PHONE CONFIGURATION
a) From httpsgithubcomSAndroidEOfficialframeworkreleases download the latest SAndroidE binary package sandroide-bin-v07zip
b) Decompress the zip file
c) Connect smartphone or tablet as mass storageUSB archive and copy within the smartphone internal memory these files
a) all xml files contained in the xml folder of the decompressed zip b) the file flasher-debugapk
d) Disconnect the smartphone and in laquoPhone Settings-gtSecurityraquo enable Installation from laquoUnknown Sourcesraquo
e) Using the laquoArchiveraquo or laquoFile Explorerraquo app of your Smartphone search and execute the file flasher-debugapk to to install it Accept all with OK
SANDROIDE APP LEARN BY EXAMPLE
26Sistemi di Elettronica Digitale A Flammini
3) FLASHING THE DEVICE WITH SANDROIDE COMPATIBILE FIRMWARE
The Nano device to interact with a SAndroidE APP must be provided with a firmware implementing the same communication protocol supported by SAndroidE on the smartphone side Letrsquos see how
a) Connect Nano to the PC using the appropriate USB programmer
b) From SandroidE wiki on Github httpsgithubcomSAndroidEOfficialframeworkwiki
access the Nano tutorial by clicking on laquoUsing RedBear Nano with SAndroidEraquo
c) In the section laquoFlashing the Nanoraquo click on laquoSandroide compatible Nano firmwareraquo and save the opened page (File-gtSave) in the MBEDDAPLINK drive
PLEASE PAY ATTENTION to save as hex and not as txt by selecting laquoAll filesraquo int the Saving form instead of laquoText fileraquo and by removing the suffix txt to the end of the file name
SANDROIDE APP LEARN BY EXAMPLE
27Sistemi di Elettronica Digitale A Flammini
4) ASSIGN AN ID TO THE NANO BY USING THE FLASHER APP
A) Launche the BleembeddedFlaser app installed at step 2 and click on laquoScan discoverable devicesraquo
B) Click on MyNano If there is more than one Nano verify characters on the label of the Nano and check that they match with the last 2 characters of the MAC address shown in the list
Warning Nano mujst be powered on Reset the power it still does not appear in the list
C) Wait until the device connects and disconnects A list of bluetooth services and characteristics provided is shown
D) Click on laquoStore attribute in description fileraquo and give it the name nanoesempio
REMEMBER THIS NAME
SANDROIDE APP LEARN BY EXAMPLE
28Sistemi di Elettronica Digitale A Flammini
5) CONNECT NANO AND COMPONENTS TO BREADBOARD AND POWER IT ON
PLEASE NOTE
- Led (only on Nano) can be connected withouth resistor because current is limited
- For SAndroidE the pin labels to consider are the blue ones if the label is P0_X then in SAndroidE you will have to use X
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SAndroidE supported devices
20Sistemi di Elettronica Digitale A Flammini
A User
programmable
devices
B Beacons and non-
programmable
devices
C Vendor Locked
device - data from
vendorrsquos APP
D Vendor Locked
device - data from
vendorrsquos Cloud
SAndroidE supported devices
21Sistemi di Elettronica Digitale A Flammini
A User programmable devices
B Beacons and
non-programmable devices
Devices like Arduino Raspberry Pi RedBear BLE Nano already
integrated
Typically require device flashing
With SAndroidE no need to write custom firmware for basic
operations
Flashing is not allowed but BT protocols are known
SAndroidE declares protocols in the Device Descriptor Files
In this way these devices can be easily virtualized
NOTE a BLE beacon does not need any coupling as it continuously broadcasts BLE messages to any device in range
BLE BEACONS
22Sistemi di Elettronica Digitale A Flammini
SAndroidE supported devices
23Sistemi di Elettronica Digitale A Flammini
C Vendor locked device ndash
data from vendorrsquosAPP
D Vendor locked device ndash
data from vendorrsquos
CLOUD
Some vendors apply an encrypted
BT protocol
Example Flic buttons
In this case SAndroidE works by
receiving notifications by vendorrsquos
APP
Some vendors provide data
only by accessing the
proprietary Cloud
Example Fitbit activity
trackers
SAndroidE retrieves such data
by means of API
SANDROIDE APP LEARN BY EXAMPLE
24Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVETo replace the text laquoHello Worldraquo with laquoHello World trimmer valueraquo
Furthermore we want to connect the button and the led not by wire but laquothrough the appraquo when the button is pressed the led must light on
QUI IMMAGINE BASETTA
SANDROIDE APP LEARN BY EXAMPLE
25Sistemi di Elettronica Digitale A Flammini
2) DOWNLOAD SANDROIDE amp PHONE CONFIGURATION
a) From httpsgithubcomSAndroidEOfficialframeworkreleases download the latest SAndroidE binary package sandroide-bin-v07zip
b) Decompress the zip file
c) Connect smartphone or tablet as mass storageUSB archive and copy within the smartphone internal memory these files
a) all xml files contained in the xml folder of the decompressed zip b) the file flasher-debugapk
d) Disconnect the smartphone and in laquoPhone Settings-gtSecurityraquo enable Installation from laquoUnknown Sourcesraquo
e) Using the laquoArchiveraquo or laquoFile Explorerraquo app of your Smartphone search and execute the file flasher-debugapk to to install it Accept all with OK
SANDROIDE APP LEARN BY EXAMPLE
26Sistemi di Elettronica Digitale A Flammini
3) FLASHING THE DEVICE WITH SANDROIDE COMPATIBILE FIRMWARE
The Nano device to interact with a SAndroidE APP must be provided with a firmware implementing the same communication protocol supported by SAndroidE on the smartphone side Letrsquos see how
a) Connect Nano to the PC using the appropriate USB programmer
b) From SandroidE wiki on Github httpsgithubcomSAndroidEOfficialframeworkwiki
access the Nano tutorial by clicking on laquoUsing RedBear Nano with SAndroidEraquo
c) In the section laquoFlashing the Nanoraquo click on laquoSandroide compatible Nano firmwareraquo and save the opened page (File-gtSave) in the MBEDDAPLINK drive
PLEASE PAY ATTENTION to save as hex and not as txt by selecting laquoAll filesraquo int the Saving form instead of laquoText fileraquo and by removing the suffix txt to the end of the file name
SANDROIDE APP LEARN BY EXAMPLE
27Sistemi di Elettronica Digitale A Flammini
4) ASSIGN AN ID TO THE NANO BY USING THE FLASHER APP
A) Launche the BleembeddedFlaser app installed at step 2 and click on laquoScan discoverable devicesraquo
B) Click on MyNano If there is more than one Nano verify characters on the label of the Nano and check that they match with the last 2 characters of the MAC address shown in the list
Warning Nano mujst be powered on Reset the power it still does not appear in the list
C) Wait until the device connects and disconnects A list of bluetooth services and characteristics provided is shown
D) Click on laquoStore attribute in description fileraquo and give it the name nanoesempio
REMEMBER THIS NAME
SANDROIDE APP LEARN BY EXAMPLE
28Sistemi di Elettronica Digitale A Flammini
5) CONNECT NANO AND COMPONENTS TO BREADBOARD AND POWER IT ON
PLEASE NOTE
- Led (only on Nano) can be connected withouth resistor because current is limited
- For SAndroidE the pin labels to consider are the blue ones if the label is P0_X then in SAndroidE you will have to use X
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SAndroidE supported devices
21Sistemi di Elettronica Digitale A Flammini
A User programmable devices
B Beacons and
non-programmable devices
Devices like Arduino Raspberry Pi RedBear BLE Nano already
integrated
Typically require device flashing
With SAndroidE no need to write custom firmware for basic
operations
Flashing is not allowed but BT protocols are known
SAndroidE declares protocols in the Device Descriptor Files
In this way these devices can be easily virtualized
NOTE a BLE beacon does not need any coupling as it continuously broadcasts BLE messages to any device in range
BLE BEACONS
22Sistemi di Elettronica Digitale A Flammini
SAndroidE supported devices
23Sistemi di Elettronica Digitale A Flammini
C Vendor locked device ndash
data from vendorrsquosAPP
D Vendor locked device ndash
data from vendorrsquos
CLOUD
Some vendors apply an encrypted
BT protocol
Example Flic buttons
In this case SAndroidE works by
receiving notifications by vendorrsquos
APP
Some vendors provide data
only by accessing the
proprietary Cloud
Example Fitbit activity
trackers
SAndroidE retrieves such data
by means of API
SANDROIDE APP LEARN BY EXAMPLE
24Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVETo replace the text laquoHello Worldraquo with laquoHello World trimmer valueraquo
Furthermore we want to connect the button and the led not by wire but laquothrough the appraquo when the button is pressed the led must light on
QUI IMMAGINE BASETTA
SANDROIDE APP LEARN BY EXAMPLE
25Sistemi di Elettronica Digitale A Flammini
2) DOWNLOAD SANDROIDE amp PHONE CONFIGURATION
a) From httpsgithubcomSAndroidEOfficialframeworkreleases download the latest SAndroidE binary package sandroide-bin-v07zip
b) Decompress the zip file
c) Connect smartphone or tablet as mass storageUSB archive and copy within the smartphone internal memory these files
a) all xml files contained in the xml folder of the decompressed zip b) the file flasher-debugapk
d) Disconnect the smartphone and in laquoPhone Settings-gtSecurityraquo enable Installation from laquoUnknown Sourcesraquo
e) Using the laquoArchiveraquo or laquoFile Explorerraquo app of your Smartphone search and execute the file flasher-debugapk to to install it Accept all with OK
SANDROIDE APP LEARN BY EXAMPLE
26Sistemi di Elettronica Digitale A Flammini
3) FLASHING THE DEVICE WITH SANDROIDE COMPATIBILE FIRMWARE
The Nano device to interact with a SAndroidE APP must be provided with a firmware implementing the same communication protocol supported by SAndroidE on the smartphone side Letrsquos see how
a) Connect Nano to the PC using the appropriate USB programmer
b) From SandroidE wiki on Github httpsgithubcomSAndroidEOfficialframeworkwiki
access the Nano tutorial by clicking on laquoUsing RedBear Nano with SAndroidEraquo
c) In the section laquoFlashing the Nanoraquo click on laquoSandroide compatible Nano firmwareraquo and save the opened page (File-gtSave) in the MBEDDAPLINK drive
PLEASE PAY ATTENTION to save as hex and not as txt by selecting laquoAll filesraquo int the Saving form instead of laquoText fileraquo and by removing the suffix txt to the end of the file name
SANDROIDE APP LEARN BY EXAMPLE
27Sistemi di Elettronica Digitale A Flammini
4) ASSIGN AN ID TO THE NANO BY USING THE FLASHER APP
A) Launche the BleembeddedFlaser app installed at step 2 and click on laquoScan discoverable devicesraquo
B) Click on MyNano If there is more than one Nano verify characters on the label of the Nano and check that they match with the last 2 characters of the MAC address shown in the list
Warning Nano mujst be powered on Reset the power it still does not appear in the list
C) Wait until the device connects and disconnects A list of bluetooth services and characteristics provided is shown
D) Click on laquoStore attribute in description fileraquo and give it the name nanoesempio
REMEMBER THIS NAME
SANDROIDE APP LEARN BY EXAMPLE
28Sistemi di Elettronica Digitale A Flammini
5) CONNECT NANO AND COMPONENTS TO BREADBOARD AND POWER IT ON
PLEASE NOTE
- Led (only on Nano) can be connected withouth resistor because current is limited
- For SAndroidE the pin labels to consider are the blue ones if the label is P0_X then in SAndroidE you will have to use X
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
BLE BEACONS
22Sistemi di Elettronica Digitale A Flammini
SAndroidE supported devices
23Sistemi di Elettronica Digitale A Flammini
C Vendor locked device ndash
data from vendorrsquosAPP
D Vendor locked device ndash
data from vendorrsquos
CLOUD
Some vendors apply an encrypted
BT protocol
Example Flic buttons
In this case SAndroidE works by
receiving notifications by vendorrsquos
APP
Some vendors provide data
only by accessing the
proprietary Cloud
Example Fitbit activity
trackers
SAndroidE retrieves such data
by means of API
SANDROIDE APP LEARN BY EXAMPLE
24Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVETo replace the text laquoHello Worldraquo with laquoHello World trimmer valueraquo
Furthermore we want to connect the button and the led not by wire but laquothrough the appraquo when the button is pressed the led must light on
QUI IMMAGINE BASETTA
SANDROIDE APP LEARN BY EXAMPLE
25Sistemi di Elettronica Digitale A Flammini
2) DOWNLOAD SANDROIDE amp PHONE CONFIGURATION
a) From httpsgithubcomSAndroidEOfficialframeworkreleases download the latest SAndroidE binary package sandroide-bin-v07zip
b) Decompress the zip file
c) Connect smartphone or tablet as mass storageUSB archive and copy within the smartphone internal memory these files
a) all xml files contained in the xml folder of the decompressed zip b) the file flasher-debugapk
d) Disconnect the smartphone and in laquoPhone Settings-gtSecurityraquo enable Installation from laquoUnknown Sourcesraquo
e) Using the laquoArchiveraquo or laquoFile Explorerraquo app of your Smartphone search and execute the file flasher-debugapk to to install it Accept all with OK
SANDROIDE APP LEARN BY EXAMPLE
26Sistemi di Elettronica Digitale A Flammini
3) FLASHING THE DEVICE WITH SANDROIDE COMPATIBILE FIRMWARE
The Nano device to interact with a SAndroidE APP must be provided with a firmware implementing the same communication protocol supported by SAndroidE on the smartphone side Letrsquos see how
a) Connect Nano to the PC using the appropriate USB programmer
b) From SandroidE wiki on Github httpsgithubcomSAndroidEOfficialframeworkwiki
access the Nano tutorial by clicking on laquoUsing RedBear Nano with SAndroidEraquo
c) In the section laquoFlashing the Nanoraquo click on laquoSandroide compatible Nano firmwareraquo and save the opened page (File-gtSave) in the MBEDDAPLINK drive
PLEASE PAY ATTENTION to save as hex and not as txt by selecting laquoAll filesraquo int the Saving form instead of laquoText fileraquo and by removing the suffix txt to the end of the file name
SANDROIDE APP LEARN BY EXAMPLE
27Sistemi di Elettronica Digitale A Flammini
4) ASSIGN AN ID TO THE NANO BY USING THE FLASHER APP
A) Launche the BleembeddedFlaser app installed at step 2 and click on laquoScan discoverable devicesraquo
B) Click on MyNano If there is more than one Nano verify characters on the label of the Nano and check that they match with the last 2 characters of the MAC address shown in the list
Warning Nano mujst be powered on Reset the power it still does not appear in the list
C) Wait until the device connects and disconnects A list of bluetooth services and characteristics provided is shown
D) Click on laquoStore attribute in description fileraquo and give it the name nanoesempio
REMEMBER THIS NAME
SANDROIDE APP LEARN BY EXAMPLE
28Sistemi di Elettronica Digitale A Flammini
5) CONNECT NANO AND COMPONENTS TO BREADBOARD AND POWER IT ON
PLEASE NOTE
- Led (only on Nano) can be connected withouth resistor because current is limited
- For SAndroidE the pin labels to consider are the blue ones if the label is P0_X then in SAndroidE you will have to use X
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SAndroidE supported devices
23Sistemi di Elettronica Digitale A Flammini
C Vendor locked device ndash
data from vendorrsquosAPP
D Vendor locked device ndash
data from vendorrsquos
CLOUD
Some vendors apply an encrypted
BT protocol
Example Flic buttons
In this case SAndroidE works by
receiving notifications by vendorrsquos
APP
Some vendors provide data
only by accessing the
proprietary Cloud
Example Fitbit activity
trackers
SAndroidE retrieves such data
by means of API
SANDROIDE APP LEARN BY EXAMPLE
24Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVETo replace the text laquoHello Worldraquo with laquoHello World trimmer valueraquo
Furthermore we want to connect the button and the led not by wire but laquothrough the appraquo when the button is pressed the led must light on
QUI IMMAGINE BASETTA
SANDROIDE APP LEARN BY EXAMPLE
25Sistemi di Elettronica Digitale A Flammini
2) DOWNLOAD SANDROIDE amp PHONE CONFIGURATION
a) From httpsgithubcomSAndroidEOfficialframeworkreleases download the latest SAndroidE binary package sandroide-bin-v07zip
b) Decompress the zip file
c) Connect smartphone or tablet as mass storageUSB archive and copy within the smartphone internal memory these files
a) all xml files contained in the xml folder of the decompressed zip b) the file flasher-debugapk
d) Disconnect the smartphone and in laquoPhone Settings-gtSecurityraquo enable Installation from laquoUnknown Sourcesraquo
e) Using the laquoArchiveraquo or laquoFile Explorerraquo app of your Smartphone search and execute the file flasher-debugapk to to install it Accept all with OK
SANDROIDE APP LEARN BY EXAMPLE
26Sistemi di Elettronica Digitale A Flammini
3) FLASHING THE DEVICE WITH SANDROIDE COMPATIBILE FIRMWARE
The Nano device to interact with a SAndroidE APP must be provided with a firmware implementing the same communication protocol supported by SAndroidE on the smartphone side Letrsquos see how
a) Connect Nano to the PC using the appropriate USB programmer
b) From SandroidE wiki on Github httpsgithubcomSAndroidEOfficialframeworkwiki
access the Nano tutorial by clicking on laquoUsing RedBear Nano with SAndroidEraquo
c) In the section laquoFlashing the Nanoraquo click on laquoSandroide compatible Nano firmwareraquo and save the opened page (File-gtSave) in the MBEDDAPLINK drive
PLEASE PAY ATTENTION to save as hex and not as txt by selecting laquoAll filesraquo int the Saving form instead of laquoText fileraquo and by removing the suffix txt to the end of the file name
SANDROIDE APP LEARN BY EXAMPLE
27Sistemi di Elettronica Digitale A Flammini
4) ASSIGN AN ID TO THE NANO BY USING THE FLASHER APP
A) Launche the BleembeddedFlaser app installed at step 2 and click on laquoScan discoverable devicesraquo
B) Click on MyNano If there is more than one Nano verify characters on the label of the Nano and check that they match with the last 2 characters of the MAC address shown in the list
Warning Nano mujst be powered on Reset the power it still does not appear in the list
C) Wait until the device connects and disconnects A list of bluetooth services and characteristics provided is shown
D) Click on laquoStore attribute in description fileraquo and give it the name nanoesempio
REMEMBER THIS NAME
SANDROIDE APP LEARN BY EXAMPLE
28Sistemi di Elettronica Digitale A Flammini
5) CONNECT NANO AND COMPONENTS TO BREADBOARD AND POWER IT ON
PLEASE NOTE
- Led (only on Nano) can be connected withouth resistor because current is limited
- For SAndroidE the pin labels to consider are the blue ones if the label is P0_X then in SAndroidE you will have to use X
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SANDROIDE APP LEARN BY EXAMPLE
24Sistemi di Elettronica Digitale A Flammini
MODIFY EMPTY APP (EXAMPLE)
1) OBJECTIVETo replace the text laquoHello Worldraquo with laquoHello World trimmer valueraquo
Furthermore we want to connect the button and the led not by wire but laquothrough the appraquo when the button is pressed the led must light on
QUI IMMAGINE BASETTA
SANDROIDE APP LEARN BY EXAMPLE
25Sistemi di Elettronica Digitale A Flammini
2) DOWNLOAD SANDROIDE amp PHONE CONFIGURATION
a) From httpsgithubcomSAndroidEOfficialframeworkreleases download the latest SAndroidE binary package sandroide-bin-v07zip
b) Decompress the zip file
c) Connect smartphone or tablet as mass storageUSB archive and copy within the smartphone internal memory these files
a) all xml files contained in the xml folder of the decompressed zip b) the file flasher-debugapk
d) Disconnect the smartphone and in laquoPhone Settings-gtSecurityraquo enable Installation from laquoUnknown Sourcesraquo
e) Using the laquoArchiveraquo or laquoFile Explorerraquo app of your Smartphone search and execute the file flasher-debugapk to to install it Accept all with OK
SANDROIDE APP LEARN BY EXAMPLE
26Sistemi di Elettronica Digitale A Flammini
3) FLASHING THE DEVICE WITH SANDROIDE COMPATIBILE FIRMWARE
The Nano device to interact with a SAndroidE APP must be provided with a firmware implementing the same communication protocol supported by SAndroidE on the smartphone side Letrsquos see how
a) Connect Nano to the PC using the appropriate USB programmer
b) From SandroidE wiki on Github httpsgithubcomSAndroidEOfficialframeworkwiki
access the Nano tutorial by clicking on laquoUsing RedBear Nano with SAndroidEraquo
c) In the section laquoFlashing the Nanoraquo click on laquoSandroide compatible Nano firmwareraquo and save the opened page (File-gtSave) in the MBEDDAPLINK drive
PLEASE PAY ATTENTION to save as hex and not as txt by selecting laquoAll filesraquo int the Saving form instead of laquoText fileraquo and by removing the suffix txt to the end of the file name
SANDROIDE APP LEARN BY EXAMPLE
27Sistemi di Elettronica Digitale A Flammini
4) ASSIGN AN ID TO THE NANO BY USING THE FLASHER APP
A) Launche the BleembeddedFlaser app installed at step 2 and click on laquoScan discoverable devicesraquo
B) Click on MyNano If there is more than one Nano verify characters on the label of the Nano and check that they match with the last 2 characters of the MAC address shown in the list
Warning Nano mujst be powered on Reset the power it still does not appear in the list
C) Wait until the device connects and disconnects A list of bluetooth services and characteristics provided is shown
D) Click on laquoStore attribute in description fileraquo and give it the name nanoesempio
REMEMBER THIS NAME
SANDROIDE APP LEARN BY EXAMPLE
28Sistemi di Elettronica Digitale A Flammini
5) CONNECT NANO AND COMPONENTS TO BREADBOARD AND POWER IT ON
PLEASE NOTE
- Led (only on Nano) can be connected withouth resistor because current is limited
- For SAndroidE the pin labels to consider are the blue ones if the label is P0_X then in SAndroidE you will have to use X
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SANDROIDE APP LEARN BY EXAMPLE
25Sistemi di Elettronica Digitale A Flammini
2) DOWNLOAD SANDROIDE amp PHONE CONFIGURATION
a) From httpsgithubcomSAndroidEOfficialframeworkreleases download the latest SAndroidE binary package sandroide-bin-v07zip
b) Decompress the zip file
c) Connect smartphone or tablet as mass storageUSB archive and copy within the smartphone internal memory these files
a) all xml files contained in the xml folder of the decompressed zip b) the file flasher-debugapk
d) Disconnect the smartphone and in laquoPhone Settings-gtSecurityraquo enable Installation from laquoUnknown Sourcesraquo
e) Using the laquoArchiveraquo or laquoFile Explorerraquo app of your Smartphone search and execute the file flasher-debugapk to to install it Accept all with OK
SANDROIDE APP LEARN BY EXAMPLE
26Sistemi di Elettronica Digitale A Flammini
3) FLASHING THE DEVICE WITH SANDROIDE COMPATIBILE FIRMWARE
The Nano device to interact with a SAndroidE APP must be provided with a firmware implementing the same communication protocol supported by SAndroidE on the smartphone side Letrsquos see how
a) Connect Nano to the PC using the appropriate USB programmer
b) From SandroidE wiki on Github httpsgithubcomSAndroidEOfficialframeworkwiki
access the Nano tutorial by clicking on laquoUsing RedBear Nano with SAndroidEraquo
c) In the section laquoFlashing the Nanoraquo click on laquoSandroide compatible Nano firmwareraquo and save the opened page (File-gtSave) in the MBEDDAPLINK drive
PLEASE PAY ATTENTION to save as hex and not as txt by selecting laquoAll filesraquo int the Saving form instead of laquoText fileraquo and by removing the suffix txt to the end of the file name
SANDROIDE APP LEARN BY EXAMPLE
27Sistemi di Elettronica Digitale A Flammini
4) ASSIGN AN ID TO THE NANO BY USING THE FLASHER APP
A) Launche the BleembeddedFlaser app installed at step 2 and click on laquoScan discoverable devicesraquo
B) Click on MyNano If there is more than one Nano verify characters on the label of the Nano and check that they match with the last 2 characters of the MAC address shown in the list
Warning Nano mujst be powered on Reset the power it still does not appear in the list
C) Wait until the device connects and disconnects A list of bluetooth services and characteristics provided is shown
D) Click on laquoStore attribute in description fileraquo and give it the name nanoesempio
REMEMBER THIS NAME
SANDROIDE APP LEARN BY EXAMPLE
28Sistemi di Elettronica Digitale A Flammini
5) CONNECT NANO AND COMPONENTS TO BREADBOARD AND POWER IT ON
PLEASE NOTE
- Led (only on Nano) can be connected withouth resistor because current is limited
- For SAndroidE the pin labels to consider are the blue ones if the label is P0_X then in SAndroidE you will have to use X
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SANDROIDE APP LEARN BY EXAMPLE
26Sistemi di Elettronica Digitale A Flammini
3) FLASHING THE DEVICE WITH SANDROIDE COMPATIBILE FIRMWARE
The Nano device to interact with a SAndroidE APP must be provided with a firmware implementing the same communication protocol supported by SAndroidE on the smartphone side Letrsquos see how
a) Connect Nano to the PC using the appropriate USB programmer
b) From SandroidE wiki on Github httpsgithubcomSAndroidEOfficialframeworkwiki
access the Nano tutorial by clicking on laquoUsing RedBear Nano with SAndroidEraquo
c) In the section laquoFlashing the Nanoraquo click on laquoSandroide compatible Nano firmwareraquo and save the opened page (File-gtSave) in the MBEDDAPLINK drive
PLEASE PAY ATTENTION to save as hex and not as txt by selecting laquoAll filesraquo int the Saving form instead of laquoText fileraquo and by removing the suffix txt to the end of the file name
SANDROIDE APP LEARN BY EXAMPLE
27Sistemi di Elettronica Digitale A Flammini
4) ASSIGN AN ID TO THE NANO BY USING THE FLASHER APP
A) Launche the BleembeddedFlaser app installed at step 2 and click on laquoScan discoverable devicesraquo
B) Click on MyNano If there is more than one Nano verify characters on the label of the Nano and check that they match with the last 2 characters of the MAC address shown in the list
Warning Nano mujst be powered on Reset the power it still does not appear in the list
C) Wait until the device connects and disconnects A list of bluetooth services and characteristics provided is shown
D) Click on laquoStore attribute in description fileraquo and give it the name nanoesempio
REMEMBER THIS NAME
SANDROIDE APP LEARN BY EXAMPLE
28Sistemi di Elettronica Digitale A Flammini
5) CONNECT NANO AND COMPONENTS TO BREADBOARD AND POWER IT ON
PLEASE NOTE
- Led (only on Nano) can be connected withouth resistor because current is limited
- For SAndroidE the pin labels to consider are the blue ones if the label is P0_X then in SAndroidE you will have to use X
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SANDROIDE APP LEARN BY EXAMPLE
27Sistemi di Elettronica Digitale A Flammini
4) ASSIGN AN ID TO THE NANO BY USING THE FLASHER APP
A) Launche the BleembeddedFlaser app installed at step 2 and click on laquoScan discoverable devicesraquo
B) Click on MyNano If there is more than one Nano verify characters on the label of the Nano and check that they match with the last 2 characters of the MAC address shown in the list
Warning Nano mujst be powered on Reset the power it still does not appear in the list
C) Wait until the device connects and disconnects A list of bluetooth services and characteristics provided is shown
D) Click on laquoStore attribute in description fileraquo and give it the name nanoesempio
REMEMBER THIS NAME
SANDROIDE APP LEARN BY EXAMPLE
28Sistemi di Elettronica Digitale A Flammini
5) CONNECT NANO AND COMPONENTS TO BREADBOARD AND POWER IT ON
PLEASE NOTE
- Led (only on Nano) can be connected withouth resistor because current is limited
- For SAndroidE the pin labels to consider are the blue ones if the label is P0_X then in SAndroidE you will have to use X
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SANDROIDE APP LEARN BY EXAMPLE
28Sistemi di Elettronica Digitale A Flammini
5) CONNECT NANO AND COMPONENTS TO BREADBOARD AND POWER IT ON
PLEASE NOTE
- Led (only on Nano) can be connected withouth resistor because current is limited
- For SAndroidE the pin labels to consider are the blue ones if the label is P0_X then in SAndroidE you will have to use X
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SANDROIDE APP LEARN BY EXAMPLE
29Sistemi di Elettronica Digitale A Flammini
6) IMPORT SANDROIDE LIBRARY COPY-PASTE SANDROIDEAAR
Copy the SAndroidEaar file(or SAndroidEv-07aar) included in the SandroidE zip file in the Android project folder applibs Right click on libs -gt Paste
If libs folder does not appear click on the menu shown in figure and select the view Project
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SANDROIDE APP LEARN BY EXAMPLE
30Sistemi di Elettronica Digitale A Flammini
7) IMPORT SANDROIDE LIBRARY BUILDGRADLE
A) Insert in the buildgradle of the app folder the following code
allprojects repositories
jcenter()flatDir dirs lsquolibsrsquo
dependencies
compile(nameSAndroidE-v07 extaar)
B) Click on Sync Now
C) When finished this text should appear at the bottom of the page
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SANDROIDE APP LEARN BY EXAMPLE
31Sistemi di Elettronica Digitale A Flammini
8) INHERITS FROM SANDROIDEAPPLICATION
Add to the manifest file AndroidManifestxml within the tag application this attribute
androidname=itunibssandroidelibactivitiesSandroideApplication
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SANDROIDE APP LEARN BY EXAMPLE
32Sistemi di Elettronica Digitale A Flammini
9) INHERITS FROM SANDROIDEBASEACTIVITY
Modify the MainActivityjava file by adding
extends SandroideBaseActivity
Remember to press ALT+Enter if the words are shown in red color
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SANDROIDE APP LEARN BY EXAMPLE
33Sistemi di Elettronica Digitale A Flammini
10) READ TRIMMER VALUE (see next slide for code position)
SandroideDevice nano
SandroidePin nanoTrimmer
A) Add the variables which will represent the physical device and the trimmer
nano = ((SandroideDevice) BLEContextfindViewById(nanoesempio_sandroide_device))
nanoTrimmer = new SandroidePin()setDevice(nano 6) we connect to pin number 6
setMode(SandroidePinPIN_MODE_ANALOG_INPUT) the pin is analog input
setDeltaThreshold(001) means value is changed of 1 percent of fullscale
setSamplingInterval(500) means sample every 500ms
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
runOnUiThread(new Runnable()
Override
public void run()
tvsetText(Hello world +
trimmer=+newValuetoString())
)
)
B) Connect to device identified by laquonanoesempioraquo followed by laquo_sandroide_deviceraquo
C) Define the pin 6 as analog pin and write the callback to execute when its value changes
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SANDROIDE APP LEARN BY EXAMPLE
34Sistemi di Elettronica Digitale A Flammini
11) THE CODE SHOULD BE SIMILAR TO
A B
C
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SANDROIDE APP LEARN BY EXAMPLE
35Sistemi di Elettronica Digitale A Flammini
12) CODE EXPLANATION
bull The method BleContextfindViewById(iddevice) connects the physical device to the app by creating a virtual object of type SandroideDevice and storing it to the variable named nano
bull In the trimmer creationbull The method setDevice is used to indicate the the virtual object nanoTrimmer of
type SandroidePin belongs to the physical object connected to the virtual object named nano at pin position number 4 (see the pinout)
bull The method setSamplingThreshold(ms) received a number in milliseconds which is the sampling interval to use for that pin Different sampling intervals can be defined for different pin numbers
bull The method setDeltaThreshold(X) receives a float value from 0 to 1 and instructs the device to sent the pin value only if it differs of +- X percent from the last sent value Example setDeltaThreshold(01) asks the Nano(which operates at 33V) to send the new value only if it is +-033V from the last sent value
bull La callback ha come parametro newValue di tipo object nel caso di pin analogici questo object deve essere castato a float in caso di pin digitali a boolean
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SANDROIDE APP LEARN BY EXAMPLE
36Sistemi di Elettronica Digitale A Flammini
13) RUN
PLEASE NOTE The first time you run a SAndroidE app you are asked to grant permissions Just press OK
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SANDROIDE APP LEARN BY EXAMPLE
37Sistemi di Elettronica Digitale A Flammini
14) BUTTON STATE -gt LED STATE (see next slide for code position)
A) Add variables to contain virtual objects representing pins of button and led
B) Create the digital output pin representing the led
C) Create the digital input representing the button and at each change of state (press or release) modify the state of the led(onoff) accordingly
SandroidePin nanoButton
SandroidePin nanoLed
nanoLed = new SandroidePin()setDevice(nano28)
setMode(SandroidePinPIN_MODE_DIGITAL_OUTPUT)
nanoButton = new SandroidePin()setDevice(nano15)
setMode(SandroidePinPIN_MODE_DIGITAL_INPUT)
setOnValueReceived(new SandroidePinOnValueReceivedListener()
Override
public void onEvent(final Object newValue Object oldValue)
nanoLedsetValue(newValue)
)
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SANDROIDE APP LEARN BY EXAMPLE
38Sistemi di Elettronica Digitale A Flammini
15) THE CODE SHOULD BE SIMILAR TO
A
B
C
The method setValue() of a SandroidePin object receives a boolean value true or false to change the state of the pin to HIGH or LOW
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SANDROIDE APP LEARN BY EXAMPLE
39Sistemi di Elettronica Digitale A Flammini
16) RUN
IMMAGINE CON DITO CHE PREME PULSANTE
Just press and release the button to see the LED turning ON and OFF
Now just think if the LED was connected to another Nano you can remotely control everything you want
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SANDROIDE OTHER TUTORIALS
40Sistemi di Elettronica Digitale A Flammini
Altri tutorial sono disponibili su Github allrsquoindirizzo
httpsgithubcomSAndroidEOfficialframeworkwiki
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework
SANDROIDE PROJECT WEB
41Sistemi di Elettronica Digitale A Flammini
httpes3unibsitSAndroidE
Visit SAndroidE website for links resources and videos to get started
On Github you can contribute to extend the SAndroidE core adding new devices or creating new tutorials
httpsgithubcomSAndroidEOfficialframework