public class SensorController
extends java.lang.Object
The SensorController binds to the internal Service element
SensorService
and cooperates with it.
Due to the nature of messaging system, all methods listed below should be handled as asynchronous requests.
User of this class must implement the SensorListener
in the calling Activity
, so that the operation result of an
asynchronous request or any error conditions to be notified.
Constructor and Description |
---|
SensorController(android.content.Context context,
int clientId)
Constructs a SensorController instance.
|
Modifier and Type | Method and Description |
---|---|
void |
bindSensorService()
Binds to the
SensorService to start sensor handling. |
void |
disableSensors(java.util.ArrayList<java.lang.Integer> sensorTypes)
Ask
SensorService to disable designated sensors. |
void |
enableSensors(java.util.ArrayList<java.lang.Integer> sensorTypes)
Ask
SensorService to enable designated sensors. |
void |
getAvailableSensorTypes()
Ask
SensorService for the all sensor information available
on this device. |
void |
setIntervalTimer(long seconds)
Ask
SensorService to set minimum time spacing for each
SensorListener.onSensorDataReceived(java.lang.String) notifications. |
void |
setLocation(float longitude,
float latitude)
Ask
SensorService to keep the geological location
(longitude, latitude) of this device. |
void |
setUserData(java.lang.String publisher,
java.lang.String note)
Ask
SensorService to keep the user data of caller. |
void |
unbindSensorService()
Unbinds from the
SensorService to finish sensor handling. |
public SensorController(@NonNull android.content.Context context, int clientId)
context
- the Application context which implements
SensorListener
,
usually it is the calling Activity
itself.clientId
- the client ID which distinguishes myself from
other clients bound to the same SensorService
.java.lang.RuntimeException
- if given context does not implement
the required listener.public void bindSensorService()
SensorService
to start sensor handling.
This is an asynchronous request and thus caller should wait
for a notification to know the operation result.
On success, caller will be notified by
SensorListener.onSensorEngaged(java.lang.String)
, otherwise
notified by SensorListener.onError(java.lang.String)
.
public void unbindSensorService()
SensorService
to finish sensor handling.
This is an asynchronous request and thus caller should wait
for a notification to know the operation result.
On success, caller will be notified by
SensorListener.onSensorDisengaged(java.lang.String)
, otherwise
notified by SensorListener.onError(java.lang.String)
.
public void getAvailableSensorTypes()
SensorService
for the all sensor information available
on this device.
This is an asynchronous request and thus caller should wait
for a notification to know the operation result.
On success, caller will be notified by
SensorListener.onSensorTypesReceived(java.util.ArrayList<java.lang.Integer>, java.util.ArrayList<java.lang.String>)
,
otherwise SensorListener.onError(java.lang.String)
will be notified.
NOTE: Availability of sensor devices depends on the running environment. Some devices may even have vendor private sensors those which we have no idea how to handle those readout values. To eliminate ambiguity, we only handle sensors those which types and values are defined in the Android Developers document.
public void enableSensors(@NonNull java.util.ArrayList<java.lang.Integer> sensorTypes)
SensorService
to enable designated sensors.
This is an asynchronous request, but caller DON'T have to
wait for the operation result.
As soon as designated sensors being enabled, those readout
data will be periodically notified by
SensorListener.onSensorDataReceived(java.lang.String)
.
If something goes bad, SensorListener.onError(java.lang.String)
will
be notified.
NOTE:
The preloaded list of available sensor types on this device
is kept in the SensorService
. If the caller specifies
an unknown sensorType, it will simply be ignored.
sensorTypes
- ArrayList of target sensor typespublic void disableSensors(@NonNull java.util.ArrayList<java.lang.Integer> sensorTypes)
SensorService
to disable designated sensors.
This is an asynchronous request, but caller DON'T have to
wait for the operation result.
As soon as designated sensors being disabled, those readout
data will NOT be notified anymore.
If something goes bad, SensorListener.onError(java.lang.String)
will
be notified.
NOTE:
The preloaded list of available sensor types on this device
is kept in the SensorService
. If the caller specifies
an unknown sensorType, it will simply be ignored.
sensorTypes
- ArrayList of target sensor typespublic void setIntervalTimer(long seconds)
SensorService
to set minimum time spacing for each
SensorListener.onSensorDataReceived(java.lang.String)
notifications.
Calling of this method is optional. If omitted, default value 10 (seconds) will be used.
seconds
- interval timer, where {0 < seconds <= Long.MAX_VALUE}public void setLocation(float longitude, float latitude)
SensorService
to keep the geological location
(longitude, latitude) of this device.
The specified value pair will be embedded in the JSON data
notified by SensorListener.onSensorDataReceived(java.lang.String)
.
Calling of method is optional. If omitted, empty location daa will be used.
longitude
- longitude of this device, where {-180.0 <= longitude <= 180.0}latitude
- latitude of this device, where {-90.0 <= latitude <= 90.0}public void setUserData(@Nullable java.lang.String publisher, @Nullable java.lang.String note)
SensorService
to keep the user data of caller.
The specified values will be embedded in the JSON data
notified by SensorListener.onSensorDataReceived(java.lang.String)
.
Calling of this method is optional. If omitted, empty user data will be used.
publisher
- user descriptions, if anynote
- additional comment, if any