Sample Scanning:
Using Don Coleman's great BLE Central Cordova Plugin, scanning for peripherals is easy:
Install the plugin in your phonegap project: phonegap -d cordova plugin add com.megster.cordova.ble
and here is a quick example
function onPageLoad() { document.addEventListener("deviceready", onDeviceReady, false); } function onDeviceReady() { ble.scan([], 10, scanSuccess, scanFailure); } function scanSuccess(peripheral) { console.log(peripheral); } function scanFailure(err) { console.log(err); } window.addEventListener('load', onPageLoad);The TI SensorTag is a great BLE device for experimenting. It's capabilities are detailed online in the SensorTag User Guide.
<!DOCTYPE html> <html> <head> <script type="text/javascript" src="cordova.js"></script> <script type="text/javascript"> var accelerometer = { service: "F000AA10-0451-4000-B000-000000000000", data: "F000AA11-0451-4000-B000-000000000000", // read/notify 3 bytes X : Y : Z configuration: "F000AA12-0451-4000-B000-000000000000", // read/write 1 byte period: "F000AA13-0451-4000-B000-000000000000" // read/write 1 byte Period = [Input*10]ms }; var peripheralId = null; function onPageLoad() { document.addEventListener("deviceready", onDeviceReady, false); } function onDeviceReady() { ble.scan([], 10, scanSuccess, scanFailure); } function scanSuccess(peripheral) { //console.log(peripheral); //document.body.innerHTML = document.innerHTML + peripheral.name; if (peripheral.name == "SensorTag") { ble.stopScan(stopScanSuccess, stopScanFailure); console.log(peripheral); console.log("Connecting"); ble.connect(peripheral.id, connectSuccess, connectFailure); } } function scanFailure(err) { console.log(err); } function stopScanSuccess() { console.log("Stop Scan Success"); } function stopScanFailure() { console.log("Stop Scan Failure"); } function connectSuccess(peripheral) { console.log("Connected"); console.log(peripheral); peripheralId = peripheral.id; //console.log(foundPeripheral); // Turn the accelerometer on var configData = new Uint8Array(1); configData[0] = 0x01; ble.write(peripheral.id, accelerometer.service, accelerometer.configuration, configData.buffer, function() { console.log("Started accelerometer."); },function() { console.log("Start accelerometer error");}); // Do a read //ble.read(peripheral.id, accelerometer.service, accelerometer.data, readSuccess, readFailure); // We want to be notified ble.startNotification(peripheral.id, accelerometer.service, accelerometer.data, notifySuccess, notifyError); } function connectFailure() { console.log("Connection Failed"); } function readSuccess(data) { console.log(data) var a = new Uint8Array(data); var message = "X: " + a[0]/64 + "<br/>" + "Y: " + a[1]/64 + "<br/>" + "Z: " + a[2]/64 * -1; document.body.innerHTML = message; setTimeout(readAccel, 1000); } function readAccel() { ble.read(peripheralId, accelerometer.service, accelerometer.data, readSuccess, readFailure); } function readFailure(err) { console.log(err); } function notifySuccess(data) { console.log(data) var a = new Uint8Array(data); var message = "X: " + a[0]/64 + "<br/>" + "Y: " + a[1]/64 + "<br/>" + "Z: " + a[2]/64 * -1; document.body.innerHTML = message; } function notifyError(err) { console.log(err); } </script> </head> <body onload="onPageLoad()"> </body> </html>