SDK Integration Steps
1. Initialize QNSDK
Use QNBleApi.initSdk for initialization. For details, refer to the Development Process.
2. Set Scan Callback Listener for Height and Weight Scale
Before starting Bluetooth scanning, set up the listener via QNBleApi.setBleDeviceDiscoveryListener. Call this method once, and set it to null
/nil
when scanning is no longer needed.
Android Example:
QNBleApi mQNBleApi = QNBleApi.getInstance(context);
mQNBleApi.setBleDeviceDiscoveryListener(new QNBleDeviceDiscoveryListener() {
@Override
public void onDeviceDiscover(QNBleDevice device) {
//Height scale devices found during scanning will be called back here
}
...
});
iOS Example:
// Set delegate
QNBleApi *bleApi = [QNBleApi sharedBleApi];
bleApi.discoveryListener = self;
bleApi.connectionChangeListener = self;
bleApi.dataListener = self;
bleApi.bleStateListener = self;
3. Start Bluetooth Scanning
Ensure Bluetooth is enabled.
- Android: Additionally verify
Location Permission
andLocation Service
are enabled. - iOS: Confirm Bluetooth usage permission is granted (required since iOS 13).
Android Notes:
- For apps with
targetSdkVersion >= 23
, location permission is required for BLE scanning (details).- Location Service is not mandatory but may be required on some devices.
iOS Note:
- System Bluetooth usage permission must be authorized.
Use QNBleApi.startBleDeviceDiscovery to start scanning. Scanned devices are returned via QNBleDeviceDiscoveryListener.
Configure scan settings via QNConfig.
Android Example:
QNBleApi.getInstance(context).startBleDeviceDiscovery(new QNResultCallback() {
@Override
public void onResult(int code, String msg) {
//This method does not callback Bluetooth devices, but indicates whether scanning started successfully
if (code != CheckStatus.OK.getCode()) {
Toast.makeText(context, code+":"+msg).show();
}
}
});
iOS Example:
//Start scanning
[[QNBleApi sharedBleApi] startBleDeviceDiscovery:^(NSError *error) {
//This callback indicates whether the scanning method started successfully
if (error) {
NSLog([NSString stringWithFormat:@"Failed to start scanning method, reason: %@",error]);
}
}];
4. Set Connection State Listener for Height and Weight Scale
Set Bluetooth connection state listener via QNBleApi.setBleConnectionChangeListener to receive connection state changes.
Android Example:
QNBleApi.getInstance(this).setBleConnectionChangeListener(new QNBleConnectionChangeListener() {
//Connecting
@Override
public void onConnecting(QNBleDevice device) {
}
//Connected, note that other Bluetooth operations cannot be performed at this time
@Override
public void onConnected(QNBleDevice device) {
}
//Service discovered
@Override
public void onServiceSearchComplete(QNBleDevice device) {
}
//Disconnecting, will be called back immediately when disconnect is called
@Override
public void onDisconnecting(QNBleDevice device) {
}
// Disconnected, callback after disconnection
@Override
public void onDisconnected(QNBleDevice device) {
}
//Connection error occurred, refer to the appendix for error codes
@Override
public void onConnectError(QNBleDevice device, int errorCode) {
}
//Can execute other Bluetooth operation API methods
@Override
public void onStartInteracting(QNBleDevice device) {
}
});
iOS Example:
- (void)onConnecting:(QNBleDevice *)device {
}
- (void)onConnected:(QNBleDevice *)device {
}
- (void)onServiceSearchComplete:(QNBleDevice *)device {
}
- (void)onDisconnecting:(QNBleDevice *)device {
}
- (void)onDisconnected:(QNBleDevice *)device {
}
- (void)onConnectError:(QNBleDevice *)device error:(NSError *)error {
}
5. Set Data Measurement Listener for Height and Weight Scale
Set measurement data listener via QNBleApi.setDataListener,to receive measurement states and data.
Android Example:
QNBleApi.getInstance(this).setDataListener(new QNScaleDataListener() {
//Barcode scanner gets scanning results
@Override
public void onGetBarCode(String devMac, String barCode) {
}
//Barcode scanner failed to get scanning results
@Override
public void onGetBarCodeFail(String devMac) {
}
//Barcode scanner connection status change
@Override
public void onGetBarCodeGunState(String devMac, boolean isConnected) {
}
//Real-time weight data callback
@Override
public void onGetUnsteadyWeight(QNBleDevice device, double weight) {
}
//Measurement result data callback
@Override
public void onGetScaleData(QNBleDevice device, QNScaleData data) {
}
//Stored data callback
@Override
public void onGetStoredScale(QNBleDevice device, List<QNScaleStoreData> storedDataList) {
}
...
});
iOS Example:
- (void)onGetUnsteadyWeight:(QNBleDevice *)device weight:(double)weight {
}
- (void)onGetScaleData:(QNBleDevice *)device data:(QNScaleData *)scaleData {
}
- (void)onGetStoredScale:(QNBleDevice *)device data:(NSArray<QNScaleStoreData *> *)storedDataList {
}
/// Get barcode
- (void)onGetBarCode:(NSString *)barCode mac:(NSString *)mac {
self.unstableWeightLabel.text = [NSString stringWithFormat:@"Barcode content: %@",barCode];
}
- (void)onGetBarCodeFail:(NSString *)mac {
self.unstableWeightLabel.text = [NSString stringWithFormat:@"Failed to get barcode: %@",barCode];
}
- (void)onGetBarCodeGunState:(BOOL)isConnect mac:(NSString *)mac {
if (isConnect) {
self.unstableWeightLabel.text = @"Device connected to barcode scanner";
}else {
self.unstableWeightLabel.text = @"Barcode scanner disconnected";
}
}
6. Connect to Height and Weight Scale
After receiving a device callback, initiate connection via QNBleApi.connectHeightScaleDevice if it matches your logic.
Android Example:
QNBleApi.getInstance(this).connectHeightScaleDevice(mBleDevice, deviceConfig, (code, msg) -> {
//Indicates whether the method executed successfully
if (code != CheckStatus.OK.getCode()) {
Toast.makeText(context, code+":"+msg).show();
}
})
iOS Example:
QNHeightDeviceConfig *config = [[QNHeightDeviceConfig alloc]init];
QNUser *user = [[QNUser alloc]init];
user.gender = @"female";
user.birthday = [NSDate dateWithString:@"1995-01-01" format:@"yyyy-MM-dd"];
config.curUser = user;
[_bleApi connectHeightScaleDevice:device config:config callback:^(NSError *error) {
if (error) {
NSLog(@"%@", error.localizedDescription);
}
}];
Wi-Fi Configuration
To configure Wi-Fi, pass a QNWiFiConfig
7. Disconnect Device
Actively disconnect viaQNBleApi.disconnectDevice
Android Example:
QNBleApi.getInstance(RulerActivity.this).disconnectDevice(mac, new QNResultCallback() {
@Override
public void onResult(int code, String msg) {
}
});
iOS Example:
[_bleApi disconnectDeviceWithMac:nil callback:^(NSError *error) {
}];
// or
[_bleApi disconnectDeviceWithMac:device.mac callback:^(NSError *error) {
}];