Code Monkey home page Code Monkey logo

Comments (2)

bykof avatar bykof commented on August 13, 2024

Here is an example: https://github.com/bykof/cordova-plugin-webserver#example

from cordova-plugin-webserver.

Gunasekaran15 avatar Gunasekaran15 commented on August 13, 2024

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>

from cordova-plugin-webserver.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.