Android Sensor SDK
Quick Start
1.Create a New Project
Android Studio Configuration
Adding the sensoro-sensor-kit.jar file under libs folder,then open the build.gradle file and add this to the dependencies, namely, compile files('libs/sensoro-sensor-kit.jar')
Eclipse Configuration
Adding sensoro-sensor-kit.jar file under libs folder, right click on properties and select Java build Path, and then add the sensoro-sensor-kit to the Library dependencies.
2.Android Manifest Instructions
Adding the following permissions and feature (all are mandatory)
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
Adding service, as below
<service android:name="com.sensoro.sensor.kit.SensoroDeviceService"></service>
<service android:name="com.sensoro.sensor.kit.IntentProcessorService"></service>
<service
android:name="com.sensoro.sensor.kit.update.service.DfuService"
android:exported="true" />
Adding build.gradle dependencies,as below
compile 'no.nordicsemi.android:dfu:1.0.0'
compile group: 'com.google.protobuf', name: 'protobuf-java', version: '2.5.0'
3.Sample Callback
1.Sdk Core function sample code
SensoroDeviceManager sensoroDeviceManager = SensoroDeviceManager.getInstance(this);
try {
sensoroDeviceManager.startService();
} catch (Exception e) {
e.printStackTrace();
}
sensoroDeviceManager.setSensoroDeviceListener(new SensoroDeviceListener<SensoroDevice>() {
@Override
public void onNewDevice(SensoroDevice sensoroDevice) {//callback invoked when a new device is discovered
}
@Override
public void onGoneDevice(SensoroDevice sensoroDevice) {//callback invoked when a device is missing
}
@Override
public void onUpdateDevices(final ArrayList<SensoroDevice> arrayList) {//callback invoked periodically to update the info of devices
}
});
Note: SensoroDeviceManager is a class for the device management of sensors and is responsible for device discovery, identifying missing devices and updating the device info. It is a singleton, and you can use getInstance method to get instances; SensoroDeviceListener is used to invoke callback for new device discovery, device messages, and device updates.
4. Transparent transmission
- Start mission
SensoroDevice sensoroDevice = this.getIntent().getParcelableExtra("sensoroDevice");
sensoroDeviceSession = new SensoroDeviceSession(this, sensoroDevice);
sensoroDeviceSession.startSession("password", new SensoroDeviceSession.ConnectionCallback() {
@Override
public void onConnectFailed(int i) {//Failed to connect sensor
}
@Override
public void onConnectSuccess() {//Connect sensor successfully
}
@Override
public void onNotify(byte[] bytes) {//Transparent data transfer callback
}
});
- Data input
byte []data = SensoroUtils.HexString2Bytes(str);
if (sensoroDeviceSession != null) {
sensoroDeviceSession.write(data, new
SensoroDeviceSession.WriteCallback() {
@Override
public void onWriteSuccess() {//Data written successfully
}
@Override
public void onWriteFailure(int i) {//Data write failed
}
});
}
5. Equipment upgrade instructions
Create an object
//parameter1:Context //parameter2:SensoroDevice object(from Scanning) mSensoroDeviceSession = new SensoroDeviceSession(this.getApplicationContext(), mSensoroDevice);
Start the upgrade
```
//parameter1:Upgrade file path //parameter2:password //parameter3:monitor mSensoroDeviceSession.startUpdate(path, "", new OnDeviceUpdateObserver() { @Override public void onEnteringDFU(String s, String s1, String s2) { loge("Entering DFU-->>" + s + ",s1 = " + s1 + ",s2 = " + s2); } @Override public void onUpdateCompleted(String s, String s1, String s2) { loge("update completed-->" + s + ",s1 = " + s1 + ",s2 = " + s2); } @Override public void onDFUTransfering(String s, int i, float v, float v1, int i1, int i2, String s1) { loge("onDFUTransfering==========s = " + s + ",i = " + i + ",v = " + v + ",v1 = " + v1 + ",i1 = " + i1 + ",i2 = " + i2 + ",s1 = " + s1); } @Override public void onUpdateValidating(String s, String s1) { loge("Inspection documents:onUpdateValidating=====" + s + "s1 = " + s1); } @Override public void onUpdateTimeout(int i, Object o, String s) { loge("outTime"); } @Override public void onDisconnecting() { loge("isconnect the device"); }
@Override
public void onFailed(String s, String s1, Throwable throwable) {
loge("Upgrade failed======" + s + ",s1 = " + s1 + ",msg = " + (throwable == null ? "e 为空" : throwable
.getMessage()));
}
});
```
Add life cycle method
/** * Add life cycle method onSessionResume!!! */ @Override protected void onResume() { super.onResume(); mSensoroDeviceSession.onSessionResume(); } /** * Add life cycle method onSessonPause!!! */ @Override protected void onPause() { super.onPause(); mSensoroDeviceSession.onSessonPause(); }
6. Sensor device object description
Description:SensoroDevice For sensor device objects, the following are the object properties
serialNumber ---String; // SN
macAddress---String; // MAC
hardwareVersion---String;//hardwareVersion
firmwareVersion---String;//firmwareVersion
batteryLevel---Integer;// batteryLevel
temperature---Float;// temperature
light----Float; // light
humidity---Integer;//humidity
accelerometerCount---Integer; // accelerometerCount
rssi---int;
customize----byte[];//customize data
drip---Integer;//drip
co---Float;//co
co2---Float;//co2
no2---Float;//no2
methane---Float;//methane
lpg---Float;liquefied petroleum gas
pm1---Float;
pm25---Float;//PM2.5
pm10---Float;
coverstatus---Integer;//Well cover status
level---Float;//Liquid level
Sample Code
Github code Suitable for Android studio
Log of Updates
Date | Version | Author | Content |
---|---|---|---|
2016-07-27 | 1.0 | Will | Initial Content |
2018-04-19 | 1.0 | ddong1031 | Added description of transparent transmission function, and sensor support, added upgrade module and new sensor support |