Scanning Devices


⚠️ Note: This document describes how to use the SDK's API to scan devices and provides sample code in C. Please note that the API is synchronous and may block the main thread when used.

1. Initialize the SDK

Before using the SDK's features, you need to initialize the SDK. The method is as follows:

const char* result = inateck_scanner_ble_init();

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"
}

2. Ensure Bluetooth is Enabled

Before scanning for devices, you need to ensure that Bluetooth is enabled on the device. If Bluetooth is not enabled, devices cannot be scanned. This operation is synchronous and may block the main thread. The method is as follows:

const char* result = inateck_scanner_ble_wait_available();

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. Register Scan Device Callback

Register a scan device callback, which will be called when a device is scanned. The method is as follows:

void callback(const char* device_info_json) {
    // device_info_json contains the scanned device information in the following format:
    // {
    //     "id": "device id",
    //     "device_name": "device name",
    //     "is_connected": "is connected",
    // }
}

const char* result = inateck_scanner_ble_set_discover_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"
}

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

4. Start Scanning Devices

Start scanning for devices. After a device is scanned, the registered scan device callback will be called. In the system's Bluetooth device list, already connected HID devices will also be scanned.

⚠️ Please ensure that the scan device callback is registered.

⚠️ On the Windows platform, due to system limitations, in HID mode, connect the barcode scanner on the Windows system Bluetooth device page first, then use this API to scan and obtain the barcode scanner.

The method is as follows:

const char* result = inateck_scanner_ble_start_discover();

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"
}

5. Stop Scanning Devices

Stop scanning for devices. After stopping, the registered scan device callback will no longer be called. The method is as follows:

const char* result = inateck_scanner_ble_stop_discover();

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. Get Device List

Get the list of scanned devices. The method is as follows:

const char* result = inateck_scanner_ble_get_device_list();

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

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

If the call fails, result will return:

{
    "status": 1,
    "error": "error message"
    "device_list": []
}

7. Sample Code

Here is a complete sample code:

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

void callback(const char* device_info_json) {
    printf("device_info_json: %s\n", device_info_json);
}

int main() {
    const char* result = inateck_scanner_ble_init();
    printf("inateck_scanner_ble_init: %s\n", result);

    result = inateck_scanner_ble_wait_available();
    printf("inateck_scanner_ble_wait_available: %s\n", result);

    result = inateck_scanner_ble_set_discover_callback(callback);
    printf("inateck_scanner_ble_set_discover_callback: %s\n", result);

    result = inateck_scanner_ble_start_discover();
    printf("inateck_scanner_ble_start_discover: %s\n", result);

    // Wait for device scanning
    sleep(10);

    result = inateck_scanner_ble_stop_discover();
    printf("inateck_scanner_ble_stop_discover: %s\n", result);

    result = inateck_scanner_ble_get_device_list();
    printf("inateck_scanner_ble_get_device_list: %s\n", result);

    return 0;
}

8. Sample Code in Other Languages