larkone
larkone

Reputation: 85

Cordova bluetooth plugins

I'm developing android app in cordova where i want read data sent from bluetooth. I'm testing on Android Lolipop (Sony XPERIA M2) and bluetooth barcode scanner.

For this purpose i tried to use plugins for cordova. Tested:

https://www.npmjs.com/package/cordova-plugin-bluetoothle

https://www.npmjs.com/package/cordova-plugin-networking-bluetooth

https://github.com/tanelih/phonegap-bluetooth-plugin

https://github.com/don/BluetoothSerial

and many others.

Every time i was possible to find my device but none of these plugins allow me to connect to scanner.

I got error: READ FAILED, SOCKET MIGHT CLOSED OR TIMEOUT, ERROR CODE: 9

My mobile is connected and paired with scanner correctly but i cant figure out why i cant connect via my app.

EDIT:

  onDeviceReady: function () {
    var dev = {};
    app.receivedEvent('deviceready');
    window.bluetooth.enable(function () {
        alert("SUCCESS");
    }, function () {
        alert("FAILED");
    });
    window.bluetooth.startDiscovery(onDeviceDiscovered, onDiscoveryFinished, onError);

    function onDeviceDiscovered(device) {
        alert("Found device " + device.address);
        if (device.address === "40:83:DE:4B:D4:12") { // address of scanner
            window.bluetooth.getUuids(onUuidsRetrieved, onError, device.address);
        }
    }
    function onDiscoveryFinished() {
        alert("SUCC FINISHED");
        if (dev.address === "40:83:DE:4B:D4:12") {
            alert("Trying to connect...");
            window.bluetooth.connect(onConnected, onErrorConn, {
                address: dev.address,
                uuid: dev.uuids[0]
            });
        }
    }
      function onConnected() {
        alert("Connected");
    }

    function onUuidsRetrieved(device) {
        alert(device.address + " UUID: " + device.uuids);
        if (device.address === "40:83:DE:4B:D4:12") {
            dev = device;
        }
    }

    function onError() {
        alert("ERROR");
    }

    function onErrorConn(error) {
        alert(error.code + " " + error.message);
    }


}

Was someone facing to same issue ?

Thanks for all your help.

Upvotes: 1

Views: 921

Answers (1)

David
David

Reputation: 633

try using the cordova-plugin-networking-bluetooth for the central and for the central I have used cordova-plugin-ble-peripheral.

works great, able to send service and chariteristics changes, and do file transfer

Upvotes: 0

Related Questions