Skip to content

Instantly share code, notes, and snippets.

@shubham7298
Created February 22, 2019 11:01
Show Gist options
  • Save shubham7298/d2a15d899ffa26ebab7b40a6d6965adb to your computer and use it in GitHub Desktop.
Save shubham7298/d2a15d899ffa26ebab7b40a6d6965adb to your computer and use it in GitHub Desktop.
BLE Eddystone scanners
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, user-scalable=no
initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0" />
<title>Sahayak</title>
<script>
// Redirect console.log when running from Evothings Workbench.
if (window.hyper && window.hyper.log) { console.log = hyper.log }
</script>
<style>
body
{
font-family: sans-serif;
}
h1
{
margin-right:110px;
}
.evo-image
{
position:fixed;
right:5px;
top:5px;
width:100px;
height:auto;
background:white;
}
</style>
</head>
<body>
<header style="background-color: #4fc3f7">Sahayak</header>
<h1>BLE Bus Scanner</h1>
<div id="formd">
<label for="from"><b>From:</b></label>
<input type="text" placeholder="Enter Source" id="from" required>
<br>
<label for="to"><b>To:</b></label>
<input type="text" placeholder="Enter Destination" id="to" required>
<button style="background-color: #0093c4" type="button" class="submitbtn" onclick="AppStart()">Submit</button>
</div>
<div id="present">
<button style="background-color: #0093c4" type="button" class="editbtn" onclick="editInfo()">Edit</button>
<p id="message">No Buses nearby</p>
<ul id="found-beacons" class="dynamic"></ul>
</div>
<!--
Including cordova.js will automatically include the JavaScript library
for Eddystone.
-->
<script src="cordova.js"></script>
<script>
document.getElementById("present").style.display = "none";
function AppStart() {
document.getElementById("formd").style.display = "none";
document.getElementById("present").style.display = "block";
blogic();
}
function editInfo() {
document.getElementById("present").style.display = "none";
document.getElementById("formd").style.display = "block";
document.getElementById("from").value = '';
document.getElementById("to").value = '';
}
// Application code starts here. The code is wrapped in a
// function closure to prevent overwriting global objects.
function blogic() {
var app = (function()
{
// Dictionary of beacons.
var beacons = {};
// Application object.
var app = {};
// Timer that displays list of beacons.
var timer = null;
app.initialize = function () {
document.addEventListener(
'deviceready',
function () { evothings.scriptsLoaded(onDeviceReady) },
false);
};
function onDeviceReady()
{
// Start tracking beacons!
setTimeout(startScan, 300);
// Timer that refreshes the display.
timer = setInterval(updateBeaconList, 500);
}
function onBackButtonDown()
{
evothings.eddystone.stopScan();
navigator.app.exitApp();
}
function startScan()
{
showMessage('Scan in progress.');
evothings.eddystone.startScan(
function(beacon)
{
// Update beacon data.
beacon.timeStamp = Date.now();
beacons[beacon.address] = beacon;
},
function(error)
{
showMessage('Eddystone scan error: ' + error);
});
}
// Map the RSSI value to a value between 1 and 100.
function mapBeaconRSSI(rssi)
{
if (rssi >= 0) return 1; // Unknown RSSI maps to 1.
if (rssi < -100) return 100; // Max RSSI
return 100 + rssi;
}
function getSortedBeaconList(beacons)
{
var beaconList = [];
for (var key in beacons)
{
beaconList.push(beacons[key]);
}
beaconList.sort(function(beacon1, beacon2)
{
return mapBeaconRSSI(beacon1.rssi) < mapBeaconRSSI(beacon2.rssi);
});
return beaconList;
}
function updateBeaconList()
{
removeOldBeacons();
displayBeacons();
}
function removeOldBeacons()
{
var timeNow = Date.now();
for (var key in beacons)
{
// Only show beacons updated during the last 60 seconds.
var beacon = beacons[key];
if (beacon.timeStamp + 3000 < timeNow)
{
delete beacons[key];
}
}
}
function displayBeacons()
{
var html = '';
var sortedList = getSortedBeaconList(beacons);
for (var i = 0; i < sortedList.length; ++i)
{
var beacon = sortedList[i];
var htmlBeacon =
'<p>'
// + htmlBeaconName(beacon)
// + htmlBeaconURL(beacon)
// + htmlBeaconNID(beacon)
+ htmlBeaconBID(beacon)
+ displayResult(beacon)
// + htmlBeaconEID(beacon)
// + htmlBeaconVoltage(beacon)
// + htmlBeaconTemperature(beacon)
// + htmlBeaconRSSI(beacon)
+ '</p>';
html += htmlBeacon
}
document.querySelector('#found-beacons').innerHTML = html;
}
function htmlBeaconName(beacon)
{
var name = beacon.name || 'no name';
return '<strong>' + name + '</strong><br/>';
}
function htmlBeaconURL(beacon)
{
return beacon.url ?
'URL: ' + beacon.url + '<br/>' : '';
}
function htmlBeaconURL(beacon)
{
return beacon.url ?
'URL: ' + beacon.url + '<br/>' : '';
}
function htmlBeaconNID(beacon)
{
return beacon.nid ?
'NID: ' + uint8ArrayToString(beacon.nid) + '<br/>' : '';
}
function htmlBeaconBID(beacon)
{
return beacon.bid ?
'BID: ' + uint8ArrayToString(beacon.bid) + '<br/>' : '';
}
function displayResult(beacon) {
var str = beacon.bid;
var from = parseInt(document.getElementById("from").value,16);
var to = parseInt(document.getElementById("to").value,16);
let going = false;
//let reaching = false;
for(let i = 0 ; i < 6 ; i++ )
{
if(str[i] == to && going == true)
{
playBeep();
vibrate();
//notify();
return 'Found Your Bus<br/>'+from+' to '+to;
}
if(str[i] == from && going == false)
going = true;
}
//var n = str.search("^.*?" + a1 + ".*?" + a4 + ".*?$");
return'Not Your<br/>'+from+' to '+to;
}
function htmlBeaconEID(beacon)
{
return beacon.eid ?
'EID: ' + uint8ArrayToString(beacon.eid) + '<br/>' : '';
}
function htmlBeaconVoltage(beacon)
{
return beacon.voltage ?
'Voltage: ' + beacon.voltage + '<br/>' : '';
}
function htmlBeaconTemperature(beacon)
{
return beacon.temperature && beacon.temperature != 0x8000 ?
'Temperature: ' + beacon.temperature + '<br/>' : '';
}
function htmlBeaconRSSI(beacon)
{
return beacon.rssi ?
'RSSI: ' + beacon.rssi + '<br/>' : '';
}
function uint8ArrayToString(uint8Array)
{
function format(x)
{
var hex = x.toString(16);
return hex.length < 2 ? '0' + hex : hex;
}
var result = '';
for (var i = 0; i < uint8Array.length; ++i)
{
result += format(uint8Array[i]) + ' ';
}
return result;
}
function showMessage(text)
{
document.querySelector('#message').innerHTML = text;
}
// This calls onDeviceReady when Cordova has loaded everything.
document.addEventListener('deviceready', onDeviceReady, false);
// Add back button listener (for Android).
document.addEventListener('backbutton', onBackButtonDown, false);
})();
app.initialize();
} // End of closure.
function playBeep() {
navigator.notification.beep(4);
}
// Vibrate for 1/2 seconds
function vibrate() {
navigator.notification.vibrate(200);
}
function notify() {
navigator.notification.alert(
'Your bus from '+document.getElementById("from").value+' to '+document.getElementById("to").value+' has come !', // message
'Bus Arrived', // title
'Done' // buttonName
);
}
function getValue() {
var from = parseInt(document.getElementById("from").value,16);
var to = parseInt(document.getElementById("to").value,16);
return from,to ;
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment