Created
September 11, 2015 19:01
-
-
Save don/d9fef2fb45fbd67a372d to your computer and use it in GitHub Desktop.
Cordova BLE Notify Test using bleno's Test service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Cordova project to test BLE notifications | |
// Run the test service on OS X or Linux with Node.js | |
// http://bit.ly/bleno-test-service | |
// characteristic ffffffff-ffff-ffff-ffff-fffffffffff5 will | |
// increment a counter every 5 seconds notifying this app | |
// create a new cordova project | |
// $ cordova create notify com.example.notify Notify | |
// replace notify/www/js/index.js with this file | |
// $ cd notify | |
// $ cordova plugin add cordova-plugin-ble-central | |
// $ cordova platform add android | |
// $ cordova run --device | |
var countDiv = document.createElement('div'); | |
countDiv.style.fontSize = '4em'; | |
countDiv.innerHTML = '-'; | |
var app = { | |
initialize: function() { | |
this.bindEvents(); | |
// clober contents of index with new HTML | |
document.querySelector('style,[rel="stylesheet"]').remove(); | |
document.body.innerHTML = 'Notification Test'; | |
document.body.appendChild(countDiv); | |
}, | |
bindEvents: function() { | |
document.addEventListener('deviceready', this.onDeviceReady, false); | |
}, | |
onDeviceReady: function() { | |
app.scan(); | |
}, | |
scan: function() { | |
// http://bit.ly/bleno-test-service | |
ble.scan(['ffffffff-ffff-ffff-ffff-fffffffffff0'], 10, | |
function(device) { | |
console.log('Connecting to ' + device); | |
// only expecting 1 service, so just connect | |
ble.connect(device.id, app.onConnect, app.onDisconnect); | |
}, | |
function() { | |
console.log('Scan Failed'); | |
} | |
); | |
}, | |
onConnect: function(device) { | |
console.log('onConnect'); | |
app.device = device; | |
ble.startNotification(device.id, | |
'ffffffff-ffff-ffff-ffff-fffffffffff0', // service | |
'ffffffff-ffff-ffff-ffff-fffffffffff5', // characteristic | |
app.onNotify, | |
function() { | |
console.log('startNotification failed'); | |
}); | |
}, | |
onDisconnect: function() { | |
alert('Disconnected'); | |
}, | |
onNotify: function(buffer) { | |
var data = new Uint32Array(buffer); | |
console.log(data); | |
countDiv.innerHTML = data[0]; | |
} | |
}; | |
app.initialize(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment