Connect Device


⚠️ Note: This document describes how to use the SDK's API to operate the scanner device and provides sample code in C language. Please note that the API is a synchronous operation and may block the main thread.

⚠️ Note: Before using the API, you need to scan and obtain the device, and get the id of the device to connect. For specific steps, please refer to Scan Device.

1. Connect Device

After obtaining the device id, you can use the following method to connect the device:

const char* result = inateck_scanner_ble_connect(const char* device_id);

The return result is in JSON format. If the call is successful, result will return:

{
    "status": 0,
    "error": "",
    "id": "device id",
    "device_name": "device name",
    "is_connected": "is connected"
}

If the call fails, result will return:

{
    "status": 1,
    "error": "error message",
    "id": "",
    "device_name": "",
    "is_connected": false
}

The return result contains the following fields:

2. Check Communication

After connecting the device, you can use the following method to check if you can communicate with the device:

const char* result = inateck_scanner_ble_check_communication(const char* device_id);

The return result is in JSON format. If the call is successful, result will return:

{
    "status": 0,
    "error": "",
}

If the call fails, result will return:

{
    "status": 1,
    "error": "error message",
}

3. Grant Configuration Permission

After connecting the device, you need to grant configuration permission. If permission is not granted, the scanner will disconnect the Bluetooth connection. You can use the following method to grant configuration permission:

const char* result = inateck_scanner_ble_auth(const char* device_id);

The return result is in JSON format. If the call is successful, result will return:

{
    "status": 0,
    "error": "",
}

If the call fails, result will return:

{
    "status": 1,
    "error": "error message",
}

4. Register Scan Result Callback

Register the scan result callback. When a QR code is scanned, the callback will be called. The method is as follows:

void callback(const char* scan_result) {
    // scan_result is the scanned barcode information
    // The barcode information is in JSON format
    // {
    //     "status": 0,
    //     "code": "scan code",
    //     "source_code": "scan original code",
    //     "id": "device id",
    //     "device_name": "device name",
    // }
}

const char* result = inateck_scanner_ble_set_code_callback(const char* device_id, callback);

The return result is in JSON format. If the call is successful, result will return:

{
    "status": 0,
    "error": "",
}

If the call fails, result will return:

{
    "status": 1,
    "error": "error message",
}

After the scan result callback is successfully registered, the callback function will be called when a barcode is scanned. The scan result information is in JSON format, with the following parameters:

5. Register Device Disconnect Callback

Register the device disconnect callback. When the device is disconnected, the callback will be called. The method is as follows:

void callback(const char* device_info) {
    // device_info is the device information
    // The device information is in JSON format
    // {
    //     "status": 0,
    //     "id": "device id",
    //     "device_name": "device name",
    //     "is_connected": "is connected",
    // }
}

const char* result = inateck_scanner_ble_set_disconnect_callback(callback);

The return result is in JSON format. If the call is successful, result will return:

{
    "status": 0,
    "error": "",
}

If the call fails, result will return:

{
    "status": 1,
    "error": "error message",
}

6. Disconnect Device

Disconnect the device. After disconnection, the registered scan result callback and device disconnect callback will no longer be called. The method is as follows:

const char* result = inateck_scanner_ble_disconnect(const char* device_id);

The return result is in JSON format. If the call is successful, result will return:

{
    "status": 0,
    "error": "",
}

If the call fails, result will return:

{
    "status": 1,
    "error": "error message",
}

7. Sample Code

Here is a complete sample code:

#include <stdio.h>
#include <inateck_scanner_ble.h>

void scan_callback(const char* scan_result) {
    printf("Scan result: %s\n", scan_result);
}

void disconnect_callback(const char* device_info) {
    printf("Device disconnect: %s\n", device_info);
}

int main() {
    const char* device_id = "device id";

    const char* result = inateck_scanner_ble_connect(device_id);
    printf("Connect result: %s\n", result);

    result = inateck_scanner_ble_check_communication(device_id);
    printf("Check communication result: %s\n", result);

    result = inateck_scanner_ble_auth(device_id);
    printf("Auth result: %s\n", result);

    result = inateck_scanner_ble_set_code_callback(device_id, scan_callback);
    printf("Set code callback result: %s\n", result);

    result = inateck_scanner_ble_set_disconnect_callback(disconnect_callback);
    printf("Set disconnect callback result: %s\n", result);

    sleep(10);

    result = inateck_scanner_ble_disconnect(device_id);
    printf("Disconnect result: %s\n", result);

    return 0;
}

8. Sample Code in Other Languages