Bluetooth ruler
- Short press the device
switch keyto power on, long press the key; - Device
function keyShort 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
Make sure that Bluetooth is turned on. On the Android side, you also need to check that the corresponding permissions have been authorized and the location service switch is turned on. Then you can start the Bluetooth scan If the targetSdk of your app is greater than 30 and the system version of your phone is Android 12 or above, the following permissions are required
android.permission.BLUETOOTH_ADVERTISE android.permission.BLUETOOTH_SCAN android.permission.BLUETOOTH_CONNECT
Otherwise, it is required android.permission.BLUETOOTH android.permission.BLUETOOTH_ADMIN android.permission.ACCESS_COARSE_LOCATION android.permission.ACCESS_FINE_LOCATION
The iOS13 system has added Bluetooth usage permission. You need to check if you have the usage permission. After confirming that you are authorized and Bluetooth is turned on, start the scan
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) {
}];