Skip to content

Commit

Permalink
Restore test app functionality and slightly bulk out tests (#962)
Browse files Browse the repository at this point in the history
  • Loading branch information
peitschie committed Feb 13, 2025
1 parent b330a0f commit 506c645
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 2 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "Bluetooth Low Energy (BLE) Central Plugin",
"scripts": {
"slimify": "node tools/make-slim-variant.mjs",
"version": "node tools/set-plugin-version.mjs && git add plugin.xml"
"version": "node tools/set-plugin-version.mjs && git add plugin.xml",
"test": "tsc types.d.ts"
},
"cordova": {
"id": "cordova-plugin-ble-central",
Expand Down
14 changes: 14 additions & 0 deletions tests/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "cordova-plugin-ble-central-tests",
"version": "1.0.0-dev",
"description": "",
"cordova": {
"id": "cordova-plugin-ble-central-tests",
"platforms": []
},
"keywords": [
"ecosystem:cordova"
],
"author": "",
"license": "Apache 2.0"
}
2 changes: 1 addition & 1 deletion tests/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
xmlns:rim="http://www.blackberry.com/ns/widgets"
xmlns:android="http://schemas.android.com/apk/res/android"
id="com.megster.cordova.ble.tests"
version="0.1.9-dev">
version="1.0.0-dev">
<name>Bluetooth Low Energy Plugin Tests</name>
<license>Apache 2.0</license>

Expand Down
104 changes: 104 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ exports.defineAutoTests = function () {
};

exports.defineManualTests = function (contentEl, createActionButton) {
let scanned = [];
let connectedDevice;

createActionButton('Is Bluetooth Enabled?', function () {
ble.isEnabled(
function () {
Expand Down Expand Up @@ -43,11 +46,13 @@ exports.defineManualTests = function (contentEl, createActionButton) {
}

createActionButton('Scan', function () {
scanned = [];
var scanSeconds = 5;
console.log('Scanning for BLE peripherals for ' + scanSeconds + ' seconds.');
ble.startScan(
[],
function (device) {
scanned.push(device);
console.log(JSON.stringify(device));
},
function (reason) {
Expand All @@ -66,4 +71,103 @@ exports.defineManualTests = function (contentEl, createActionButton) {
}
);
});

createActionButton('Connect', function () {
connectedDevice = undefined;
contentEl.innerHTML = '';
buttonList(
scanned,
(d) => [d.name, d.id],
(d) => ble.connect(d.id, onConnect, onError)
);

function onConnect(d) {
connectedDevice = d;
console.log('BLE connected:', JSON.stringify(d));
contentEl.innerHTML = '';
}

function onError(e) {
console.log('Error', e);
contentEl.innerHTML = '';
}
});

createActionButton('Read', function () {
contentEl.innerHTML = '';
const readable = connectedDevice.characteristics.filter((c) => c.properties.indexOf('Read') != -1);
buttonList(
readable,
(c) => [c.service, c.characteristic],
(c) => ble.read(connectedDevice.id, c.service, c.characteristic, onRead, onError)
);

function onRead(d) {
console.log('Payload read:', JSON.stringify(Array.from(new Uint8Array(d))));
contentEl.innerHTML = '';
}

function onError(e) {
console.log('Error', e);
contentEl.innerHTML = '';
}
});

createActionButton('Write', function () {
contentEl.innerHTML = '';
const writable = connectedDevice.characteristics.filter((c) => c.properties.indexOf('Write') != -1);
const payload = new Uint8Array(1);
payload[0] = 65; // Capital A
buttonList(
writable,
(c) => [c.service, c.characteristic],
(c) => ble.write(connectedDevice.id, c.service, c.characteristic, payload.buffer, onWrite, onError)
);

function onWrite(d) {
console.log('Payload written');
contentEl.innerHTML = '';
}

function onError(e) {
console.log('Error', e);
contentEl.innerHTML = '';
}
});

createActionButton('Notify', function () {
contentEl.innerHTML = '';
const notifiable = connectedDevice.characteristics.filter(
(c) => c.properties.indexOf('Notify') != -1 || c.properties.indexOf('Indicate') != -1
);
buttonList(
notifiable,
(c) => [c.service, c.characteristic],
(c) => ble.startNotifications(connectedDevice.id, c.service, c.characteristic, onRead, onError)
);

function onRead(d) {
console.log('Payload received:', JSON.stringify(Array.from(new Uint8Array(d))));
contentEl.innerHTML = '';
}

function onError(e) {
console.log('Error', e);
contentEl.innerHTML = '';
}
});

function buttonList(items, labelFn, actionFn) {
for (const item of items) {
const button = document.createElement('button');
for (const label of labelFn(item)) {
button.append(label);
button.append(document.createElement('br'));
}
button.removeChild(button.lastElementChild);
button.onclick = () => actionFn(item);
contentEl.append(button);
contentEl.append(document.createElement('br'));
}
}
};

0 comments on commit 506c645

Please sign in to comment.