1

我正在尝试制作一个可以向 Datecs DP05BT 财务打印机发送命令的 chrome 应用程序。

我设法建立了与财务打印机的连接,但我不知道如何使用chrome.bluetooth.send将命令(<44>例如)发送到设备。

编辑1:我设法使用chrome开发人员提供的字符串到数组缓冲区转换器发送带有chrome.bluetooth.send的字符串,但财务打印机没有做任何事情。我设法找到了一个可以向财务打印机发送命令的 android 应用程序。

我的问题是如何在我的应用程序中实现 JavaScript?还是可以管理字符串data.readeable.by.fiscal.printer连接的SDK?

编辑 2:我设法找到了一个用于与财务打印机通信的 SDK,问题是,它适用于 android。我可以将它包含在我的应用程序中吗?

编辑 3:财务打印机是Datecs DP05BT.

    function ConnectToService(device, uuid, container) {
    chrome.bluetoothSocket.create({}, function (info) {
        if (chrome.runtime.lastError) {
            log("Error creating socket: " + chrome.runtime.lastError.message);
            return;
        }
        log("Socket OK!");
        container.socketId = info.socketId;
        chrome.bluetoothSocket.onReceive.addListener(function (info) {
            log("Data received on socket " + info.socketId + ", length=" + info.data.byteLength);
        });
        chrome.bluetoothSocket.onReceiveError.addListener(function (info) {
            log("Error receiving data on socket " + info.socketId + ", error code=" + info.error + ", message=" + info.errorMessage);
            chrome.bluetoothSocket.close(info.socketId, function () {
                log("socket closed");
                container.socketId = -1;
            });
        });
        chrome.bluetoothSocket.connect(info.socketId, device.address, uuid, function () {
            if (chrome.runtime.lastError) {
                log("Error connecting to socket: " + chrome.runtime.lastError.message);
                return;
            }
            log("Connect OK!");

        chrome.bluetoothSocket.send(info.socketId, str2ab('<44>'), function(bytes_sent){

            if (chrome.runtime.lastError) {
            console.log("Send failed: " + chrome.runtime.lastError.message);
         } else {
            console.log("Sent " + bytes_sent + " bytes")
         }
        });
    });
    });
};

function str2ab(str)
{
     var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char
     var bufView = new Uint16Array(buf);
     for (var i=0, strLen=str.length; i < strLen; i++) {
       bufView[i] = str.charCodeAt(i);
     }
     return buf;
}
4

0 回答 0