bykof/cordova-plugin-webserver

some times this code not work correctly

Closed this issue · 2 comments

some times this code not work correctly,Please provide some examples to work

I have integrate this code with cordova Bluetooth plugin but that code not work correctly,I have attached my code below

'use strict';

var app = {
initialize: function() {
this.bindEvents();
this.showMainPage();
},
bindEvents: function() {

    var TOUCH_START = 'touchstart';
    if (window.navigator.msPointerEnabled) { // windows phone
        TOUCH_START = 'MSPointerDown';
    }
    document.addEventListener('deviceready', this.onDeviceReady, false);
    refreshButton.addEventListener(TOUCH_START, this.refreshDeviceList, false);
    sendButton.addEventListener(TOUCH_START, this.sendData, false);
    start.addEventListener(TOUCH_START,this.start,false);
    disconnectButton.addEventListener(TOUCH_START, this.disconnect, false);
    deviceList.addEventListener('touchstart', this.connect, false);
},
onDeviceReady: function() {
    app.refreshDeviceList();
},
refreshDeviceList: function() {
    bluetoothSerial.list(app.onDeviceList, app.onError);
},
onDeviceList: function(devices) {
    var option;

    // remove existing devices
    deviceList.innerHTML = "";
    app.setStatus("");

    devices.forEach(function(device) {

        var listItem = document.createElement('li'),
            html = '<b>' + device.name + '</b><br/>' + device.id;

        listItem.innerHTML = html;

        if (cordova.platformId === 'windowsphone') {
          // This is a temporary hack until I get the list tap working
          var button = document.createElement('button');
          button.innerHTML = "Connect";
          button.addEventListener('click', app.connect, false);
          button.dataset = {};
          button.dataset.deviceId = device.id;
          listItem.appendChild(button);
        } else {
          listItem.dataset.deviceId = device.id;
        }
        deviceList.appendChild(listItem);
    });

    if (devices.length === 0) {

        option = document.createElement('option');
        option.innerHTML = "No Bluetooth Devices";
        deviceList.appendChild(option);

        if (cordova.platformId === "ios") { // BLE
            app.setStatus("No Bluetooth Peripherals Discovered.");
        } else { // Android or Windows Phone
            app.setStatus("Please Pair a Bluetooth Device.");
        }

    } else {
        app.setStatus("Found " + devices.length + " device" + (devices.length === 1 ? "." : "s."));
    }

},
connect: function(e) {
    var onConnect = function() {
            // subscribe for incoming data
            bluetoothSerial.subscribe('\n',app.onError);//app.onData

            resultDiv.innerHTML = "";
            app.setStatus("Connected");
            app.showDetailPage();
        };

    var deviceId = e.target.dataset.deviceId;
    if (!deviceId) { // try the parent
        deviceId = e.target.parentNode.dataset.deviceId;
    }

    bluetoothSerial.connect(deviceId, onConnect, app.onError);
},

start: function(event){
var string = device.uuid;
webserver.onRequest(
function(request) {
console.log("O MA GAWD! This is the request: ", request);

        webserver.sendResponse(
            request.requestId,
            {
                status: 200,
                body: '{ "DeviceInfo": {"uuid":"'+string+'"}}',
                headers: {
                    'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE,PATCH,OPTIONS',
'Access-Control-Allow-Headers': 'Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers',
                    'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
                }
            }
        );
    }
);

webserver.start(8081);
alert("webserver started");
},



sendData: function(event) { // send data to Arduino

var data2=" hello how are you guys";
var success = function() {
console.log("success");
alert("data printed successfully");
// resultDiv.innerHTML = resultDiv.innerHTML + "Sent: " + messageInput.value + "
";
// resultDiv.scrollTop = resultDiv.scrollHeight;
};

    var failure = function() {
        alert("Failed writing data to Bluetooth peripheral");
    };

    var data = data2;
    bluetoothSerial.write(data, success, failure);
},
disconnect: function(event) {
    bluetoothSerial.disconnect(app.showMainPage, app.onError);
},
showMainPage: function() {
    mainPage.style.display = "";
    detailPage.style.display = "none";
},
showDetailPage: function() {
    mainPage.style.display = "none";
    detailPage.style.display = "";
},
setStatus: function(message) {
    console.log(message);

    window.clearTimeout(app.statusTimeout);
    statusDiv.innerHTML = message;
    statusDiv.className = 'fadein';

    // automatically clear the status with a timer
    // app.statusTimeout = setTimeout(function () {
    //     statusDiv.className = 'fadeout';
    // }, 5000);
},
onError: function(reason) {
    alert("ERROR: " + reason); // real apps should use notification.alert
}

};

////////////////////////////////////////////////////////////////////////////////////////////
Html code is:

    <link rel="stylesheet" type="text/css" href="css/index.css" />
    <title>ITMS Bluetoot Print App</title>
</head>
<body>
    <div class="app">
        <h1>ITMS Bluetoot Print App</h1>
        <div id="mainPage">
            <ul id="deviceList">
            </ul>
            <button id="refreshButton">Refresh</button>
        </div>
        <div id="detailPage">
            <div id="resultDiv"></div>
            <div>
                <button id="start">start</button>
                <button id="sendButton">Send</button>
            </div>
            <button id="disconnectButton">Disconnect</button>
        </div>
        <div id="statusDiv"></div>
    </div>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript">
        app.initialize();
    </script>
</body>