Comments (2)
Here is an example: https://github.com/bykof/cordova-plugin-webserver#example
from cordova-plugin-webserver.
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)
- Can't init `webserver` - Can't set a GET path.
- Could not send response to the client HOT 1
- 'GCDWebServer.h' file not found HOT 4
- [bug] Headers are not presented in case of file serving
- Feature request: From base64 to response HOT 5
- Unclear way to set up the java files to use with javascript
- Wait for response to be finished and return possible error
- 1.0.4 does not install properly HOT 1
- 'webserver' is not defined HOT 2
- Point webserver to dataDirectory? HOT 1
- "webserver" or "window.webserver" always undefined...
- Capacitor Support
- Websocket response HOT 2
- Unable to install HOT 1
- Binary Responses
- not working HOT 1
- Can you provide a license file please? HOT 2
- Website accessible from a browser, but not from fetch? HOT 1
- Build fails for ios. GCDWebServer is not configured correctly? HOT 1
- Function documentation in README not correct
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cordova-plugin-webserver.