Skip to content

Commit 506c645

Browse files
committed
Restore test app functionality and slightly bulk out tests (#962)
1 parent b330a0f commit 506c645

File tree

4 files changed

+121
-2
lines changed

4 files changed

+121
-2
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "Bluetooth Low Energy (BLE) Central Plugin",
55
"scripts": {
66
"slimify": "node tools/make-slim-variant.mjs",
7-
"version": "node tools/set-plugin-version.mjs && git add plugin.xml"
7+
"version": "node tools/set-plugin-version.mjs && git add plugin.xml",
8+
"test": "tsc types.d.ts"
89
},
910
"cordova": {
1011
"id": "cordova-plugin-ble-central",

tests/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "cordova-plugin-ble-central-tests",
3+
"version": "1.0.0-dev",
4+
"description": "",
5+
"cordova": {
6+
"id": "cordova-plugin-ble-central-tests",
7+
"platforms": []
8+
},
9+
"keywords": [
10+
"ecosystem:cordova"
11+
],
12+
"author": "",
13+
"license": "Apache 2.0"
14+
}

tests/plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
xmlns:rim="http://www.blackberry.com/ns/widgets"
33
xmlns:android="http://schemas.android.com/apk/res/android"
44
id="com.megster.cordova.ble.tests"
5-
version="0.1.9-dev">
5+
version="1.0.0-dev">
66
<name>Bluetooth Low Energy Plugin Tests</name>
77
<license>Apache 2.0</license>
88

tests/tests.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ exports.defineAutoTests = function () {
1212
};
1313

1414
exports.defineManualTests = function (contentEl, createActionButton) {
15+
let scanned = [];
16+
let connectedDevice;
17+
1518
createActionButton('Is Bluetooth Enabled?', function () {
1619
ble.isEnabled(
1720
function () {
@@ -43,11 +46,13 @@ exports.defineManualTests = function (contentEl, createActionButton) {
4346
}
4447

4548
createActionButton('Scan', function () {
49+
scanned = [];
4650
var scanSeconds = 5;
4751
console.log('Scanning for BLE peripherals for ' + scanSeconds + ' seconds.');
4852
ble.startScan(
4953
[],
5054
function (device) {
55+
scanned.push(device);
5156
console.log(JSON.stringify(device));
5257
},
5358
function (reason) {
@@ -66,4 +71,103 @@ exports.defineManualTests = function (contentEl, createActionButton) {
6671
}
6772
);
6873
});
74+
75+
createActionButton('Connect', function () {
76+
connectedDevice = undefined;
77+
contentEl.innerHTML = '';
78+
buttonList(
79+
scanned,
80+
(d) => [d.name, d.id],
81+
(d) => ble.connect(d.id, onConnect, onError)
82+
);
83+
84+
function onConnect(d) {
85+
connectedDevice = d;
86+
console.log('BLE connected:', JSON.stringify(d));
87+
contentEl.innerHTML = '';
88+
}
89+
90+
function onError(e) {
91+
console.log('Error', e);
92+
contentEl.innerHTML = '';
93+
}
94+
});
95+
96+
createActionButton('Read', function () {
97+
contentEl.innerHTML = '';
98+
const readable = connectedDevice.characteristics.filter((c) => c.properties.indexOf('Read') != -1);
99+
buttonList(
100+
readable,
101+
(c) => [c.service, c.characteristic],
102+
(c) => ble.read(connectedDevice.id, c.service, c.characteristic, onRead, onError)
103+
);
104+
105+
function onRead(d) {
106+
console.log('Payload read:', JSON.stringify(Array.from(new Uint8Array(d))));
107+
contentEl.innerHTML = '';
108+
}
109+
110+
function onError(e) {
111+
console.log('Error', e);
112+
contentEl.innerHTML = '';
113+
}
114+
});
115+
116+
createActionButton('Write', function () {
117+
contentEl.innerHTML = '';
118+
const writable = connectedDevice.characteristics.filter((c) => c.properties.indexOf('Write') != -1);
119+
const payload = new Uint8Array(1);
120+
payload[0] = 65; // Capital A
121+
buttonList(
122+
writable,
123+
(c) => [c.service, c.characteristic],
124+
(c) => ble.write(connectedDevice.id, c.service, c.characteristic, payload.buffer, onWrite, onError)
125+
);
126+
127+
function onWrite(d) {
128+
console.log('Payload written');
129+
contentEl.innerHTML = '';
130+
}
131+
132+
function onError(e) {
133+
console.log('Error', e);
134+
contentEl.innerHTML = '';
135+
}
136+
});
137+
138+
createActionButton('Notify', function () {
139+
contentEl.innerHTML = '';
140+
const notifiable = connectedDevice.characteristics.filter(
141+
(c) => c.properties.indexOf('Notify') != -1 || c.properties.indexOf('Indicate') != -1
142+
);
143+
buttonList(
144+
notifiable,
145+
(c) => [c.service, c.characteristic],
146+
(c) => ble.startNotifications(connectedDevice.id, c.service, c.characteristic, onRead, onError)
147+
);
148+
149+
function onRead(d) {
150+
console.log('Payload received:', JSON.stringify(Array.from(new Uint8Array(d))));
151+
contentEl.innerHTML = '';
152+
}
153+
154+
function onError(e) {
155+
console.log('Error', e);
156+
contentEl.innerHTML = '';
157+
}
158+
});
159+
160+
function buttonList(items, labelFn, actionFn) {
161+
for (const item of items) {
162+
const button = document.createElement('button');
163+
for (const label of labelFn(item)) {
164+
button.append(label);
165+
button.append(document.createElement('br'));
166+
}
167+
button.removeChild(button.lastElementChild);
168+
button.onclick = () => actionFn(item);
169+
contentEl.append(button);
170+
contentEl.append(document.createElement('br'));
171+
}
172+
}
69173
};

0 commit comments

Comments
 (0)