Bluetooth ruler
- Short press the device
switch key
to power on, long press the key; - Device
function key
Short press to lock the current measurement data, long press to switch the device unit.
Bluetooth ruler SDK access steps
1. Initialize QNSDK
Use QNBleApi.initSdk for initialization, which has been introduced in the previous article, please check
Second, set the scale callback listener class
Before enabling Bluetooth scanning, you need to set the listener class, the method is QNBleApi.setBleRulerListener, this method only needs to be called once, and remember to set it to null/ nil
android example:
QNBleApi mQNBleApi = QNBleApi.getInstance(context);
mQNBleApi.setBleRulerListener(this)
iOS example:
QNBleApi *bleApi = [QNBleApi sharedBleApi];
bleApi.bleRulerListener = self;
2. Implement the listener method
In order to implement the monitoring method, QNSDK will call back the discovered settings, device link status and measurement data results to the monitoring method.
android example:
@Override
public void onRulerDeviceDiscover(QNBleRulerDevice device) {
}
@Override
public void onRulerConnecting(QNBleRulerDevice device) {
}
@Override
public void onRulerConnected(QNBleRulerDevice device) {
}
@Override
public void onGetReceiveRealTimeData(QNBleRulerData data, QNBleRulerDevice device) {
}
@Override
public void onGetReceiveResultData(QNBleRulerData data, QNBleRulerDevice device) {
}
@Override
public void onRulerDisconnected(QNBleRulerDevice device) {
}
@Override
public void onRulerConnectFail(QNBleRulerDevice device) {
}
iOS example:
- (void)onRulerDeviceDiscover:(QNBleRulerDevice *)device {
}
- (void)onRulerConnecting:(QNBleRulerDevice *)device {
}
- (void)onRulerConnectFail:(QNBleRulerDevice *)device {
}
- (void)onRulerConnected:(QNBleRulerDevice *)device {
}
- (void)onRulerDisconnected:(QNBleRulerDevice *)device {
}
- (void)onGetReceiveRealTimeData:(QNBleRulerData *)data device:(QNBleRulerDevice *)device {
}
- (void)onGetReceiveResultData:(QNBleRulerData *)data device:(QNBleRulerDevice *)device {
}
3. Initiate bluetooth 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.
android example:
QNBleApi.getInstance(context).startBleDeviceDiscovery(new QNResultCallback() {
@Override
public void onResult(int code, String msg) {
if (code != CheckStatus.OK.getCode()) {
ToastMaker.show(ScanActivity.this,code+":"+msg);
}
}
});
iOS example:
[[QNBleApi sharedBleApi] startBleDeviceDiscovery:^(NSError *error) {
if (error) {
NSLog([NSString stringWithFormat:@"启动扫描方法失败,原因: %@",error]);
}
}];
4. Connect Bluetooth ruler
After receiving the callback device, you can judge whether it is the device that needs to be connected (this belongs to the business logic of the APP), and if so, connect QNBleApi.connectRulerDevice. android example:
QNBleApi.getInstance(RulerActivity.this).connectRulerDevice(qnBleRulerDevice, new QNResultCallback() {
@Override
public void onResult(int code, String msg) {
}
});
iOS example:
[_bleApi connectRulerDevice:device callback:^(NSError *error) {
}];
5. Receive real-time data and user-determined data
Data from time to time, when the SDK and the perimeter ruler establish a Bluetooth connection, the perimeter ruler will actively report the current ruler length value and unit.
The user confirms the data, when the user presses the function key
for a short time, the scale will automatically report the length value and unit of the ruler when the user presses the function key
.
六、Disconnect
The disconnection of the gage scale can be divided into two types: the active disconnection of the SDK software and the active disconnection of the girder device. Both disconnect methods will trigger the Disconnect method of the perimeter monitor device. SDK software actively disconnects QNBleApi.disconnectDevice android example:
QNBleApi.getInstance(RulerActivity.this).disconnectDevice(mac, new QNResultCallback() {
@Override
public void onResult(int i, String s) {
}
});
iOS example:
[_bleApi disconnectDeviceWithMac:_ruler.mac callback:^(NSError *error) {
}];