Inateck-Technology-Inc / csharp_sdk

0 stars 0 forks source link

Init failed: 1 #1

Open af-multichannelsystems opened 4 days ago

af-multichannelsystems commented 4 days ago

After changing the path to the libfile to: const string LibPath = "./lib/scanner_ble_x86_64-pc-windows-gnu.dll"; and copying the file to: "C:\Programming\csharp_sdk-master\bin\x64\Debug\net8.0\lib\scanner_ble_x86_64-pc-windows-gnu.dll"

running the project results in this output:

SDK Version: 2.0.0 Init failed: 1

C:\Programming\csharp_sdk-master\bin\x64\Debug\net8.0\csharp_sdk.exe (process 39496) exited with code 1. To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops. Press any key to close this window . . .

The Scanner used is: Model: BCST-42 SN: 4B22XXL1

The Scanner is connected via the Dongle. I've tried disconnecting the dongle and connecting using a cable. The results are the same.

The OS is: OS Name: Microsoft WIndows 10 Enterprise Version: 10.0.19045 Build 19045 System Type: x64-based PC

I hope I provided all needed Information...

soSeven commented 3 days ago

Hello, we've recently added debug mode which will help us identify any issues by printing error messages. Could you please download the updated version and run it again? This way, we can see exactly where the problem lies.

af-multichannelsystems commented 3 days ago

The Output is as follows:

debug: true SDK Version: 2.0.1 No adapter found Init failed: 1

C:\Programming\csharp_sdk-master\bin\Debug\net8.0\csharp_sdk.exe (process 34556) exited with code 1.

The thing is, when scanning any barcode while I am in a textentry field, the code's text will get written in said textfield... So there definitely is a device connected.

On the Inateck website there is a paragraph stating, that the Firmware of certain scanners has to be updated, but only if the SN-Prefix is in a certain range.

For my case (the BCST-42) the range is: 3B09 - 3D10

As the SN of the owned scanner is 4B22[____] I haven't done anything regarding the firmware. Maybe that could be the solution?

soSeven commented 2 days ago

Hello, based on the error, it appears that your computer either lacks a Bluetooth adapter or the Bluetooth function is not turned on, as the SDK requires a Bluetooth adapter and for Bluetooth to be enabled. The current SDK does not support wired connections or connections via a dongle.

af-multichannelsystems commented 2 days ago

Hello, this explains a lot. Thanks for clarifying! It works like a charm as of yet.

If you are interested, I extended the demo a bit to be able to understand better, what is happening. Maybe it helps future customers:

namespace csharp_sdk;
using InateckScannerBle;
using Newtonsoft.Json.Linq;

class Program
{
    public const string CommandPrefix = "!";
    public static string MacAddress { get; set; }

    public static ScannerBle Scanner { get; set; }

    static Program()
    {
        MacAddress = ""; //set the MacAddress
        Scanner = new ScannerBle();
    }
    static void Main(string[] args)
    {
        Scanner.Debug(false);
        string sdkVersion = Scanner.SdkVersion();
        Console.WriteLine("SDK Version: " + sdkVersion);
        Console.WriteLine("MacAddress: " + MacAddress ?? "N/A");
        int status = Scanner.RegisterEvent((message) =>
        {
            PrintInGreen("Message: " + message);
            HandleEvents(message);
        });
        if (status != 0)
        {
            Console.WriteLine("Init failed: " + status);
            Environment.Exit(1);
        }
        Console.WriteLine("init success Status: " + status);
        while (true)
        {
            PrintMenu();
            Console.ForegroundColor = ConsoleColor.White;
            string? input = Console.ReadLine();
            input = input?.Trim();
            if (input == null)
            {
                continue;
            }
            string[] cmd = input.Split(' ');
            string command = cmd[0];
            if (!command.StartsWith(CommandPrefix))
            {
                PrintInRed("Invalid command.");
                continue;
            }
            command = command[CommandPrefix.Length..];
            PrintInGreen("Command: " + command);

            if (command == "scan")
            {
                int scanResult = Scanner.StartScan();
                PrintInGreen("Scan Result: " + scanResult);
            }
            else if (command == "stop")
            {
                int stopResult = Scanner.StopScan();
                PrintInGreen("Stop Result: " + stopResult);
            }
            else if (command == "devices")
            {
                string devices = Scanner.GetDevices();
                PrintInGreen("Devices: " + devices);
            }
            else if (command == "connect")
            {
                ConnectToScanner();
            }
            else if (command == "disconnect")
            {
                if (MacAddress == null)
                {
                    PrintInRed($"MacAddress is not assigned. Please use {CommandPrefix}setMac");
                    continue;
                }
                int device = Scanner.Disconnect(MacAddress);
                PrintInGreen("Device: " + device);
            }
            else if (command == "setMac")
            {
                if (cmd.Length != 2)
                {
                    PrintInRed($"Invalid Format. Please use the command as follows: {CommandPrefix}setMac FF:FF:FF:FF:FF:FF");
                    continue;
                }
                MacAddress = cmd[1];
                PrintInGreen($"Set MacAddress to: {MacAddress}");
            }
            else if (command == "version")
            {
                if (MacAddress == null)
                {
                    PrintInRed($"MacAddress is not assigned. Please use {CommandPrefix}setMac");
                    continue;
                }
                string version = Scanner.GetHardwareVersion(MacAddress);
                PrintInGreen("Hardware version: " + version);
            }
            else if (command == "battery")
            {
                if (MacAddress == null)
                {
                    PrintInRed($"MacAddress is not assigned. Please use {CommandPrefix}setMac");
                    continue;
                }
                string battery = Scanner.GetBattery(MacAddress);
                PrintInGreen("Battery: " + battery);
            }
            else if (command == "software")
            {
                if (MacAddress == null)
                {
                    PrintInRed($"MacAddress is not assigned. Please use {CommandPrefix}setMac");
                    continue;
                }
                string software = Scanner.GetSoftwareVersion(MacAddress);
                PrintInGreen("Software: " + software);
            }
            else if (command == "settingInfo")
            {
                if (MacAddress == null)
                {
                    PrintInRed($"MacAddress is not assigned. Please use {CommandPrefix}setMac");
                    continue;
                }
                string settingInfo = Scanner.GetSettingInfo(MacAddress);
                PrintInGreen("SettingInfo: " + settingInfo.Replace("},", "},\n"));
            }
            else if (command == "quiet")
            {
                if (MacAddress == null)
                {
                    PrintInRed($"MacAddress is not assigned. Please use {CommandPrefix}setMac");
                    continue;
                }
                string closeVolume = "[{\"area\":\"3\",\"value\":\"0\",\"name\":\"volume\"}]";
                string info = Scanner.SetSettingInfo(MacAddress, closeVolume);
                PrintInGreen("SettingInfo: " + info.Replace("},", "},\n"));
            }
            else if (command == "loud")
            {
                if (MacAddress == null)
                {
                    PrintInRed($"MacAddress is not assigned. Please use {CommandPrefix}setMac");
                    continue;
                }
                string closeVolume = "[{\"area\":\"3\",\"value\":\"4\",\"name\":\"volume\"}]";
                string info = Scanner.SetSettingInfo(MacAddress, closeVolume);
                PrintInGreen("SettingInfo: " + info.Replace("},", "},\n"));
            }
            else if (command == "destroy")
            {
                Scanner.Destroy();
            }
            else
            {
                PrintInRed($"Invalid command, example: {CommandPrefix} scan");
            }
        }
    }

