mirror of
https://github.com/efiten/coredrive-rx.git
synced 2026-11-23 12:57:58 +00:00
Step 1 of a native Android app (issue #13). The Android WebView has no Web Bluetooth, so CapacitorBleTransport (src/transport-native.js) implements the WebBluetoothTransport API on @capacitor-community/bluetooth-le: NUS service, notify to every onFrame listener, serialised writes, reconnect with backoff. The plugin requests a 512-byte MTU on connect (Device.kt). NativeGps (src/gps-native.js) uses @capacitor-community/background- geolocation with a backgroundMessage, which runs a foreground service, so fixes should keep coming with the screen off. That is the point of the native app; it is not verified on a device yet. src/platform.js picks one or the other via Capacitor.isNativePlatform(). In the browser nothing changes in behaviour; the web bundle grows by about 21 kB (512 kB, was 490 kB) because the plugin JS is now imported. The service worker is not registered in the native app, and the Web Bluetooth notice is not shown there. Tests: 9 new in test/native.test.mjs with injected plugin fakes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
135 lines
5.6 KiB
JavaScript
135 lines
5.6 KiB
JavaScript
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { CapacitorBleTransport } from '../src/transport-native.js';
|
|
import { NativeGps } from '../src/gps-native.js';
|
|
import { isNativeApp } from '../src/platform.js';
|
|
import { NUS_SERVICE, NUS_WRITE, NUS_NOTIFY } from '../src/transport.js';
|
|
|
|
function fakeBle() {
|
|
const calls = [];
|
|
const ble = {
|
|
calls, onDisconnect: null, notify: null, enabled: true, writes: [],
|
|
async initialize(o) { calls.push(['initialize', o]); },
|
|
async isEnabled() { return ble.enabled; },
|
|
async requestEnable() { calls.push(['requestEnable']); ble.enabled = true; },
|
|
async requestDevice(o) { calls.push(['requestDevice', o]); return { deviceId: 'AA:BB' }; },
|
|
async connect(id, onDisconnect) { calls.push(['connect', id]); ble.onDisconnect = onDisconnect; },
|
|
async startNotifications(id, svc, ch, cb) { calls.push(['notify', id, svc, ch]); ble.notify = cb; },
|
|
async write(id, svc, ch, dv) {
|
|
calls.push(['write', id, svc, ch]);
|
|
ble.writes.push(Array.from(new Uint8Array(dv.buffer, dv.byteOffset, dv.byteLength)));
|
|
},
|
|
async disconnect(id) { calls.push(['disconnect', id]); },
|
|
};
|
|
return ble;
|
|
}
|
|
|
|
test('connect asks for the NUS service, connects and subscribes to the notify characteristic', async () => {
|
|
const ble = fakeBle();
|
|
const t = new CapacitorBleTransport(ble);
|
|
await t.connect();
|
|
assert.deepEqual(ble.calls[0], ['initialize', { androidNeverForLocation: true }]);
|
|
assert.deepEqual(ble.calls.find((c) => c[0] === 'requestDevice'), ['requestDevice', { services: [NUS_SERVICE] }]);
|
|
assert.deepEqual(ble.calls.find((c) => c[0] === 'notify'), ['notify', 'AA:BB', NUS_SERVICE, NUS_NOTIFY]);
|
|
assert.equal(t.connected(), true);
|
|
assert.equal(t.isConnected(), true);
|
|
});
|
|
|
|
test('Bluetooth switched off: the user is asked to enable it first', async () => {
|
|
const ble = fakeBle();
|
|
ble.enabled = false;
|
|
await new CapacitorBleTransport(ble).connect();
|
|
assert.ok(ble.calls.some((c) => c[0] === 'requestEnable'));
|
|
});
|
|
|
|
test('frames from the notify characteristic reach every listener as a DataView', async () => {
|
|
const ble = fakeBle();
|
|
const t = new CapacitorBleTransport(ble);
|
|
const got = [];
|
|
const a = (dv) => got.push(['a', dv.getUint8(0)]);
|
|
t.onFrame(a);
|
|
t.onFrame((dv) => got.push(['b', dv.getUint8(0)]));
|
|
await t.connect();
|
|
ble.notify(new DataView(new Uint8Array([0x88, 1]).buffer));
|
|
t.offFrame(a);
|
|
ble.notify(new DataView(new Uint8Array([0x83]).buffer));
|
|
assert.deepEqual(got, [['a', 0x88], ['b', 0x88], ['b', 0x83]]);
|
|
});
|
|
|
|
test('send writes the whole frame to the write characteristic, one at a time', async () => {
|
|
const ble = fakeBle();
|
|
let release;
|
|
const gate = new Promise((r) => { release = r; });
|
|
const order = [];
|
|
const write = ble.write;
|
|
ble.write = async (...args) => { order.push('start'); if (order.length === 1) await gate; await write(...args); order.push('end'); };
|
|
const t = new CapacitorBleTransport(ble);
|
|
await t.connect();
|
|
const sub = new Uint8Array([9, 1, 2, 3]).subarray(1); // a view, not the whole buffer
|
|
const p1 = t.send(new Uint8Array([7, 8]));
|
|
const p2 = t.send(sub);
|
|
await new Promise((r) => setTimeout(r, 5));
|
|
assert.deepEqual(order, ['start'], 'second write waits for the first');
|
|
release();
|
|
await Promise.all([p1, p2]);
|
|
assert.deepEqual(ble.writes, [[7, 8], [1, 2, 3]]);
|
|
assert.ok(ble.calls.filter((c) => c[0] === 'write').every((c) => c[3] === NUS_WRITE));
|
|
});
|
|
|
|
test('send before connect rejects', async () => {
|
|
await assert.rejects(new CapacitorBleTransport(fakeBle()).send(new Uint8Array([1])), /not connected/);
|
|
});
|
|
|
|
test('a dropped link reconnects and reports reconnecting, then connected', async () => {
|
|
const ble = fakeBle();
|
|
const t = new CapacitorBleTransport(ble, { firstDelayMs: 1 });
|
|
const status = [];
|
|
t.onStatus((s) => status.push(s));
|
|
await t.connect();
|
|
ble.onDisconnect('AA:BB');
|
|
assert.equal(t.connected(), false);
|
|
await new Promise((r) => setTimeout(r, 20));
|
|
assert.deepEqual(status, ['reconnecting', 'connected']);
|
|
assert.equal(t.connected(), true);
|
|
assert.equal(ble.calls.filter((c) => c[0] === 'connect').length, 2);
|
|
});
|
|
|
|
test('a user disconnect does not reconnect', async () => {
|
|
const ble = fakeBle();
|
|
const t = new CapacitorBleTransport(ble, { firstDelayMs: 1 });
|
|
await t.connect();
|
|
await t.disconnect();
|
|
ble.onDisconnect('AA:BB');
|
|
await new Promise((r) => setTimeout(r, 20));
|
|
assert.equal(ble.calls.filter((c) => c[0] === 'connect').length, 1);
|
|
assert.equal(t.connected(), false);
|
|
});
|
|
|
|
test('native GPS starts a background watcher and keeps the latest fix', async () => {
|
|
let opts, cb, removed = null;
|
|
const plugin = {
|
|
async addWatcher(o, c) { opts = o; cb = c; return 'w1'; },
|
|
async removeWatcher(o) { removed = o; },
|
|
};
|
|
const g = new NativeGps(plugin);
|
|
const fixes = [];
|
|
await g.start((f) => fixes.push(f));
|
|
assert.equal(opts.requestPermissions, true);
|
|
assert.equal(opts.stale, false);
|
|
assert.ok(opts.backgroundMessage, 'a background message turns on the foreground service');
|
|
assert.equal(g.latest(), null);
|
|
cb({ latitude: 50.9, longitude: 4.7, accuracy: 8 });
|
|
cb(undefined, Object.assign(new Error('denied'), { code: 'NOT_AUTHORIZED' }));
|
|
assert.deepEqual(g.latest(), { lat: 50.9, lon: 4.7, acc_m: 8 });
|
|
assert.deepEqual(fixes, [{ lat: 50.9, lon: 4.7, acc_m: 8 }]);
|
|
g.stop();
|
|
await new Promise((r) => setTimeout(r, 1));
|
|
assert.deepEqual(removed, { id: 'w1' });
|
|
});
|
|
|
|
test('isNativeApp follows Capacitor.isNativePlatform', () => {
|
|
assert.equal(isNativeApp({ isNativePlatform: () => true }), true);
|
|
assert.equal(isNativeApp({ isNativePlatform: () => false }), false);
|
|
assert.equal(isNativeApp(undefined), false);
|
|
});
|