PhoneGap SF Meetup - May 24, 2016
Don Coleman - Chariot Solutions
0000FF10-0000-1000-8000-00805F9B34FB
0000FF11-0000-1000-8000-00805F9B34FB
0000FF12-0000-1000-8000-00805F9B34FB
0000FF16-0000-1000-8000-00805F9B34FB
Provides services
Uses services on a peripheral
cordova plugin add cordova-plugin-ble-central
ble.scan(services,
seconds,
success,
failure);
ble.scan(["FF10"],
5,
app.onDeviceDiscovered,
failure);
{
"name": "Robosmart",
"id": "78:C5:E5:9A:15:AA",
"advertising": {},
"rssi": -65
}
ble.connect(device_id,
connectSuccess,
connectFailure);
ble.connect("78:C5:E5:9A:15:AA",
onConnect,
onDisconnect);
var onConnect = function(peripheral) {
console.log(peripheral);
// read current state of light
// update UI
};
ble.read(device_id,
service_uuid,
characteristic_uuid,
success,
failure);
ble.read("78:C5:E5:9A:15:AA",
"FF10",
"FF11",
success,
failure);
var success = function(buffer) {
var data = new Uint8Array(buffer);
if (data[0] === 1) {
onButton.className = 'selected';
} else {
offButton.className = 'selected';
}
};
ble.write(device_id,
service_uuid,
characteristic_uuid,
value,
success,
failure);
var data = new Uint8Array(1);
data[0] = 1;
ble.write("78:C5:E5:9A:15:AA",
"FF10",
"FF11",
data.buffer,
success);
ble.writeWithoutResponse("78:C5:E5:9A:15:AA",
"FF10",
"FF11",
data.buffer,
success,
failure);
ble.startNotification(device_id,
service_uuid,
characteristic_uuid,
success,
failure);
ble.startNotification(deviceId,
"180d",
"2a37",
onData,
failure);
onData = function(buffer) {
var data = new Unit8Array(buffer);
var flags = data[0];
// read flags, etc
var rate = data[1];
console.log("Heart Rate is " + rate);
}
ble.startNotification(deviceId,
"FFE0",
"FFE1",
app.onButtonData,
app.onError);
Button | Integer | Bits |
---|---|---|
Left Button | 1 | 0001 |
Right Button | 2 | 0010 |
Reed Switch | 4 | 0100 |
if (state & LEFT_BUTTON) {
message += 'Left button is pressed.';
}
if (state & RIGHT_BUTTON) {
message += 'Right button is pressed.';
}
if (state & REED_SWITCH) {
message += 'Reed switch is activated.';
}
Decode button from bitmask in app.onButtonData()
WARNING: Experimental
cordova plugin add cordova-plugin-ble-peripheral
Don Coleman
Bluetooth Low Energy with Apache Cordova by Don Coleman
is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Based on a work at https://github.com/don/.../2015-06-24-phonegap-meetup.