Guide to Adding a new Sensor Driver to OSH Android App
Prerequisites
- OSH Android Repository
- Java 17
- Android Studio
Step 1: Open the project in Android Studio
Before moving to Step 2: determine whether you are:
- Is the driver an existing driver from osh-addons or another addon-like driver repo or is it android specific?
- If it is android specific, is it a hardware driver or external device driver?
Hardware drivers are drivers that are built into the android device and are accessed through the android API whereas external drivers can be things connected through the android device through Bluetooth, USB, WiFi or other means.
Step 2: Creating a New Driver directory
- Right click on the
'sensorhub-android-template'
module and clickCopy
and paste the directory in the top level of the project. - Rename the project to 'sensorhub-android-sensorName'
- Navigate to the `sensorhub-android-app' build.gradle and include your dependency
/res/xml/values/prefs_sensor.xml
implementation project(':sensorhub-android-sensorName')
- Sync Gradle after modifying the
build.gradle
Step 3: Updating the new Sensor Driver
- Make the necessary modifications to the driver based on the sensor's requirements.
Step 4: Add Sensor to the Android Preferences
- Open the
prefs_sensor.xml
file in theres/xml
directory ofsensorhub-android-app
. - Add an entry to enable the sensor in the UI:
/res/xml/prefs_sensor.xml
<SwitchPreference
android:defaultValue="false"
android:key="sensor_enabled"
android:summary="Enable streaming of Sensor"
android:title="Sensor Data" />
<EditTextPreference
android:defaultValue="00:00:00:00:00:00"
android:inputType="text"
android:key="bluetooth_address"
android:singleLine="true"
android:title="Sensor Bluetooth Address" />
<MultiSelectListPreference
android:key="sensor_options"
android:title="Sensor Output Options"
android:summary="Options for pushing sensor data"
android:entries="@array/sos_option_list"
android:entryValues="@array/sos_option_values"
android:defaultValue="@array/sos_option_defaults" />
Step 5: Add the necessary information to the strings.xml
file
- In this file, you can add the information about the sensor, this may be more in depth depending on the complexity of your sensor.
- Open
res/values/strings.xml
- Add the sensors display name:
<string name="sensor_name">Sensor Name</string>
Step 6: Importing Sensor into MainActivity.java
- Import your sensor at the top of the file
- Update the
Sensors
enum to include the new sensor name
/src/org/sensorhub.android/MainActvity.java
enum Sensors{
//... rest of sensors
sensorName,
}
- Modify the
updateConfig
method to check if the sensor is enabled and configure it accordingly:
/src/org/sensorhub.android/MainActvity.java
protected void updateConfig(SharedPreferences prefs, String runName) {
// rest of code
enabled = prefs.getBoolean("sensor_enabled", false);
if (enabled) {
BleConfig bleConf = new BleConfig();
bleConf.id = "BLE";
bleConf.moduleClass = BleNetwork.class.getCanonicalName();
bleConf.androidContext = this.getApplicationContext();
bleConf.autoStart = true;(buil)
sensorhubConfig.add(bleConf);
SensorConfig config = new SensorConfig();
config.id = "SENSOR";
config.name = "Sensor [" + deviceName + "]";
config.autoStart = true;
config.networkID = bleConf.id;
config.btAddress = prefs.getString("sensor_bt_address", null);
config.androidContext = this.getApplicationContext();
sensorhubConfig.add(config);
}
}