web_lib

Common web application libraries
git clone https://radroots.dev/git/web_lib.git
Log | Files | Refs | LICENSE

commit 3e4e939ae0416f726d969b55eff7507e112777fe
parent a2c5be0a7c0a1fb899a6099098814ff7ece1109d
Author: triesap <137732411+triesap@users.noreply.github.com>
Date:   Fri, 23 Aug 2024 13:50:54 +0000

client: add capacitor bluetooth-le  api

Diffstat:
Mclient/package.json | 1+
Aclient/src/capacitor/bluetooth-le.ts | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mclient/src/capacitor/index.ts | 9++++++++-
Mclient/src/types.ts | 12++++++++++++
4 files changed, 81 insertions(+), 1 deletion(-)

diff --git a/client/package.json b/client/package.json @@ -20,6 +20,7 @@ "@capacitor/share": "^6.0.0", "@capacitor/splash-screen": "^6.0.0", "@capacitor/status-bar": "^6.0.0", + "@radroots/capacitor-bluetooth-le": "workspace:*", "@radroots/capacitor-date-picker": "workspace:*", "@radroots/capacitor-secure-storage": "workspace:*", "@radroots/capacitor-wifi": "workspace:*" diff --git a/client/src/capacitor/bluetooth-le.ts b/client/src/capacitor/bluetooth-le.ts @@ -0,0 +1,59 @@ +import { BleClient } from '@radroots/capacitor-bluetooth-le'; +import type { IClientBluetoothLe, IClientBluetoothLeScanResult } from '../types'; + +export class CapacitorClientBluetoothLe implements IClientBluetoothLe { + private _scan_results: IClientBluetoothLeScanResult[] = []; + + private update_scan_results(result: IClientBluetoothLeScanResult): void { + const existing_result_i = this._scan_results.findIndex(i => i.device.deviceId === result.device.deviceId); + if (existing_result_i !== -1) this._scan_results[existing_result_i] = result; + else this._scan_results.push(result); + } + + public async enabled(): Promise<boolean> { + try { + const res = await BleClient.isEnabled(); + return res; + } catch (e) { + return false; + }; + } + + public async initialize(): Promise<boolean> { + try { + await BleClient.initialize({ + androidNeverForLocation: false + }); + if (!this.enabled()) await BleClient.requestEnable(); + return true; + } catch (e) { + return false; + }; + } + + public async scan(): Promise<boolean> { + try { + await BleClient.requestLEScan( + { allowDuplicates: true }, + (result) => this.update_scan_results(result) + ); + return true; + } catch (e) { + return false; + }; + } + + public async select_device(device_id: string): Promise<IClientBluetoothLeScanResult | undefined> { + try { + const res = this._scan_results.find(i => i.device.deviceId === device_id); + return res; + } catch (e) { }; + } + + public async select_devices(): Promise<IClientBluetoothLeScanResult[] | undefined> { + try { + const res = this._scan_results.length ? this._scan_results : []; + return res; + } catch (e) { }; + } +}; +\ No newline at end of file diff --git a/client/src/capacitor/index.ts b/client/src/capacitor/index.ts @@ -1,7 +1,8 @@ import { Capacitor } from "@capacitor/core"; -import type { IClient, IClientBrowser, IClientDatePicker, IClientDevice, IClientDialog, IClientGeolocation, IClientHaptics, IClientHttp, IClientKeystore, IClientNetwork, IClientPlatform, IClientPreferences, IClientShare, IClientWifi, IClientWindow } from "../types"; +import type { IClient, IClientBluetoothLe, IClientBrowser, IClientDatePicker, IClientDevice, IClientDialog, IClientGeolocation, IClientHaptics, IClientHttp, IClientKeystore, IClientNetwork, IClientPlatform, IClientPreferences, IClientShare, IClientWifi, IClientWindow } from "../types"; import { parse_platform } from "../utils"; +import { CapacitorClientBluetoothLe } from "./bluetooth-le"; import { CapacitorClientBrowser } from "./browser"; import { CapacitorClientDatePicker } from "./date-picker"; import { CapacitorClientDevice } from "./device"; @@ -17,6 +18,7 @@ import { CapacitorClientWifi } from "./wifi"; import { CapacitorClientWindow } from "./window"; export class ClientCapacitor implements IClient { + ble: IClientBluetoothLe; private _platform: IClientPlatform = parse_platform(Capacitor.getPlatform()); private _keystore: IClientKeystore = new CapacitorClientKeystore(); private _device: IClientDevice = new CapacitorClientDevice(); @@ -31,6 +33,7 @@ export class ClientCapacitor implements IClient { private _geo: IClientGeolocation = new CapacitorClientGeolocation(); private _http: IClientHttp = new CapacitorClientHttp(); private _window: IClientWindow = new CapacitorClientWindow(); + private _ble: IClientBluetoothLe = new CapacitorClientBluetoothLe(); public get platform() { return this._platform; @@ -87,4 +90,8 @@ export class ClientCapacitor implements IClient { public get window() { return this._window; } + + public get blue() { + return this._ble; + } }; \ No newline at end of file diff --git a/client/src/types.ts b/client/src/types.ts @@ -1,4 +1,5 @@ import { type BatteryInfo, type DeviceInfo } from '@capacitor/device'; +import { type ScanResult } from '@radroots/capacitor-bluetooth-le'; import { ConnectToWifiResult, type GetCurrentWifiResult, type ScanWifiResult } from '@radroots/capacitor-wifi'; export type IClient = { @@ -16,6 +17,7 @@ export type IClient = { geo: IClientGeolocation; http: IClientHttp; window: IClientWindow; + ble: IClientBluetoothLe; }; export type IClientPlatform = `androiď` | `ios` | `web`; @@ -161,4 +163,14 @@ export type IClientWindow = { status_hide(): Promise<void>; status_show(): Promise<void>; status_style(style: "light" | "dark"): Promise<void>; +}; + +export type IClientBluetoothLeScanResult = ScanResult; + +export type IClientBluetoothLe = { + enabled(): Promise<boolean>; + initialize(): Promise<boolean>; + scan(): Promise<boolean>; + select_device(device_id: string): Promise<IClientBluetoothLeScanResult | undefined>; + select_devices(): Promise<IClientBluetoothLeScanResult[] | undefined>; }; \ No newline at end of file