PhoneGap Day - October 12, 2017
Don Coleman @doncoleman
0000FF10-0000-1000-8000-00805F9B34FB
0000FF11-0000-1000-8000-00805F9B34FB
0000FF12-0000-1000-8000-00805F9B34FB
Provides services
Exercise 1
cordova create \
scan com.example.scan Scan \
--template bluetooth-workshop-template
ble.scan(
[], // any services
5, // 5 seconds
onDiscoverDevice,
failure
);
var onDiscoverDevice = function(device) {
console.log(
device.name,
device.id,
device.rssi
);
}
https://github.com/don/phonegap-ble-workshop
Exercise 2
ble.connect(
device.id,
onConnection,
failure
);
var onConnection = function(peripheral) {
console.log(
JSON.stringify(peripheral)
);
}
Exercise 3
ble.startNotification(
device.id,
BUTTON_SERVICE,
DATA_CHARACTERISTIC,
onNotification,
failure);
var onNotification = function(buffer) {
var data = new Uint8Array(buffer);
if (data[0] === 0) {
message = 'Button is released.';
} else {
message = 'Button is pressed.';
}
}
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.onNotification()
Exercise 4
f000AA64-0451-4000-b000-000000000000
ble.write(
device.id,
service,
characteristic,
data,
success,
failure);
ble.write(
app.peripheral.id,
IO_SERVICE,
CONFIGURATION_CHARACTERISTIC,
new Uint8Array([1]).buffer,
app.showDetailPage,
app.onError
);
Enable IO Service in app.onConnect()
ble.write(
app.peripheral.id,
IO_SERVICE,
DATA_CHARACTERISTIC,
data.buffer,
success,
app.onError
);
Write value to data characteristic in app.setDataValue(int)
Device | Integer | Bits |
---|---|---|
Red LED | 1 | 0001 |
Green LED | 2 | 0010 |
Buzzer | 4 | 0100 |
var data = 0;
if (redCheckbox.checked) {
data = data | RED_LED;
}
if (greenCheckbox.checked) {
data = data | GREEN_LED;
}
if (buzzerCheckbox.checked) {
data = data | BUZZER;
}
app.setDataValue(data);
Encode data with bitmask in app.optionsChanged()
Exercise 5
f000AA00-0451-4000-b000-000000000000
Don Coleman @doncoleman
Hands-on Bluetooth Low Energy Workshop by Don Coleman is licensed under a Creative Commons Attribution 4.0 International License.