diff --git a/package.json b/package.json index 5b943076..ac4f8d74 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/tests/package.json b/tests/package.json new file mode 100644 index 00000000..df31ebe7 --- /dev/null +++ b/tests/package.json @@ -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" +} diff --git a/tests/plugin.xml b/tests/plugin.xml index 2d3ac36b..324cb011 100644 --- a/tests/plugin.xml +++ b/tests/plugin.xml @@ -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"> Bluetooth Low Energy Plugin Tests Apache 2.0 diff --git a/tests/tests.js b/tests/tests.js index 7b34d2ba..f653445e 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -12,6 +12,9 @@ exports.defineAutoTests = function () { }; exports.defineManualTests = function (contentEl, createActionButton) { + let scanned = []; + let connectedDevice; + createActionButton('Is Bluetooth Enabled?', function () { ble.isEnabled( function () { @@ -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) { @@ -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')); + } + } };