    private static void ConnectToScanner()
    {
        if (MacAddress == null)
        {
            PrintInRed($"MacAddress is not assigned. Please use {CommandPrefix}setMac");
            return;
        }
        string device = Scanner.Connect(MacAddress, (message) =>
        {
            PrintInGreen("Mac: " + MacAddress);
            PrintInGreen("Code: " + message);

            JObject msgAsJson = JObject.Parse(message);
            var cleanedScannedCode = msgAsJson["code"]?.ToString();
            if (cleanedScannedCode != null)
            {
                PrintInGreen($"Cleaned scan result: {cleanedScannedCode}");
            }
            else
            {
                PrintInRed("Unable to extract code from json.");
            }
        });
        Scanner.Auth(MacAddress);
        PrintInGreen("Device: " + device);
    }

    private static void HandleEvents(string eventText)
    {
        JObject msgAsJson = JObject.Parse(eventText);
        var cleanedMessage = msgAsJson["msg"]?.ToString();
        if (cleanedMessage != null)
        {
            PrintInGreen($"Message: {cleanedMessage}");
            if (cleanedMessage == "scan") //scanner discovered
            {

            }
            else if (cleanedMessage == "disconnect")
            {

            }
            else
            {

            }
        }
    }

    private static void PrintMenu()
    {
        var preColor = Console.ForegroundColor;
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.WriteLine($"Command prefix: {CommandPrefix}");
        Console.WriteLine("Possible commands:\n" +
                          "\tscan --> scans for bluetooth devices\n" +
                          "\tstop --> stops scanning for bluetooth devices\n" +
                          "\tdevices --> shows discovered bluetooth devices\n" +
                          "\tdestroy --> destroys scanner property\n" +
                          "\tsetMac --> sets the mac address to the given one\n" +

                          "\n\tUse setMac before using the next commands:\n" +

                          "\t\tconnect --> connect to the device with the corresponding MacAddress\n" +
                          "\t\tdisconnect --> disconnect from the connected device\n" +
                          "\t\tversion --> print the hardware-version of the connected device\n" +
                          "\t\tbattery --> print the battery percentage of the connected device\n" +
                          "\t\tsoftware --> print the firmware version\n" +
                          "\t\tsettingInfo --> print all the settings\n" +
                          "\t\tquiet --> sets volume to 0\n" +
                          "\t\tloud --> sets volume to 4\n"
        //"\t\n" +
        );
        Console.ForegroundColor = preColor;
    }

    static void PrintInGreen(string printThis)
    {
        var consoleColorPre = Console.ForegroundColor;
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine(printThis);
        Console.ForegroundColor = consoleColorPre;
    }
    static void PrintInRed(string printThis)
    {
        var consoleColorPre = Console.ForegroundColor;
        Console.ForegroundColor = ConsoleColor.DarkRed;
        Console.WriteLine(printThis);
        Console.ForegroundColor = consoleColorPre;
    }
}