giraffplus:android-middleware
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
giraffplus:android-middleware [2013-08-06 16:04] – [One-time setup] Add middleware configuration details andrea | giraffplus:android-middleware [2014-05-30 16:10] (current) – [GiraffPlus packages] davide | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== GiraffPlus on Android ====== | ====== GiraffPlus on Android ====== | ||
Make sure you’ve obtained the archive interfaces.middleware.zip (it is in the Android\giraff.android folder from the svn checkout). This file contains the AIDL interface needed to communicate with the middleware. In order to setup your application you need to extract this file to the src/ directory of the project. | Make sure you’ve obtained the archive interfaces.middleware.zip (it is in the Android\giraff.android folder from the svn checkout). This file contains the AIDL interface needed to communicate with the middleware. In order to setup your application you need to extract this file to the src/ directory of the project. | ||
+ | |||
+ | |||
+ | ===== GiraffPlus packages ==== | ||
+ | |||
+ | Middleware: | ||
+ | * https:// | ||
+ | * https:// | ||
+ | | ||
+ | Previous versions: | ||
+ | * https:// | ||
+ | * https:// | ||
===== One-time setup ===== | ===== One-time setup ===== | ||
Applications that use this infrastructure require that the middleware application (Android/ | Applications that use this infrastructure require that the middleware application (Android/ | ||
Also, the middleware needs to be configured **before** using any of the clients applications, | Also, the middleware needs to be configured **before** using any of the clients applications, | ||
- | * URI of the MQTT broker ("< | + | * URI of the MQTT broker ("< |
- | * user of the MQTT broker; | + | * Trust keystore password |
- | * password of the MQTT broker; | + | * Client keystore password |
- | * location string. | + | * Path to the trust keystore file (bks format) |
+ | * Path to the client keystore file (bks format) | ||
+ | |||
+ | **Notice.** In order to convert a jks keystore to a bks keystore: | ||
+ | - Download the utility Portecle (http:// | ||
+ | - Open the *.jks keystore | ||
+ | - Select //Tools// -> //Change Keystore Type// -> //BKS// | ||
+ | - Enter the password | ||
+ | * On the Oracle Java Downloads webpage (http:// | ||
+ | * Unzip the archive and copy the local_policy.jar and US_export_policy.jar files to the $JAVA_HOME/ | ||
+ | * Restart Portecle | ||
Make sure to kill the " | Make sure to kill the " | ||
Line 54: | Line 75: | ||
{{: | {{: | ||
- | The SensorsService class initializes | + | The SensorsService class initializes |
- | - Import middleware | + | - Import middleware |
- Implement a service connection | - Implement a service connection | ||
- Implement the callback logic (optional) | - Implement the callback logic (optional) | ||
Line 127: | Line 148: | ||
==== Implement an error callback ==== | ==== Implement an error callback ==== | ||
---- | ---- | ||
- | Each method in the API takes an optional (can be null) errorListener | + | Each method in the API takes an optional (can be null) resultListener |
<code java> | <code java> | ||
- | private | + | private |
| | ||
@Override | @Override | ||
public void error(Bundle info) throws RemoteException { | public void error(Bundle info) throws RemoteException { | ||
// ... | // ... | ||
+ | } | ||
+ | | ||
+ | @Override | ||
+ | public void success () throws RemoteException { | ||
+ | // ... | ||
} | } | ||
}; | }; | ||
Line 177: | Line 203: | ||
</ | </ | ||
- | The SensorsDescriptors constructor creates a descriptor for each sensor present on the device | + | We called |
<code java> | <code java> | ||
- | private final static int SENSORS_DELAY = SensorManager.SENSOR_DELAY_NORMAL; | + | public |
- | private final static SparseArray< | + | |
- | private SparseArray< | + | |
- | private SensorManager sensorManager; | + | |
- | static { | + | |
- | // desirable sensors | + | |
- | SENSORS.append(Sensor.TYPE_ACCELEROMETER, | + | } catch (RemoteException e) { |
- | SENSORS.append(Sensor.TYPE_AMBIENT_TEMPERATURE, | + | Log.e(TAG, "cannot announce"); |
- | SENSORS.append(Sensor.TYPE_GRAVITY, | + | |
- | SENSORS.append(Sensor.TYPE_GYROSCOPE, | + | |
- | SENSORS.append(Sensor.TYPE_LIGHT, | + | |
- | SENSORS.append(Sensor.TYPE_LINEAR_ACCELERATION, | + | |
- | SENSORS.append(Sensor.TYPE_MAGNETIC_FIELD, | + | |
- | SENSORS.append(Sensor.TYPE_PRESSURE, | + | |
- | SENSORS.append(Sensor.TYPE_PROXIMITY, | + | |
- | SENSORS.append(Sensor.TYPE_RELATIVE_HUMIDITY, | + | |
- | SENSORS.append(Sensor.TYPE_ROTATION_VECTOR, | + | |
- | } | + | |
- | + | ||
- | public | + | |
- | | + | |
- | Bundle serviceDescriptor; | + | |
- | + | ||
- | sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); | + | |
- | + | ||
- | // get device id | + | |
- | TelephonyManager telephonyManager | + | |
- | | + | |
- | + | ||
- | // for each desirable sensor | + | |
- | for (int i = 0; i < SENSORS.size(); | + | |
- | | + | |
- | String sensorName = SENSORS.get(sensorType); | + | |
- | + | ||
- | // probe sensor and add to the list | + | |
- | sensor = sensorManager.getDefaultSensor(sensorType); | + | |
- | if (sensor != null) { | + | |
- | serviceDescriptor = new Bundle(); | + | |
- | serviceDescriptor.putString(" | + | |
- | serviceDescriptor.putString(" | + | |
- | serviceDescriptor.putString(" | + | |
- | serviceDescriptor.putString(" | + | |
- | serviceDescriptor.putString(" | + | |
- | serviceDescriptor.putString(" | + | |
- | SERVICE_DESCRIPTORS.put(sensorType, | + | |
- | } | + | |
} | } | ||
} | } | ||
</ | </ | ||
- | We called the method sensorsDescriptors.initialize(middleware) in the SensorsService class when we implemented | + | The createSensorDescriptors() creates a descriptor for the accelerometer sensor present on the device using the **id** taken directly from the database (for sake of consistency with the GiraffPlus ecosystem we choose |
<code java> | <code java> | ||
- | public | + | private |
+ | Sensor sensor; | ||
try { | try { | ||
- | announce(middleware); | + | // Gets all the local sensors in a JSON array |
- | register(); | + | JSONArray localSensors = new JSONArray |
- | } catch (RemoteException e) { | + | |
- | Log.e(TAG, | + | // for each desirable sensor |
+ | for (int i = 0; i < sensors.size(); i++) | ||
+ | { | ||
+ | int sensorCode = sensors.keyAt (i); | ||
+ | String sensorType = sensors.get (sensorCode); | ||
+ | |||
+ | // probe sensor and add to the list | ||
+ | sensor = sensorManager.getDefaultSensor (sensorCode); | ||
+ | if (sensor != null) { | ||
+ | JSONObject sensDesc = null; | ||
+ | |||
+ | Log.d(TAG, " | ||
+ | |||
+ | for (int j = 0; j < localSensors.length (); j++) | ||
+ | if (((String)((JSONObject) localSensors.get (j)).get (" | ||
+ | sensDesc = (JSONObject) localSensors.get (j); | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | if (sensDesc != null) { // The sensor descriptor exists in the database | ||
+ | Bundle serviceDescriptor = new Bundle (); | ||
+ | serviceDescriptor.putString (" | ||
+ | serviceDescriptor.putString (" | ||
+ | serviceDescriptor.putString (" | ||
+ | serviceDescriptor.putString (" | ||
+ | serviceDescriptor.putString (" | ||
+ | serviceDescriptor.putString (" | ||
+ | |||
+ | JSONArray params = sensDesc.getJSONArray (" | ||
+ | Bundle[] messageFormat = new Bundle[params.length ()]; | ||
+ | for (int j = 0; j < params.length (); j++) { | ||
+ | messageFormat[j] = new Bundle (); | ||
+ | messageFormat[j].putString (" | ||
+ | messageFormat[j].putString (" | ||
+ | } | ||
+ | |||
+ | serviceDescriptor.putParcelableArray (" | ||
+ | serviceDescriptor.putParcelableArray (" | ||
+ | serviceDescriptors.put(sensorCode, | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | Log.e (TAG, | ||
} | } | ||
} | } | ||
Line 249: | Line 283: | ||
Bundle serviceDescriptor = SERVICE_DESCRIPTORS.get(SERVICE_DESCRIPTORS.keyAt(i)).second; | Bundle serviceDescriptor = SERVICE_DESCRIPTORS.get(SERVICE_DESCRIPTORS.keyAt(i)).second; | ||
// CALL TO THE MIDDLEWARE API - ANNOUNCE | // CALL TO THE MIDDLEWARE API - ANNOUNCE | ||
- | middleware.announce(serviceDescriptor, | + | middleware.announce(serviceDescriptor, |
+ | { | ||
+ | @Override | ||
+ | public void success () throws RemoteException { | ||
+ | Log.d (TAG, " | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void error (Bundle info) throws RemoteException { | ||
+ | Log.e (TAG, " | ||
+ | } | ||
+ | }); | ||
} | } | ||
} | } | ||
Line 276: | Line 321: | ||
payload.put(" | payload.put(" | ||
payload.put(" | payload.put(" | ||
- | JSONArray | + | JSONObject |
- | JSONObject array_value | + | values.put("Acc_X", event.values[0]); |
- | array_value.put("x", event.values[0]); | + | values.put("Acc_Y", event.values[1]); |
- | array_value.put("y", event.values[1]); | + | values.put("Acc_z", event.values[2]); |
- | array_value.put("z", event.values[2]); | + | |
- | values.put(array_value); | + | |
payload.put(" | payload.put(" | ||
} catch (JSONException e) { | } catch (JSONException e) { | ||
Line 289: | Line 332: | ||
// publish message | // publish message | ||
try { | try { | ||
- | middleware.publish(topic, | + | middleware.publish(topic, |
} catch (RemoteException e) { | } catch (RemoteException e) { | ||
Log.e(TAG, " | Log.e(TAG, " | ||
Line 314: | Line 357: | ||
for (int i = 0; i < SERVICE_DESCRIPTORS.size(); | for (int i = 0; i < SERVICE_DESCRIPTORS.size(); | ||
Bundle serviceDescriptor = SERVICE_DESCRIPTORS.get(SERVICE_DESCRIPTORS.keyAt(i)).second; | Bundle serviceDescriptor = SERVICE_DESCRIPTORS.get(SERVICE_DESCRIPTORS.keyAt(i)).second; | ||
- | middleware.remove(serviceDescriptor, | + | middleware.remove(serviceDescriptor, |
+ | { | ||
+ | @Override | ||
+ | public void success () throws RemoteException { | ||
+ | Log.d (TAG, " | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void error (Bundle info) throws RemoteException { | ||
+ | Log.e (TAG, " | ||
+ | } | ||
+ | }); | ||
} | } | ||
} | } |
giraffplus/android-middleware.1375805056.txt.gz · Last modified: 2013-08-06 16:04 by andrea