Ordinary Bluetooth scale
The ordinary Bluetooth scale is the most common scale sold by our company. Its Bluetooth name is usually: QN-Scale
orQN-Scale1
. There is no difference in the use of these two Bluetooth name scales.
The workflow of the device is approximately as follows:
One、initialization
Use QNBleApi.initSdk-to-initialize,-there-are-related-introductions-in-the-previous-article,-please-check
2. scanning
1. Set the Bluetooth scan callback monitor class
Before scanning, you need to set the listener class. The method is QNBleApi.setBleDeviceDiscoveryListener. This method only needs to be called once. When you do not need to scan, remember to set to null/nil
android example:
QNBleApi.getInstance(context).setBleDeviceDiscoveryListener(new QNBleDeviceDiscoveryListener() {
@Override
public void onDeviceDiscover(QNBleDevice device) {
//This method calls back the device object, which can be processed by the device object
}
@Override
public void onStartScan() {
//Back to start scanning
}
@Override
public void onStopScan() {
//return to the end of the scan
}
@Override
public void onScanFail(int code) {
//The callback for the scan failure will be returned with an error code. For details, please refer to the error code details page
}
@Override
public void onBroadcastDeviceDiscover(QNBleBroadcastDevice device) {
//Data callback of the broadcast scale, where only the data of the broadcast scale will be called back
}
});
iOS example:
//set proxy
QNBleApi *bleApi = [QNBleApi sharedBleApi];
bleApi.discoveryListener = self;
//Implement the proxy method
- (void)onDeviceDiscover:(QNBleDevice *)device {
//This method will call back after the device is found
}
- (void)onBroadcastDeviceDiscover:(QNBleBroadcastDevice *)device {
//Callback when receiving broadcast scale data, this method will only call back the broadcast scale data
}
- (void)onStopScan {
//Callback when scanning is stopped
}
- (void)onStartScan {
//Callback when starting scanning
}
2. Start the scan
Confirm that Bluetooth is turned on, and Android needs to check the location permission
andlocation switch
. If you confirm that Bluetooth is turned on, the location permission is authorized, and the location service switch is turned on, you can start Bluetooth scanning
After Android 6.0, targetSdkVersion>= 23 App above, you need to obtain positioning permission for Bluetooth scanning, please check About The location service switch is not mandatory, but some phones do not turn on this switch, and the device cannot be scanned, which is related to each mobile phone system.
iOS13 system has added Bluetooth usage permission, you need to check whether there is usage permission, confirm that authorized and Bluetooth is turned on, start scanning
The scanning method is QNBleApi.startBleDeviceDiscovery,-the-scanned-device-data-will-be-called-back-in-the-qnbledevicediscoverylistener in the scanning interface set above.
In addition, some feature settings related to scanning can be set in QNConfig, and the content to be set has been basically covered.
Usually APP will have an interface dedicated to measurement. We generally perform Bluetooth scanning after the interface is displayed, and stop scanning when the interface disappears.
android example:
QNBleApi.getInstance(context).startBleDeviceDiscovery(new QNResultCallback() {
@Override
public void onResult(int code, String msg) {
//This method does not return to the device, but indicates whether the scan was started successfully
if (code != CheckStatus.OK.getCode()) {
ToastMaker.show(ScanActivity.this,code+":"+msg);
}
}
});
iOS example:
//start scan
[[QNBleApi sharedBleApi] startBleDeviceDiscovery:^(NSError *error) {
//The callback here indicates whether the scan method is successfully started
if (error) {
NSLog([NSString stringWithFormat:@"Failed to start scanning method, reason: %@",error]);
}
}];
three、connection
After receiving the callback device, you can determine whether it is a device that needs to be connected (this is the business logic of APP), and if so, connect it.
Before connecting, you need to set the connection status callback and measurement data callback.
The method for setting the connection status callback is: QNBleApi.setBleConnectionChangeListener, this method is the same as setting the scan callback method, multiple connections only need to be set once, if it is determined that no longer need to connect Time, set to null/nil
android example:
QNBleApi.getInstance(context).setBleConnectionChangeListener(new QNBleConnectionChangeListener() {
//connecting
@Override
public void onConnecting(QNBleDevice device) {
setBleStatus(QNScaleStatus.STATE_CONNECTING);
}
//connected
@Override
public void onConnected(QNBleDevice device) {
setBleStatus(QNScaleStatus.STATE_CONNECTED);
}
@Override
public void onServiceSearchComplete(QNBleDevice device) {
//This state is the method after the search service is completed, usually this method does not require business logic
}
//Disconnecting, when calling disconnect, it will call back immediately
@Override
public void onDisconnecting(QNBleDevice device) {
setBleStatus(QNScaleStatus.STATE_DISCONNECTING);
}
//Disconnect, callback after disconnect
@Override
public void onDisconnected(QNBleDevice device) {
setBleStatus(QNScaleStatus.STATE_DISCONNECTED);
}
//A connection error has occurred, please refer to the attached table for the error code
@Override
public void onConnectError(QNBleDevice device, int errorCode) {
setBleStatus(QNScaleStatus.STATE_DISCONNECTED);
}
});
iOS example:
- (void)onConnecting:(QNBleDevice *)device {
//callback to start connection
}
- (void)onConnected:(QNBleDevice *)device {
//callback for successful connection
}
- (void)onServiceSearchComplete:(QNBleDevice *)device {
//Callback for service completion
}
- (void)onDisconnecting:(QNBleDevice *)device {
//Disconnecting callback
}
- (void)onConnectError:(QNBleDevice *)device error:(NSError *)error {
//callback for connection failure
}
After setting the Bluetooth status callback, you also need to set the callback interface of the measurement data. The method is: QNBleApi.setDataListener, which can also be set once, please set it when it is no longer used Null/nil
android example:
QNBleApi.getInstance(context).setDataListener(new QNScaleDataListener() {
@Override
public void onGetUnsteadyWeight(QNBleDevice device, double weight) {
//This method received unstable weight data. During one measurement, this method will call back multiple times until the data is stable and complete data is obtained.
}
@Override
public void onGetScaleData(QNBleDevice device, QNScaleData data) {
//The method is to receive the complete measurement data
}
@Override
public void onGetStoredScale(QNBleDevice device, List<QNScaleStoreData> storedDataList) {
//This method is to receive the stored data on the scale side, the processing method of the stored data can refer to the demo, or you can define it yourself
}
@Override
public void onGetElectric(QNBleDevice device, int electric) {
//This is the percentage of electricity obtained, only the electricity obtained by the charging scale is meaningful
}
//Connection status during measurement
@Override
public void onScaleStateChange(QNBleDevice device, int status) {
setBleStatus(status);
}
});
iOS example:
- (void)onGetUnsteadyWeight:(QNBleDevice *)device weight:(double)weight {
//This method received unstable weight data. During one measurement, the method will call back multiple times until the data is stable and complete data is obtained
}
- (void)onGetScaleData:(QNBleDevice *)device data:(QNScaleData *)scaleData {
//The method is to receive the complete measurement data
}
- (void)onGetStoredScale:(QNBleDevice *)device data:(NSArray <QNScaleStoreData *> *)storedDataList {
//This method is to receive the stored data on the scale side, the processing method of the stored data can refer to the demo, or you can define it yourself
}
- (void)onGetElectric:(NSUInteger)electric device:(QNBleDevice *)device {
//This is the percentage of electricity obtained, only the electricity obtained by the charging scale is meaningful
}
- (void)onScaleStateChange:(QNBleDevice *)device scaleState:(QNScaleState)state {
//Connection status during measurement
}
In addition, before calling the connection, it is best to stop the previous scan (we found that some mobile phones have Bluetooth scanning and Bluetooth connection at the same time, which will reduce the failure rate of successful connection). After stopping the scan, delaying the call for 200 ~ 500ms before calling the connection will increase the success rate of the connection. The method to stop Bluetooth scanning is: QNBleApi.stopBleDeviceDiscovery.
android example:
QNBleApi.getInstance(context).stopBleDeviceDiscovery(new QNResultCallback() {
@Override
public void onResult(int code, String msg) {
if (code == CheckStatus.OK.getCode()) {
isScanning = false;
}
}
});
iOS example:
[[QNBleApi sharedBleApi] stopBleDeviceDiscovery:^(NSError *error) {
//The callback here indicates whether the method to stop scanning is successful
if (error) {
NSLog([NSString stringWithFormat:@"Failed to stop scanning method, reason: %@",error]);
}
}];
The method of connecting the device needs to pass the Yolanda Bluetooth device object (QNBleDevice) and the Yolanda user profile object (QNUser), where QNBleDeviceQNUser is created as: QNBleApi.buildUser, the specific method of use can be Refer to the method description.
After confirming OK in the previous operation, you can finally connect. The connection method is: QNBleApi.connectDevice.-the-connection-status-callback-method-is-the-above-qnbleconnectionchangelistener
android example:
QNBleApi.getInstance(context).connectDevice(device, user, new QNResultCallback() {
@Override
public void onResult(int code, String msg) {
//This method is just a method to adjust whether the connection method is successful
}
});
iOS example:
[[QNBleApi sharedBleApi] connectDevice:device user:user callback:^(NSError *error) {
//The callback here indicates whether the call to the connection method was successful
if (error) {
NSLog([NSString stringWithFormat:@"Failed to call connection method, reason: %@",error]);
}
}];
Weighing process
The data and status of the weighing process will be recalled in the above-mentioned QNScaleDataListener
End of measurement
After receiving stable data (that is, QNScaleDataListener.onGetScaleData),-the-measurement-is-completed.-at-this-time,-the-scale-will-not-disconnect-bluetooth-immediately,-but-will-wait-for-a-short-time-(may-be-2-minutes)-before-the-scale-is-closed.-if-you-need-to-disconnect-as-soon-as-possible,-you-can-call-the-method-qnbleapi.disconnectdevice with a delay of 2s after receiving stable data to disconnect the device.
At this point, the basic process of the ordinary Bluetooth scale has been completed, the APP can save the data and display the data by itself after receiving the stable data. Data standards can be judged by our company-SDK indicator standard description.
For other questions, please refer to the code in the Demo first. If you have any questions, you can also check FAQ Common problems have not found related problems, you can try to search in the SDK if there are related APIs or instructions, if not, please contact our technical staff for assistance.
Demo address is as follows: iOSDemo