1. Plugin Access Steps
1.1 Project Introduction Plugin
1.1.1 dependencyResolutionManagement
- Under the root directory of the projectsetting.gradleaddjitpacksupport
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.PREFER_SETTINGS)
repositories {
//Declare the repository URL
maven { url 'https://jitpack.io' }
//Other repository configurations
...
}
}
- Add dependencies tobuild.gradleunder the App directory
dependencies {
//Declare the dependency. Replace X.Y.Z with the actual version number.
implementation("com.github.YolandaQingniu:qnscalesdkX:X.Y.Z")
//Other repository configurations
...
}
1.1.2 allprojects
In the root directory of the project build.gradle add jitpack support
allprojects { repositories { maven { url 'https://jitpack.io' } /Other repository configurations ... } }Add dependencies tobuild.gradleunder the App directory
dependencies { //Declare the dependency. Replace X.Y.Z with the actual version number. implementation("com.github.YolandaQingniu:qnscalesdkX:X.Y.Z") //Other repository configurations ... }
1.2 Instructions for Using Bluetooth Permissions in Configuration Item
If the targetSdk of the app is >30 and the system version of the phone is Android 12 or higher, the following permissions are required
android.permission.BLUETOOTH_ADVERTISE
android.permission.BLUETOOTH_SCAN
android.permission.BLUETOOTH_CONNECT
Otherwise, the following permissions are required
android.permission.BLUETOOTH
android.permission.BLUETOOTH_ADMIN
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION
1.3 Obfuscation Configuration
-keep class com.qingniu.scale.model.BleScaleData{*;}
-keep class com.jieli.** {*;}
1.4 Initial Plugin
//Get the path of the authorization file in the project
String encryptPath = "file:///android_asset/123456789.qn";
//Initialize the plugin
QNBleApi bleApi = QNBleApi.getInstance(this);
//Request plugin authorization
bleApi.initSdk("123456789", encryptPath, new QNResultCallback() {
@Override
public void onResult(int code, String msg) {
}
});
1.5 Listen for system Bluetooth state changes (QNBleStateListener)
/*
enum QNBLEState {
Unknown,
setting,
Unsupported,
Unauthiorized,
PoweredOff,
PoweredOn;
*/
bleApi.setBleStateListener(new QNBleStateListener() {
@Override
public void onBleSystemState(QNBLEState qnbleState) {
}
});
1.6 Listen for changes in the scanning status (QNBleDeviceDiscoveryListener)
bleApi.setBleDeviceDiscoveryListener(new QNBleDeviceDiscoveryListener() {
@Override
public void onDeviceDiscover(QNBleDevice device) {
//Callback triggered when a device is discovered during scanning. Only supported devices are reported here.
}
@Override
public void onStartScan() {
//Callback triggered when scanning starts.
}
@Override
public void onStopScan() {
//Callback triggered when scanning stops.
}
@Override
public void onScanFail(int code) {
//Callback triggered when scanning fails to start
}
});
1.7 Listen for device connection status changes (QNBleConnectionChangeListener)
bleApi.setBleConnectionChangeListener(new QNBleConnectionChangeListener() {
@Override
public void onConnecting(QNBleDevice device) {
//Callback triggered when the device is connecting
}
@Override
public void onConnected(QNBleDevice device) {
//Callback triggered when the device has successfully connected
}
@Override
public void onServiceSearchComplete(QNBleDevice device) {
//Callback triggered when the device's communication services have been discovered. Usually no additional logic is required here
}
@Override
public void onDisconnecting(QNBleDevice device) {
//Callback triggered when the device is disconnecting
}
@Override
public void onDisconnected(QNBleDevice device) {
//Callback triggered when the device has disconnected
}
@Override
public void onConnectError(QNBleDevice device, int errorCode) {
//Callback triggered when the device connection fails, returning the error information
}
@Override
public void onStartInteracting(QNBleDevice device) {
//Callback triggered when the device is ready for interaction. At this point, device-specific operation commands can be sent
}
});
1.8 Listen for device data interaction (QNUserScaleDataListener)
bleApi.setDataListener(new QNUserScaleDataListener() {
@Override
public void registerUserComplete(QNBleDevice device, QNUser user) {
// Called when a user is successfully registered on the scale.
// Returns the user slot assigned by the device.
// The user slot assigned by the scale during user registration.
// The app should associate and save this slot with the user ID and device MAC.
// The slot index will be used for accessing the user's measurements when the user reconnects to this device.
int index = user.index;
}
@Override
public void onGetUnsteadyWeight(QNBleDevice device, double weight) {
// Callback for real-time weight during the measurement process
}
@Override
public void onGetScaleData(QNBleDevice device, QNScaleData data) {
// Callback triggered when the device completes a measurement
Date measureDate = data.getMeasureTime();//Measured time
double weight = data.getWeight();//Measured weight
List<QNScaleItemData> allTarget = data.getAllItem();
for (QNScaleItemData item: allTarget) {
item.getType() //Indicator type, see QNScaleType
item.getValue() //Indicator value
}
}
@Override
public void onGetStoredScale(QNBleDevice device, List<QNScaleStoreData> storedDataList) {
// Callback for currently accessed user's stored data and unknown stored data
// storedDataList List of stored data. Use the `isDataComplete` property in `QNScaleStoreData` to determine if the data is from an unknown measurement:
// false indicates unknown measurement data, true indicates known measurement data.
//For unknown measurement data, you can notify the relevant user and let them choose whether this data belongs to them.
ArrayList<QNScaleStoreData> unknowStoreDataList = new ArrayList<>();
for (QNScaleStoreData storeData : storedDataList) {
if(!storeData.isDataComplete()){
unknowStoreDataList.add(storeData);
} else {
//For known stored data of the current user, the data can be directly attributed to that user.
QNScaleData scaleData = storeData.generateScaleData();
for (QNScaleItemData item: scaleData.getAllItem()) {
item.type //Indicator type, see QNScaleType
item.value //Indicator value
}
}
}
}
@Override
public void onScaleStateChange(QNBleDevice device, int status) {
//Scale interaction state changes
}
@Override
public void onScaleEventChange(QNBleDevice device, int scaleEvent) {
//Scale-side behavior state changes
}
@Override
public String getLastDataHmac(QNBleDevice qnBleDevice, QNUser qnUser) {
//Write back a valid measurement HMAC (i.e., a body fat scale data HMAC greater than 0) to a specified user slot on the device.
//This can help improve the stability of the user's bioelectrical impedance measurement.
//If stability reinforcement is not required, you can simply return nil
//Obtain the HMAC from the most recent measurement in the user's measurement history where the body fat percentage is greater than 0.
//The HMAC is generated from the measurement data, as defined in the QNScaleData object.
String hmac = lastVailDataHmac;
return hmac;
}
});
1.9 Scan surrounding devices
bleApi.startBleDeviceDiscovery(new QNResultCallback() {
@Override
public void onResult(int code, String msg) {
}
});
1.10 Connect Device
//It is recommended to stop scanning first
bleApi.stopBleDeviceDiscovery(new QNResultCallback() {
@Override
public void onResult(int code, String msg) {
}
});
//Set the scale unit
QNConfig sdkConfig = bleApi.getConfig()
sdkConfig.unit = 0; //0-kg,1-lb,2-斤,3-st:lb,4-st,
sdkConfig.save();
QNUserScaleConfig config = new QNUserScaleConfig();
// List of registered scale users. During this connection, users on the scale
// that are not included in this array will be deleted from the device.
ArrayList<QNUser> deviceUserList = new ArrayList<>();
config.setUserlist(deviceUserList);
// Scale user for the current connection
mQNBleApi.buildUser(mUser.getUserId(),
String userId = "";
//User height in centimeters (cm)
int height = 170;
String gender = "male"; //"male"、"female"
//User birthday
Date birthday = new Date(631199317000L);
// User slot. This value is returned by the scale when registering a user on the device (see the `registerUserComplete` callback).
// If this connection is for a new user on the device and the user needs to be registered, this property does not need to be set.
int index = 1;
QNUser user = bleApi.buildUser(userId, height, gender, birthday, 0, UserShape.SHAPE_NONE, UserGoal.GOAL_NONE, 0, index, 0,
new QNResultCallback() {
@Override
public void onResult(int code, String msg) {
}
});;
config.setCurUser(user);
// Whether to use a guest user (i.e., no registration on the scale; the scale will not store this user's information).
// Note: This field is mutually exclusive with `user.index`; `isVisitor` takes priority.
config.setVisitor(false)
bleApi.connectUserScaleDevice(device, config, new QNResultCallback() {
@Override
public void onResult(int code, String msg) {
}
});
1.11 Disconnect the device
// If needed, you can proactively call the method to disconnect the device
bleApi.disconnectDevice(bleApi, new QNResultCallback() {
@Override
public void onResult(int code, String msg) {
}
});
1.12 Unknown stored data calculation
//User information for unknown stored data ownership
String userId = "";
//User height in centimeters (cm)
int height = 170;
String gender = "male"; //"male"、"female"
//User birthday
Date birthday = new Date(631199317000L);
QNUser user = mQNBleApi.buildUser(userId, height, gender, birthday, new QNResultCallback() {
@Override
public void onResult(int code, String msg) {
}
});
// hmac: The HMAC of the current unknown measurement data, i.e., the `hmac` in QNScaleStoreData.
// lastHmac: The HMAC of the user's previous measurement with body fat > 0 (8-electrode measurement), i.e., the `hmac` in QNScaleData.
//If the user has no previous measurement with body fat > 0, this can be set to nil.
QNScaleData scaleData = mQNBleApi.calculateScaleDataByHmac(user, hmac,lastHmac);
2. API
2.1 QNBleApi
Plugin instance object, used to call plugin-related APIs
2.1.1 getInstance
Description:Constructs a QNBleApi object, which is the only function for initializing the plugin
Return Value:QNBleApi
2.1.2 getConfig
Description:Get plugin configuration information
Return value:
QNConfig
duration: Scan timeout period, default is 0, which means no scan timeout will occur; if this value is set, the scan will automatically stop within the specified time after starting, and the call to the scan function needs to be maintained manually
unit: Sets the user unit at the scale end, but when initiating device connection, it will synchronize the unit settings to the scale end
- For details, please refer to classQNUnit
2.1.3 initSdk
Description: Initialize the plugin by passing the configuration file path, and call
Parameter:
appId: String: APPID authorized by YKB
dataFile: String: The relative path of the configuration file in the project
callback:QNResultCallback: Callback for function call result
Return value:
None
2.1.4 getCurSystemBleState
Description:Get the current system Bluetooth status
Return value:
- int:QNBLEState.Unknown.ordinal()、QNBLEState.setting.ordinal()、QNBLEState.Unsupported.ordinal()、QNBLEState.Unauthorized.ordinal()、QNBLEState.PoweredOff.ordinal()、QNBLEState.PoweredOn.ordinal()
2.1.5 startBleDeviceDiscovery:
Description:Start scanning for surrounding devices
Parameter:
- callback:QNResultCallback: Callback for function call result
Return value:
None
2.1.6 stopBleDeviceDiscorvery:
Description:Stop scanning for nearby devices
Parameter:
- callback:QNResultCallback: Callback for function call result
Return value:
None
2.1.7 connectUserScaleDevice
Description: Initiate a connection to a specified device
Parameter:
device: QNBleDevice: The scanned device, obtained from the device object in the callback of the onDeviceDiscover function in QNBleDeviceDiscoveryListener
config:QNUserScaleConfig: Configuration information for this connection
callback: Callback for function call result
Return value:
None
2.1.8 disconnectDevice:
Description:Disconnect the currently connected device
Parameter:
device: QNBleDevice: The currently connected device object, which can be left unassigned
callback:QNResultCallback: Callback for function call result
Return value:
None
2.1.9 calculateScaleDataByHmac
Description:Recalculate measurement data, which can be used for calculating unknown stored data, and can also be used for recalculating known measurement data that needs to be changed to data measured by other users
Parameter:
user:QNUser: User information to which this measurement data belongs
hmac:String: The hmac of the current measurement data
lastEightHmac: String: The previous eight-electrode measurement data of the user to which this measurement data belongs, where the body fat percentage is greater than 0; if none, assign nil
Return value:
QNScaleData
2.2 QNBleStateListener
Callback listener for system Bluetooth status changes
2.2.1 onBleSystemState
Description: When the system Bluetooth changes, this function is called back
Parameter:
- state:QNBLEState
Return value:
None
2.3 QNBleDeviceDiscoveryListener
2.3.1 onStartScan
Description: This function is called back when starting a scan, such as when calling the startBleDeviceDiscovery function
Parameter:
None
Return value:
None
2.3.2 onStopScan
Description: This function is called back when scanning stops, such as when calling the stopBleDeviceDiscorvery function
Parameter:
None
Return value:
None
2.3.3 onDeviceDiscover
Description:Callback when surrounding devices are discovered. This function only returns YKB devices.
Parameter:
- device: QNBleDevice: Discovered device
Return value:
None
2.4 QNBleConnectionChangeListener
2.4.1 onConnecting
Description:Callback when the device is connecting
Parameter:
- device:QNBleDevice: device object
Return value:
None
2.4.2 onConnected
Description:Callback after the device is successfully connected
Parameter:
- device:QNBleDevice: device object
Return value:
None
2.4.3 onDisconnecting
Description:Callback when the device is disconnecting
Parameter:
- device:QNBleDevice: device object
Return value:
None
2.4.4 onDisconnected
Description:Callback when the device is already disconnected
Parameter:
- device:QNBleDevice: device object
Return value:
None
2.4.5 onConnectError:error:
Description:Callback when device connection is abnormal
Parameter:
device:QNBleDevice: device object
error:int: Connection error message
Return value:
None
2.4.6 onStartInteracting
Description:Device communication callback. After this function, some API operations of the device can be called, such as updating the user's basic weight, etc.
Parameter:
- device:QNBleDevice: device object
Return value:
None
2.5 QNUserScaleDataListener
2.5.1 onScaleStateChange
Description:Callback for device state changes, such as connection state, measurement state, etc.
Parameter:
device:QNBleDevice: device object
state:int: Device status, for details, please refer to the definition in classQNScaleStatus
Return value:
None
2.5.2 onScaleEventChange
Description:Device event callback, such as connection status, measurement status, etc.
Parameter:
device:QNBleDevice: device object
scaleEvent:int: Device event type, for details, please refer to the definition in classQNScaleEvent
Return value:
None
2.5.3 onGetBleVer
Description:Device firmware version information callback
Parameter:
device:QNBleDevice: device object
bleVer: int: Firmware version number
Return value:
None
2.5.4 onGetUnsteadyWeight
Description:Callback for device measurement of real-time body weight
Parameter:
device:QNBleDevice: device object
weight:double: current real-time weight, unit kg
Return value:
None
2.5.5 onGetScaleData
Description:Callback for the current Bluetooth connection measurement results
Parameter:
device:QNBleDevice: device object
scaleData:QNScaleData: Current measurement data
Return value:
None
2.5.6 onGetStoredScale
Description:Callback for the known stored data and unknown measured data of the currently accessed user of the device; the stored data of users in non-currently accessed slots will not be called back
Parameter:
device:QNBleDevice: device object
storedDataList: List\
: Stores user
Return value:
None