mirror of
https://git.undefined-dev.fr/Blendman974/faceclaw.git
synced 2026-11-29 13:03:11 +00:00
iOS porting: core bluetooth communication with the glasses
This commit is contained in:
1 parent
99793a37a7
commit
46c93c9421
14 files changed
+1182
-8
No files matched your search
@@ -0,0 +1,68 @@
|
||||
import { Button, Color, GridLayout, Label, Page, ScrollView, StackLayout, TextField } from '@nativescript/core'
|
||||
import { loadDeviceAddresses, normalizeMacAddress, saveDeviceAddresses } from '../g2/device-addresses'
|
||||
import { deviceAddressError, type IosDevice } from '../g2/ios-peripheral-identity'
|
||||
import { iosBluetooth } from '../native/ios-bluetooth'
|
||||
|
||||
export function createConfigureDevicesPage(): Page {
|
||||
const page = new Page(); page.actionBarHidden = true
|
||||
const root = new GridLayout(); root.rows = 'auto,*,auto'; root.padding = 16; page.content = root
|
||||
const title = new Label(); title.text = 'Configure devices'; title.fontSize = 24; title.fontWeight = 'bold'; root.addChild(title)
|
||||
const scroll = new ScrollView(); GridLayout.setRow(scroll, 1); root.addChild(scroll)
|
||||
const form = new StackLayout(); scroll.content = form
|
||||
const text = (value: string, size = 15) => {
|
||||
const label = new Label(); label.text = value; label.fontSize = size; label.textWrap = true; label.marginTop = 12; form.addChild(label); return label
|
||||
}
|
||||
text('Enter the G2 arm MAC addresses and optional R1 ring address. Scan once so this iPhone can find the matching devices. Saving does not connect.')
|
||||
const stored = loadDeviceAddresses()
|
||||
const fields = {} as Record<'left' | 'right' | 'ring', TextField>
|
||||
for (const role of ['right', 'left', 'ring'] as const) {
|
||||
text(role === 'ring' ? 'R1 ring MAC (optional)' : `${role === 'left' ? 'Left' : 'Right'} arm MAC`)
|
||||
const field = new TextField(); field.text = stored[role]; field.hint = 'AA:BB:CC:DD:EE:FF'
|
||||
field.autocorrect = false; field.autocapitalizationType = 'allcharacters'; field.fontSize = 18
|
||||
field.padding = 10; field.backgroundColor = new Color('#ffffff'); field.accessibilityLabel = `${role} MAC address`
|
||||
field.accessibilityIdentifier = `device-address-${role}`; form.addChild(field); fields[role] = field
|
||||
}
|
||||
const status = text('Addresses are saved only when you tap Save.'); status.color = new Color('#285c45')
|
||||
const button = (label: string, action: () => void) => {
|
||||
const view = new Button(); view.text = label; view.fontSize = 15; view.marginTop = 12; view.on('tap', action); return view
|
||||
}
|
||||
let off: (() => void) | null = null
|
||||
let closed = false
|
||||
const found = new StackLayout()
|
||||
const rows = new Map<string, Button>()
|
||||
const showDevice = (device: IosDevice) => {
|
||||
if (device.connectable === false) return
|
||||
let row = rows.get(device.address)
|
||||
if (!row) {
|
||||
row = button('', () => { fields[device.role].text = device.address; status.text = `${device.role} address selected. Tap Save to keep it.` })
|
||||
row.textWrap = true; row.fontSize = 13; rows.set(device.address, row); found.addChild(row)
|
||||
}
|
||||
row.text = `${device.role === 'ring' ? 'Ring' : device.role === 'left' ? 'Left arm' : 'Right arm'} · ${device.address}\n${device.serial || device.name} · ${device.rssi ?? '?'} dBm`
|
||||
}
|
||||
const scan = button('Scan for devices', () => {
|
||||
for (const field of Object.values(fields)) field.dismissSoftInput()
|
||||
const ble = iosBluetooth()
|
||||
off?.(); off = ble.onEvent(event => {
|
||||
if (closed) return
|
||||
if (event.kind === 'device' && 'device' in event) showDevice(event.device)
|
||||
if (event.kind === 'scan-stopped') { scan.isEnabled = true; status.text = rows.size ? 'Tap a device to fill its address, then Save.' : 'No matching devices found. Wake the glasses and disconnect other apps, then scan again.' }
|
||||
})
|
||||
rows.clear(); found.removeChildren(); scan.isEnabled = false; status.text = 'Scanning… Allow Bluetooth if iOS asks.'
|
||||
void ble.startScan().catch(error => { if (!closed) { status.text = String(error.message ?? error); scan.isEnabled = true } })
|
||||
})
|
||||
form.addChild(scan); form.addChild(found)
|
||||
const footer = new GridLayout(); footer.columns = '*,*'; GridLayout.setRow(footer, 2); root.addChild(footer)
|
||||
const close = () => { closed = true; off?.(); off = null; iosBluetooth().stopScan(); page.closeModal() }
|
||||
const back = button('Back', close); footer.addChild(back)
|
||||
const save = button('Save', () => {
|
||||
const addresses = { right: normalizeMacAddress(fields.right.text), left: normalizeMacAddress(fields.left.text), ring: normalizeMacAddress(fields.ring.text) }
|
||||
const error = deviceAddressError(addresses)
|
||||
if (error) { status.text = error; return }
|
||||
saveDeviceAddresses(addresses)
|
||||
for (const role of ['left', 'right', 'ring'] as const) { fields[role].text = addresses[role]; fields[role].dismissSoftInput() }
|
||||
status.text = 'Saved device addresses. Return to the main screen and choose Connect.'
|
||||
})
|
||||
GridLayout.setColumn(save, 1); footer.addChild(save)
|
||||
page.on('unloaded', () => { closed = true; off?.(); off = null; iosBluetooth().stopScan() })
|
||||
return page
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
import { Application, Button, Color, GridLayout, Image, Label, Page, StackLayout, type TouchGestureEventData } from '@nativescript/core'
|
||||
import { Application, Button, Color, Dialogs, GridLayout, Image, Label, Page, StackLayout, type TouchGestureEventData } from '@nativescript/core'
|
||||
import { createConfigureDevicesPage } from './config-page.ios'
|
||||
import { IosPreviewController } from '../g2/ios-preview-controller'
|
||||
import { PhoneGestureRecognizer } from './phone-gestures'
|
||||
import { onAnySettingChanged, previewColorSetting } from '../ui/dashboard-settings'
|
||||
import { loadDeviceAddresses } from '../g2/device-addresses'
|
||||
import { deviceAddressError } from '../g2/ios-peripheral-identity'
|
||||
|
||||
export function createMainPage(): Page {
|
||||
const page = new Page()
|
||||
@@ -13,10 +16,13 @@ export function createMainPage(): Page {
|
||||
header.columns = '*,auto'; header.padding = '12 20'
|
||||
const title = new Label()
|
||||
title.text = 'Faceclaw'; title.fontSize = 25; title.fontWeight = 'bold'
|
||||
header.addChild(title)
|
||||
const heading = new StackLayout(); heading.addChild(title); header.addChild(heading)
|
||||
const state = new Label()
|
||||
state.text = 'Preview only'; state.fontSize = 14; state.verticalAlignment = 'middle'
|
||||
GridLayout.setColumn(state, 1); header.addChild(state); root.addChild(header)
|
||||
state.text = 'Preview only'; state.fontSize = 12; heading.addChild(state)
|
||||
const devices = new Button()
|
||||
devices.text = 'Devices'; devices.accessibilityLabel = 'Configure devices'; devices.fontSize = 14; devices.padding = '10 12'
|
||||
devices.on('tap', () => { void openDevices() })
|
||||
GridLayout.setColumn(devices, 1); header.addChild(devices); root.addChild(header)
|
||||
const body = new GridLayout()
|
||||
GridLayout.setRow(body, 1); root.addChild(body)
|
||||
const mirror = new Image()
|
||||
@@ -52,7 +58,21 @@ export function createMainPage(): Page {
|
||||
mirror.imageSource = image; focus.text = selectedTab === 'ring' ? 'Ring' : label
|
||||
}, message => {
|
||||
errorLabel.text = message; errorLabel.visibility = 'visible'; selectTab('settings')
|
||||
}, connection => {
|
||||
state.text = connection.phase === 'connected' ? `Connected${connection.battery !== null ? ` · ${connection.battery}%` : ''}${connection.ring ? ' · R1' : ''}`
|
||||
: ({ disconnected: 'Preview only', connecting: 'Connecting…', retrying: 'Reconnecting…', disconnecting: 'Disconnecting…', error: 'Connection failed · Devices for details' })[connection.phase] || connection.phase
|
||||
})
|
||||
async function openDevices(): Promise<void> {
|
||||
const connected = ['connected', 'connecting', 'retrying'].includes(controller.connectionState?.phase ?? '')
|
||||
const choice = await Dialogs.action({ title: 'Devices', message: controller.connectionState?.status ?? 'Preview only', cancelButtonText: 'Cancel',
|
||||
actions: ['Configure devices', connected ? 'Disconnect' : 'Connect', 'Connection details'] })
|
||||
if (choice === 'Configure devices') {
|
||||
await controller.disconnect()
|
||||
page.showModal(createConfigureDevicesPage(), { fullscreen: true, closeCallback: () => {} })
|
||||
} else if (choice === 'Connect') await controller.connect()
|
||||
else if (choice === 'Disconnect') await controller.disconnect()
|
||||
else if (choice === 'Connection details') await Dialogs.alert({ title: 'Connection details', message: controller.connectionDetails, okButtonText: 'OK' })
|
||||
}
|
||||
const padInput = new PhoneGestureRecognizer(gesture => controller.gesture(gesture, selectedTab === 'ring' ? 'ring' : 'watch'))
|
||||
const mirrorInput = new PhoneGestureRecognizer((gesture, x, y) => {
|
||||
const size = mirror.getActualSize()
|
||||
@@ -128,10 +148,16 @@ export function createMainPage(): Page {
|
||||
}
|
||||
const resume = () => controller.resume()
|
||||
let offSettings: (() => void) | null = null
|
||||
let firstLoad = true
|
||||
page.on('loaded', () => {
|
||||
Application.on(Application.suspendEvent, pause); Application.on(Application.resumeEvent, resume)
|
||||
offSettings?.(); offSettings = onAnySettingChanged(() => { sizeButton.text = controller.displayModeLabel })
|
||||
selectTab(selectedTab); controller.resume()
|
||||
if (firstLoad) {
|
||||
firstLoad = false
|
||||
// Match Android's reconnect-on-launch behavior once addresses are saved.
|
||||
if (!deviceAddressError(loadDeviceAddresses())) void controller.connect()
|
||||
}
|
||||
})
|
||||
page.on('unloaded', () => {
|
||||
pause(); offSettings?.(); offSettings = null
|
||||
|
||||
Reference in new issue
Block a user