1

I'm trying to make a chrome app that can send commands to a Datecs DP05BT fiscal printer.

I managed to establish a connection to the fiscal printer but I don't know how to use chrome.bluetooth.send to send a command (<44> for example) to the device.

EDIT 1: I managed to send a string with chrome.bluetooth.send using string to array buffer converter provided by chrome developers but the fiscal printer doesn't do anything. I managed to find an android app that can send commands to the fiscal printer.

My question is How can I implement JavaScript into my app? Or an SDK that can manage the string to data.readeable.by.fiscal.printer connection?

EDIT 2: I managed to find an SDK for communication with the fiscal printer, the problem is, it's for android. Can I include that into my app?

EDIT 3: The fiscal printer is 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;
}
Dayan
  • 7,634
  • 11
  • 49
  • 76
Sorin Ilie
  • 21
  • 3
  • Provide some of the code you currently have that establishes a connection with the printer, and the SDK you are using, plus include the model of your printer. – Dayan May 09 '17 at 12:38
  • @Dayan I updated with fiscal printer model and code snippet of the connection ! – Sorin Ilie May 09 '17 at 12:49

0 Answers0