Sample
Sample Application. Open the Sample Application in Android Studio and take as reference.
Features:
- Setting up temperature, humidity and acceleration configuration
- Read temperature, humidity and acceleration data
- Verification of temperature, humidity and acceleration data
configuration
- Request permission in the Android manifest
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.NFC" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- Used for network positioning --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- Used to access GPS location --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- For improving GPS positioning speed --> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> <!-- Used to read the current status of the phone --> <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
- Reference library
repositories { flatDir { dirs 'libs' } } dependencies { implementation(name: 'vechain-verify-sdk', ext: 'aar') implementation 'com.squareup.okhttp3:okhttp:3.13.1' implementation 'com.squareup.retrofit2:converter-gson:2.4.0' implementation 'com.squareup.retrofit2:retrofit:2.4.0' implementation 'com.squareup.retrofit2:adapter-rxjava2:2.4.0' implementation 'io.reactivex.rxjava2:rxandroid:2.0.2' implementation 'io.reactivex.rxjava2:rxjava:2.1.16' implementation 'com.google.android.gms:play-services-location:16.0.0' implementation 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.5@aar' }
- Location function configuration
When setting the working configuration of the chip and reading the temperature in the chip, it is necessary to collect the current position information. It uses Baidu Location SDK function, which requires developers to go to Baidu to register for AppKey.(Click to register)
Put the APPKey to AdroidManifest. xml,Click on Baidu for Reference
<!--baidu location--> <meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="yourBaiduAppKey"></meta-data> <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"></service>
SDK initialization
Inherit the Android. app. Application class to implement the following methods:
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
String verifyID = "verifyId is Project ID that you get from VeChain Toolchain Portal/Developer Center";
VeChainSensorSDK.init(verifyID);
}
SDK Registration
To invoke the SDK function in your app, you only need to register once in the app to get the SDK function. On the Activity page that needs to be registered, the call method is as follows:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VeChainSensorSDK.onCreate(this);
registerSDK();
}
private void registerSDK() {
startLoading();
OnRegisterNotifier notifier = new OnRegisterNotifier() {
@Override
public void onRegisterResult(int resultCode, String token) {
closeLoading();
}
};
VeChainSensorSDK.register(notifier, new MySignAction());
}
MySignAction is the implementation of the interface SignAction. The purpose is to sign the incoming pair of challengeId and notifier. onSignResult (signInfo) to notify SDK to continue the registration process. The SDK registration successfully returns the token value, and the failed token is null.
public class MySignAction implements SignAction {
/*
MySignAction is the implementation of the interface SignAction. The purpose is to sign the incoming pair of challengeId and notifier. onSignResult (signInfo) to notify SDK to continue the registration process.
*/
@Override
public void sign(Context context, String challengeId, SignNotifier signNotifier) {
String certString = "Your certificate string";
String certKeyString = "Your certificate key";
String signature = CertUtils.getSignInfo(certString, certKeyString, challengeId);
//More Detail about how to sign the challengeId and get signature, refers to Appendix A: FAQ - How to Sign ChallengeID
signNotifier.onSignResult(signature, "");
}
}
SDK Life Cycle Management
On the Activity page that requires SDK functionality:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pagelayout);
//create
VeChainSensorSDK.onCreate(this);
}
@Override
protected void onResume() {
super.onResume();
VeChainSensorSDK.onResume(this]);
}
@Override
protected void onPause() {
super.onPause();
VeChainSensorSDK.onPause();
}
In this way, the SDK's life cycle is consistent with that of this activity.
If you want to trigger events when putting th chip close to the mobile phone NFC module, you can achieve the following:
@Override
protected void onResume() {
super.onResume();
VeChainSensorSDK.onResume(this,new OnTagListener() {
@Override
public void onTagDiscovered(Tag tag) {
//Detection of NFC module with chip close to mobile phone
//The current method is a non-main thread
}
});
}
Read chip version
VeChainSensorSDK.readSensorVersion(new SensorVersionNotifier() {
@Override
public void onReadSensorVersion(SensorVersion version, int errorCode, String errorMessage) {
if (version != null) {
//success
} else {
//fail
}
}
});
public class SensorVersion {
private boolean isSupportTemp;//the chip whether or not supports temperature acquisition
private boolean isSupportHumility;//the chip whether or not supports humility acquisition
private boolean isSupportAccelerate;//the chip whether or not supports accelerate acquisition
private String firmwareVersion;//the chip firmware version
private String hardwareVersion;//the chip hardware version
}
Read UID,VID,saltNO, battery, remainingRunTime
VeChainSensorSDK.readSharedZone(new ShareZoneNotifier() {
@Override
public void onReadShareZone(ShareZone shareZone, String error) {
if (shareZone != null) {
//success
} else {
//fail
}
}
});
Read configuration information of chip operation
VeChainSensorSDK.readConfig(new ConfigNotifier() {
@Override
public void onReadConfig(Config config, String error) {
if (config != null) {
//success
} else {
//fail
}
}
});
Get write chip configuration information verification code
The configuration information of temperature, humidity or acceleration should be set before the chip works to ensure that it is the certain chip and verified by the verification code. Read the verification code as follows:
VeChainSensorSDK.requestVerifyCode(new VerifyCodeNotifier() {
@Override
public void onReadVerifyCode(String verifyCode, int errorCode,
String errorMessage){
if (!TextUtils.isEmpty(verifyCode)) {
//success
}else{
//fail
}
}
});
Setting up Chip Work Configuration
SettingParam.Builder builder = new SettingParam.Builder();
builder.setVerifyCode(verifyCode).setAccount(account)
.setTempEnable(isTempEnable)
.setInterval(interval)
.setStartDelay(startDelay)
.setRunningTime(runningTime)
.setValidMinimum(validMinimum)
.setValidMaximum(validMaximum)
.setHumidityEnable(isHumidityEnable)
.setHumidityInterval(humidityInterval)
.setHumidityStartDelay(humidityStartDelay)
.setHumidityRunningTime(humidityRunningTime)
.setAccelerateEnable(isAccelerateEnable)
.setAccelerateType(AccelerateType.values()[acTriggerType]);
SettingParam param = builder.build();
VeChainSensorSDK.writeConfig(SettingParam params, new SetConfigNotifier() {
@Override
public void onResult(int status, String errorMessage, long estimateRuntime) {
if (status == ResultCode.OK) {
//success
} else {
//fail
}
}
});
isTempEnable: Whether the temperature acquisition function is on.
interval:Temperature acquisition interval (minute), greater than or equal to 1.
startDelay:Delay time (minute), greater than or equal to 0.
runningTime:Running time (minute), 0 means infinity.
validMinimum:Minimum Temperature (°c), Accurate 1-digit decimal, not lower than -40.
validMaximum:Maximum Temperature (°c), Accurate 1-digit decimal, not higher than 85.
isHumidityEnable: Whether the humidity acquisition function is on.
humidityInterval: Humidity acquisition interval (minute), greater than or equal to 1.
humidityStartDelay: Delay time (minute), greater than or equal to 0.
humidityRunningTime: Running time (minute), 0 to unlimited time.
isAccelerateEnable: Whether the Acceleration acquisition function is on.
acTriggerType: this value is 0 or 1;The type of mapping is AccelerateType.TRIGGERTYPEROCK or AccelerateType.TRIGGERTYPEROLL.
Read the collected temperature, humidity and acceleration data
VeChainSensorSDK.readSensorData(new ReadSensorDataNotifier() {
@Override
public void onReadSensorData(SensorData sensorData, String error) {
if (sensorData != null) {
//data Reading Successfully
} else {
//read failure
}
}
});
SensorData description
class SensorData {
//Temperature data
private boolean isTempEnable; //Is the temperature measurement on
private List<Float> tempData;//Temperature data
private String tempSignature;//Temperature signature information (hexadecimal string)
private int tempInterval;//Unit minute
private int tempStartDelay;//Unit minute
private int tempRunningTime;//Unit minute
private int tempValidMinimum;//Actual value*10
private int tempValidMaximum;//Actual value*10
private int accuracy;//Temperature data storage accuracy
private int tempGroupSize; //Number of compressions per group
//Humidity data
private boolean isHumidityEnable; //Is the humidity measurement on
private List<Integer> humidityData;
private String humiditySignature;//Hexadecimal string
private int humidityInterval; //Humidity measurement interval,Unit minute
private int humidityStartDelay; //Unit minute
private int humidityRunningTime; //Unit minute
private int humidityGroupSize; //Number of compressions per group
//Accelerate data
private boolean isAccelerateEnable; //Is the accelerate measurement on
private List<Accelerate> accelerateData;
private String accelerateSignature;//Hexadecimal string
private int accGroupSize; //Number of compressions per group
//Base Information
private String uid;//Hexadecimal string
private String vid;//Hexadecimal string
private String saltNo;//Hexadecimal string
private int configTime;//Configuration time
private int configLongitude;//(Actual value*1000000);
private int configLatitude;//(Actual value*1000000);
private int configLocationAccuracy;//Positioning accuracy,Unit meters
//Position information when reading temperature
private int curLongitude;//Actual value*1000000;
private int curLatitude;//Actual value*1000000;
private int curLocationAccuracy;//Positioning accuracy,Unit meters
private int curTime;//Unit seconds
//9bytes Abnormal information
private String restPowerInfo;//Hexadecimal string
}
Verification of temperature data
Data are obtained from some channels, and SDK is used to verify whether the data is true and reliable, or whether the data has been modified.If the data is not true or modified, the validation return fails
VerifyBean bean = new VerifyBean();
bean.setData(sensorData.getTempData());
bean.setSignature(sensorData.getTempSignature());
bean.setGroupSize(sensorData.getTempGroupSize());
bean.setUid(sensorData.getUid());
bean.setVid(sensorData.getVid());
bean.setSaltNo(sensorData.getSaltNo());
String configString = sensorData.getConfig();
bean.setConfig(configString);
VeChainSensorSDK.verifySensorData(bean, (code, pass, message) -> {
closeLoading();
if (code == ResultCode.OK && pass) {
//check success
} else {
//check fail
}
});
Verification of humidity data
VerifyBean bean = new VerifyBean();
bean.setData(sensorData.getHumidityData());
bean.setSignature(sensorData.getHumiditySignature());
bean.setGroupSize(sensorData.getHumidityGroupSize());
bean.setUid(sensorData.getUid());
bean.setVid(sensorData.getVid());
bean.setSaltNo(sensorData.getSaltNo());
String configString = sensorData.getConfig();
bean.setConfig(configString);
VeChainSensorSDK.verifySensorData(bean, (code, pass, message) -> {
closeLoading();
if (code == ResultCode.OK && pass) {
//check success
} else {
//check fail
}
});
Verification of accelerate data
VerifyBean bean = new VerifyBean();
bean.setData(sensorData.getAccelerates());
bean.setSignature(sensorData.getAccelerateSignature());
bean.setGroupSize(sensorData.getAccGroupSize());
bean.setUid(sensorData.getUid());
bean.setVid(sensorData.getVid());
bean.setSaltNo(sensorData.getSaltNo());
String configString = sensorData.getConfig();
bean.setConfig(configString);
VeChainSensorSDK.verifySensorData(bean, (code, pass, message) -> {
closeLoading();
if (code == ResultCode.OK && pass) {
//check success
} else {
//check fail
}
});
Comments
0 comments
Please sign in to leave a comment.