forked from Blendman974/faceclaw
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,178 @@
|
||||
/** G2 wire protocol, ported from g2protocol/BleProtocol.java. No platform APIs. */
|
||||
export const G2_WRITE = '00002760-08c2-11e1-9073-0e8ac72e5401'
|
||||
export const G2_NOTIFY = '00002760-08c2-11e1-9073-0e8ac72e5402'
|
||||
export const G2_RENDER_NOTIFY = '00002760-08c2-11e1-9073-0e8ac72e6402'
|
||||
export const RING_NOTIFY = ['bae80011-4f05-4503-8e65-3af1f7329d1f', 'bae80013-4f05-4503-8e65-3af1f7329d1f']
|
||||
export const SID = { auth: 0x80, launch: 1, hub: 0xe0, settings: 9 } as const
|
||||
export function concat(...parts: Uint8Array[]): Uint8Array {
|
||||
const output = new Uint8Array(parts.reduce((n, p) => n + p.length, 0))
|
||||
let offset = 0; for (const part of parts) { output.set(part, offset); offset += part.length }; return output
|
||||
}
|
||||
function varint(value: number): Uint8Array {
|
||||
const bytes: number[] = []
|
||||
do { const next = value & 127; value >>>= 7; bytes.push(next | (value ? 128 : 0)) } while (value)
|
||||
return new Uint8Array(bytes)
|
||||
}
|
||||
export const integer = (field: number, value: number): Uint8Array => concat(varint(field * 8), varint(value))
|
||||
export const bytes = (field: number, value: Uint8Array): Uint8Array => concat(varint(field * 8 + 2), varint(value.length), value)
|
||||
// Outbound names/text here are fixed ASCII protocol identifiers.
|
||||
const string = (field: number, value: string): Uint8Array => bytes(field, new Uint8Array(Array.from(value, c => c.charCodeAt(0))))
|
||||
const wrap = (command: number, magic: number, field: number, body: Uint8Array) => concat(integer(1, command), integer(2, magic), bytes(field, body))
|
||||
export const authentication = (magic: number) => wrap(4, magic, 3, concat(integer(1, 1), integer(2, 4)))
|
||||
export const prelude = () => wrap(2, 156, 4, bytes(3, bytes(2, bytes(2, concat(integer(1, 0), integer(2, 0))))))
|
||||
export const heartbeat = (magic: number) => wrap(12, magic, 14, integer(1, 0))
|
||||
export const settingsQuery = (magic: number) => wrap(2, magic, 4, integer(1, 1))
|
||||
export const shutdown = (magic: number) => wrap(9, magic, 11, integer(1, 1))
|
||||
export const framebufferLease = (acquire: boolean) => concat(integer(1, 1), integer(2, 0), bytes(101, new Uint8Array([70, 67, 1, acquire ? 5 : 6, 0, 0])))
|
||||
export function createLayout(magic: number): Uint8Array {
|
||||
const geometry = concat(integer(1, 0), integer(2, 0), integer(3, 576), integer(4, 288))
|
||||
const text = concat(geometry, integer(9, 1), string(10, 'dashboard'), integer(11, 1), string(12, ' '))
|
||||
const image = concat(geometry, integer(5, 10), string(6, 'img00'))
|
||||
return wrap(0, magic, 3, concat(integer(1, 2), bytes(3, text), bytes(4, image), integer(5, 10000)))
|
||||
}
|
||||
export function imageFragment(magic: number, payload: Uint8Array, offset: number, size = 3800): Uint8Array {
|
||||
const fragment = payload.subarray(offset, offset + size)
|
||||
return wrap(3, magic, 5, concat(integer(1, 10), string(2, 'img00'), integer(3, 10), integer(4, payload.length),
|
||||
integer(5, 0), integer(6, Math.floor(offset / size)), integer(7, fragment.length), bytes(8, fragment)))
|
||||
}
|
||||
export function crc16(data: Uint8Array): number {
|
||||
let crc = 0xffff
|
||||
for (const byte of data) {
|
||||
crc ^= byte << 8
|
||||
for (let i = 0; i < 8; i++) crc = ((crc << 1) ^ ((crc & 0x8000) ? 0x1021 : 0)) & 0xffff
|
||||
}
|
||||
return crc
|
||||
}
|
||||
export function frameMessage(payload: Uint8Array, sid: number, flag: number, sequence: number, maxWrite = 240): Uint8Array[] {
|
||||
const chunkSize = Math.min(232, Math.floor(maxWrite) - 8)
|
||||
if (chunkSize < 12) throw new Error('Negotiated BLE write size is too small')
|
||||
const crc = crc16(payload), withCrc = concat(payload, new Uint8Array([crc & 255, crc >>> 8]))
|
||||
const count = Math.ceil(withCrc.length / chunkSize)
|
||||
if (count > 255) throw new Error('Protocol message exceeds the fragment limit')
|
||||
return Array.from({ length: count }, (_, i) => {
|
||||
const chunk = withCrc.subarray(i * chunkSize, (i + 1) * chunkSize)
|
||||
return concat(new Uint8Array([0xaa, 0x21, sequence & 255, chunk.length, count, i + 1, sid, flag]), chunk)
|
||||
})
|
||||
}
|
||||
export type ProtocolMessage = { sid: number; flag: number; payload: Uint8Array; command: number; magic: number }
|
||||
/** Handles split/coalesced envelopes and reassembles each link/sequence separately. */
|
||||
export class MessageReceiver {
|
||||
private streams = new Map<string, Uint8Array>()
|
||||
private messages = new Map<string, { chunks: Uint8Array[]; total: number; seen: number; size: number }>()
|
||||
clear(): void { this.streams.clear(); this.messages.clear() }
|
||||
receive(link: string, data: Uint8Array, now = Date.now()): ProtocolMessage[] {
|
||||
for (const [key, message] of this.messages) if (now - message.seen > 5000) this.messages.delete(key)
|
||||
let stream = concat(this.streams.get(link) ?? new Uint8Array(), data)
|
||||
const output: ProtocolMessage[] = []
|
||||
while (stream.length >= 8) {
|
||||
if (stream[0] !== 0xaa || ![0x21, 0x12].includes(stream[1])) { stream = stream.subarray(1); continue }
|
||||
const length = stream[3], count = stream[4], index = stream[5]
|
||||
if (stream.length < length + 8) break
|
||||
const sid = stream[6], flag = stream[7], key = `${link}:${stream[2]}:${sid}:${flag}`
|
||||
const chunk = stream.slice(8, 8 + length); stream = stream.subarray(length + 8)
|
||||
if (!count || !index || index > count) { this.messages.delete(key); continue }
|
||||
if (index === 1) this.messages.set(key, { chunks: [], total: count, seen: now, size: 0 })
|
||||
const message = this.messages.get(key)
|
||||
if (!message || message.total !== count || message.chunks.length + 1 !== index) { this.messages.delete(key); continue }
|
||||
message.chunks.push(chunk); message.size += chunk.length; message.seen = now
|
||||
if (message.size > 65536) { this.messages.delete(key); continue }
|
||||
if (index !== count) continue
|
||||
this.messages.delete(key)
|
||||
const complete = concat(...message.chunks)
|
||||
if (complete.length < 2) continue
|
||||
const payload = complete.subarray(0, -2), crc = complete[complete.length - 2] | (complete[complete.length - 1] << 8)
|
||||
if (crc16(payload) !== crc) continue
|
||||
try { output.push({ sid, flag, payload, command: readInteger(payload, 1, -1), magic: readInteger(payload, 2, -1) }) }
|
||||
catch { /* Malformed notifications cannot satisfy an acknowledgement. */ }
|
||||
}
|
||||
this.streams.set(link, stream.slice())
|
||||
return output
|
||||
}
|
||||
}
|
||||
function readVarint(data: Uint8Array, offset: number): [number, number] {
|
||||
let value = 0
|
||||
for (let i = 0; i < 5 && offset < data.length; i++) {
|
||||
const byte = data[offset++]; value += (byte & 127) * 2 ** (i * 7)
|
||||
if (!(byte & 128)) return [value, offset]
|
||||
}
|
||||
throw new Error('Truncated or oversized protobuf varint')
|
||||
}
|
||||
function field(data: Uint8Array, number: number): number | Uint8Array | undefined {
|
||||
let offset = 0
|
||||
while (offset < data.length) {
|
||||
let key: number; [key, offset] = readVarint(data, offset)
|
||||
const tag = key >>> 3, wire = key & 7
|
||||
if (!tag) throw new Error('Invalid protobuf field')
|
||||
if (wire === 0) {
|
||||
let value: number; [value, offset] = readVarint(data, offset)
|
||||
if (tag === number) return value
|
||||
} else {
|
||||
let length: number
|
||||
if (wire === 2) [length, offset] = readVarint(data, offset)
|
||||
else if (wire === 1) length = 8
|
||||
else if (wire === 5) length = 4
|
||||
else throw new Error('Unsupported protobuf wire type')
|
||||
if (offset + length > data.length) throw new Error('Truncated protobuf field')
|
||||
if (tag === number && wire === 2) return data.subarray(offset, offset + length)
|
||||
offset += length
|
||||
}
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
export function readInteger(data: Uint8Array, number: number, fallback = 0): number { const value = field(data, number); return typeof value === 'number' ? value : fallback }
|
||||
export function readBytes(data: Uint8Array, number: number): Uint8Array | undefined { const value = field(data, number); return value instanceof Uint8Array ? value : undefined }
|
||||
export function readString(data: Uint8Array, number: number): string { return Array.from(readBytes(data, number) ?? [], value => String.fromCharCode(value)).join('') }
|
||||
export function authenticationSucceeded(message: ProtocolMessage, magic: number): boolean {
|
||||
return message.sid === SID.auth && message.command === 4 && message.magic === magic && readBytes(message.payload, 3)?.length === 0
|
||||
}
|
||||
export type GlassesInput = { kind: 'sys-event' | 'list-click' | 'text-click'; eventType: number; eventSource: number; systemExitReasonCode: number; containerName: string; frameId: number }
|
||||
export function decodeGlassesInput(message: ProtocolMessage): GlassesInput | null {
|
||||
if (message.sid !== SID.hub || ![1, 6].includes(message.flag)) return null
|
||||
const events = readBytes(message.payload, 13); if (!events) return null
|
||||
for (const [field, kind, typeField] of [[1, 'list-click', 5], [2, 'text-click', 3], [3, 'sys-event', 1]] as const) {
|
||||
const event = readBytes(events, field)
|
||||
if (event) return { kind, eventType: readInteger(event, typeField), eventSource: field === 3 ? readInteger(event, 2) : 0,
|
||||
systemExitReasonCode: field === 3 ? readInteger(event, 4) : 0, containerName: field === 3 ? '' : readString(event, 2), frameId: 0 }
|
||||
}
|
||||
return null
|
||||
}
|
||||
export function decodeRingInput(data: Uint8Array): GlassesInput | null {
|
||||
let eventType: number | undefined
|
||||
if (data.length === 11 && data[0] === 0 && data[1] === 9 && data[2] === 0x61 && data[3] === 0)
|
||||
eventType = ({ 0: 9, 1: 0, 2: 3, 4: 1, 5: 2, 8: 10 } as Record<number, number>)[data[4]]
|
||||
if (data.length === 3 && data[0] === 255) {
|
||||
if (data[1] === 3 && data[2] === 32) eventType = 9
|
||||
if (data[1] === 4 && data[2] === 1) eventType = 0
|
||||
if (data[1] === 4 && data[2] === 2) eventType = 3
|
||||
if (data[1] === 5) eventType = data[2] <= 1 ? 2 : 1
|
||||
}
|
||||
return eventType === undefined ? null : { kind: 'sys-event', eventType, eventSource: 2, systemExitReasonCode: 0, containerName: '', frameId: 0 }
|
||||
}
|
||||
export function packGray4(gray: Uint8Array, width: number, height: number): Uint8Array {
|
||||
if (!Number.isInteger(width) || !Number.isInteger(height) || width < 1 || height < 1 || gray.length !== width * height) throw new Error('Invalid display frame')
|
||||
const stride = Math.ceil(width / 2), packed = new Uint8Array(stride * height)
|
||||
for (let y = 0; y < height; y++) for (let x = 0; x < width; x++) {
|
||||
const nibble = Math.min(15, (gray[y * width + x] + 8) >> 4)
|
||||
packed[y * stride + (x >> 1)] |= nibble << (x % 2 ? 0 : 4)
|
||||
}
|
||||
return packed
|
||||
}
|
||||
export function rle4(packed: Uint8Array): Uint8Array {
|
||||
const output = new Uint8Array(packed.length * 2)
|
||||
const nibble = (i: number) => (packed[i >> 1] >> (i % 2 ? 0 : 4)) & 15
|
||||
let used = 0, i = 0
|
||||
while (i < packed.length * 2) {
|
||||
const color = nibble(i); let end = i + 1
|
||||
while (end < packed.length * 2 && nibble(end) === color) end++
|
||||
let remaining = end - i
|
||||
while (remaining) {
|
||||
const count = Math.min(65535, remaining)
|
||||
if (count <= 15) output[used++] = (count << 4) | color
|
||||
else { output[used++] = color; if (count <= 255) output[used++] = count
|
||||
else { output[used++] = 0; output[used++] = count & 255; output[used++] = count >>> 8 } }
|
||||
remaining -= count
|
||||
}
|
||||
i = end
|
||||
}
|
||||
return output.slice(0, used)
|
||||
}
|
||||
@@ -0,0 +1,298 @@
|
||||
import * as protocol from './ble-protocol'
|
||||
import { hexToBytes } from '../util/hex-util'
|
||||
import { deviceAddressError } from './ios-peripheral-identity'
|
||||
|
||||
declare function setTimeout(callback: () => void, ms: number): number
|
||||
declare function clearTimeout(id: number): void
|
||||
export type SessionAddresses = { left: string; right: string; ring: string }
|
||||
export type TransportEvent = { kind: string; identifier?: string; characteristic?: string; data?: string; message?: string; state?: number }
|
||||
export interface SessionTransport {
|
||||
onEvent(listener: (event: TransportEvent) => void): () => void
|
||||
resolveDevices(addresses: SessionAddresses): Promise<Record<string, string>>
|
||||
connect(identifier: string): Promise<{ characteristics: string[]; maxWrite: number }>
|
||||
subscribe(identifier: string, characteristic: string): Promise<void>
|
||||
write(identifier: string, characteristic: string, bytes: Uint8Array): Promise<void>
|
||||
disconnect(identifier: string): void
|
||||
stopScan(): void
|
||||
}
|
||||
export type SessionState = { phase: 'disconnected' | 'connecting' | 'connected' | 'retrying' | 'disconnecting' | 'error';
|
||||
status: string; battery: number | null; ring: boolean; frames: number; capabilities: string; leftVersion: string; rightVersion: string }
|
||||
type PendingAck = { resolve: (message: protocol.ProtocolMessage) => void; reject: (error: Error) => void;
|
||||
timer: number | null; command: number; label: string }
|
||||
class AckTimeout extends Error {}
|
||||
|
||||
/** G2 session independent of the phone OS. Images go to L, control to R, as on Android. */
|
||||
export class GlassesSession {
|
||||
state: SessionState = { phase: 'disconnected', status: 'Preview only', battery: null, ring: false,
|
||||
frames: 0, capabilities: '', leftVersion: '', rightVersion: '' }
|
||||
private generation = 0
|
||||
private ids: Record<string, string> = {}
|
||||
private limits: Record<string, number> = {}
|
||||
private pending = new Map<string, PendingAck>()
|
||||
private receiver = new protocol.MessageReceiver()
|
||||
private writes = new Map<string, Promise<void>>()
|
||||
private magic = 100
|
||||
private sequence = 0x40
|
||||
private timer: number | null = null
|
||||
private retryTimer: number | null = null
|
||||
private retryCount = 0
|
||||
private wanted = false
|
||||
private addresses: SessionAddresses | null = null
|
||||
private latest: Uint8Array | null = null
|
||||
private displayed: Uint8Array | null = null
|
||||
private pumping = false
|
||||
private lastHeartbeat = 0
|
||||
private lastLease = 0
|
||||
private lastSettings = 0
|
||||
private charging = false
|
||||
private layoutCreated = false
|
||||
private off: () => void
|
||||
constructor(private readonly transport: SessionTransport,
|
||||
private readonly deflate: (data: Uint8Array) => Uint8Array,
|
||||
private readonly onState: (state: SessionState) => void,
|
||||
private readonly onInput: (input: protocol.GlassesInput) => void,
|
||||
private readonly log: (message: string) => void = () => {}) {
|
||||
this.off = transport.onEvent(event => this.receive(event))
|
||||
}
|
||||
private update(phase: SessionState['phase'], status: string): void {
|
||||
this.state = { ...this.state, phase, status }; this.log(status); this.onState({ ...this.state })
|
||||
}
|
||||
async start(addresses: SessionAddresses, retry = false): Promise<void> {
|
||||
if (['connecting', 'connected', 'disconnecting'].includes(this.state.phase)) return
|
||||
const invalid = deviceAddressError(addresses)
|
||||
if (invalid) { this.update('error', invalid); return }
|
||||
this.addresses = { ...addresses }; this.wanted = true
|
||||
if (!retry) this.retryCount = 0
|
||||
const generation = ++this.generation
|
||||
this.state = { ...this.state, capabilities: '', leftVersion: '', rightVersion: '', battery: null, ring: false }
|
||||
this.charging = false
|
||||
this.update('connecting', 'Finding configured devices…')
|
||||
try {
|
||||
const ids = await this.transport.resolveDevices(addresses); this.check(generation)
|
||||
this.ids = ids
|
||||
for (const role of ['right', 'left']) {
|
||||
this.update('connecting', `Connecting ${role} arm…`)
|
||||
const details = await this.transport.connect(ids[role]); this.check(generation)
|
||||
this.limits[role] = details.maxWrite
|
||||
if (!details.characteristics.includes(protocol.G2_WRITE) || !details.characteristics.includes(protocol.G2_NOTIFY)) throw new Error(`${role} arm is missing G2 communication characteristics.`)
|
||||
await this.transport.subscribe(ids[role], protocol.G2_NOTIFY); this.check(generation)
|
||||
}
|
||||
this.update('connecting', 'Authenticating… Accept pairing if iOS asks.')
|
||||
await Promise.all(['right', 'left'].map(async role => {
|
||||
try {
|
||||
const answer = await this.request(role, protocol.SID.auth, protocol.authentication, 'Security authentication', 30_000, 0)
|
||||
if (!protocol.authenticationSucceeded(answer, answer.magic)) throw new Error(`${role} arm rejected authentication.`)
|
||||
} catch (error) {
|
||||
if (!(error instanceof AckTimeout)) throw error
|
||||
// Older CFW may not implement the reply. A settings response below
|
||||
// still has to prove we have a working, compatible session.
|
||||
this.log(`${role} authentication unconfirmed; checking firmware response`)
|
||||
}
|
||||
}))
|
||||
this.check(generation)
|
||||
this.update('connecting', 'Checking glasses firmware…')
|
||||
const settings = await this.request('right', protocol.SID.settings, protocol.settingsQuery, 'Settings query')
|
||||
this.applySettings(settings)
|
||||
const caps = this.state.capabilities.split(/\s+/)
|
||||
if (!['img640', 'fbguard', 'wearnotify'].every(cap => caps.includes(cap)))
|
||||
throw new Error('The glasses do not report compatible Faceclaw firmware (img640, fbguard, wearnotify). Bluetooth is working, but this display requires the modified firmware. Flashing remains available on Android.')
|
||||
this.update('connecting', 'Starting glasses display…')
|
||||
await this.request('right', protocol.SID.launch, () => protocol.prelude(), 'App launch', 3500, 0x20, 156)
|
||||
await this.lease(true)
|
||||
await this.request('right', protocol.SID.hub, protocol.createLayout, 'Create display layout')
|
||||
this.check(generation); this.layoutCreated = true
|
||||
this.lastHeartbeat = this.lastLease = this.lastSettings = Date.now()
|
||||
this.displayed = null; this.retryCount = 0
|
||||
this.update('connected', 'Glasses connected')
|
||||
this.schedule()
|
||||
if (addresses.ring && ids.ring) {
|
||||
try {
|
||||
const ring = await this.transport.connect(ids.ring); this.check(generation)
|
||||
const notifications = protocol.RING_NOTIFY.filter(uuid => ring.characteristics.includes(uuid))
|
||||
if (!notifications.length) throw new Error('No R1 gesture notifications found')
|
||||
for (const uuid of notifications) await this.transport.subscribe(ids.ring, uuid)
|
||||
this.check(generation); this.state.ring = true; this.update('connected', 'Glasses and ring connected')
|
||||
} catch (error) {
|
||||
if (generation !== this.generation) return
|
||||
this.transport.disconnect(ids.ring)
|
||||
this.update('connected', `Glasses connected; ring unavailable: ${this.message(error)}`)
|
||||
}
|
||||
} else if (addresses.ring) this.update('connected', 'Glasses connected; configured ring was not found')
|
||||
} catch (error) {
|
||||
if (generation !== this.generation) return
|
||||
this.fail(error, retry)
|
||||
}
|
||||
}
|
||||
private check(generation: number): void { if (generation !== this.generation) throw new Error('Connection cancelled') }
|
||||
private nextMagic(): number {
|
||||
for (let i = 0; i < 156; i++) {
|
||||
const magic = this.magic; this.magic = this.magic === 255 ? 100 : this.magic + 1
|
||||
if (magic !== 156 && ![...this.pending.keys()].some(key => key.endsWith(`:${magic}`))) return magic
|
||||
}
|
||||
throw new Error('All message identifiers are in use')
|
||||
}
|
||||
private request(role: string, sid: number, build: (magic: number) => Uint8Array, label: string,
|
||||
timeout = 3500, flag = 0x20, fixedMagic?: number): Promise<protocol.ProtocolMessage> {
|
||||
const magic = fixedMagic ?? this.nextMagic(), payload = build(magic), key = `${sid}:${magic}`
|
||||
const generation = this.generation
|
||||
return new Promise((resolve, reject) => {
|
||||
const pending: PendingAck = { resolve, reject, timer: null, command: protocol.readInteger(payload, 1, -1), label }
|
||||
this.pending.set(key, pending)
|
||||
this.send(role, sid, flag, payload).then(() => {
|
||||
if (this.pending.get(key) !== pending || generation !== this.generation) return
|
||||
pending.timer = setTimeout(() => {
|
||||
this.pending.delete(key); reject(new AckTimeout(`${label} was not acknowledged by the glasses.`))
|
||||
}, timeout)
|
||||
}).catch(error => {
|
||||
if (this.pending.get(key) !== pending) return
|
||||
this.pending.delete(key); if (pending.timer) clearTimeout(pending.timer); reject(error)
|
||||
})
|
||||
})
|
||||
}
|
||||
private send(role: string, sid: number, flag: number, payload: Uint8Array): Promise<void> {
|
||||
const identifier = this.ids[role], generation = this.generation
|
||||
if (!identifier) return Promise.reject(new Error(`No ${role} device connected`))
|
||||
const frames = protocol.frameMessage(payload, sid, flag, this.sequence++, this.limits[role])
|
||||
const previous = this.writes.get(identifier) ?? Promise.resolve()
|
||||
const work = previous.catch(() => {}).then(async () => {
|
||||
for (const frame of frames) { this.check(generation); await this.transport.write(identifier, protocol.G2_WRITE, frame) }
|
||||
})
|
||||
// A whole message owns the write queue; heartbeat packets cannot split an image message.
|
||||
this.writes.set(identifier, work)
|
||||
return work
|
||||
}
|
||||
private lease(acquire: boolean): Promise<void[]> {
|
||||
return Promise.all(['right', 'left'].map(role => this.send(role, protocol.SID.settings, 0x20, protocol.framebufferLease(acquire))))
|
||||
}
|
||||
private receive(event: TransportEvent): void {
|
||||
if (!Object.values(this.ids).includes(event.identifier ?? '')) return
|
||||
if (event.kind === 'disconnected') {
|
||||
if (event.identifier === this.ids.ring) {
|
||||
this.state.ring = false
|
||||
if (this.state.phase === 'connected') this.update('connected', 'Glasses connected; ring disconnected')
|
||||
} else if (['connected', 'connecting'].includes(this.state.phase)) this.fail(new Error(event.message || 'Glasses disconnected'), true)
|
||||
return
|
||||
}
|
||||
if (event.kind !== 'notification' || !event.data) return
|
||||
try {
|
||||
const data = hexToBytes(event.data)
|
||||
if (event.identifier === this.ids.ring) {
|
||||
if (!protocol.RING_NOTIFY.includes(event.characteristic ?? '')) return
|
||||
const input = protocol.decodeRingInput(data); if (input) this.onInput(input); return
|
||||
}
|
||||
if (event.characteristic !== protocol.G2_NOTIFY) return
|
||||
for (const message of this.receiver.receive(event.identifier!, data)) {
|
||||
if (![1, 6].includes(message.flag)) {
|
||||
// A master arm can relay the other arm's ACK; magic is allocated
|
||||
// across BOTH links, matching Android's communicator.
|
||||
const key = `${message.sid}:${message.magic}`, pending = this.pending.get(key)
|
||||
if (pending) {
|
||||
this.pending.delete(key); if (pending.timer) clearTimeout(pending.timer)
|
||||
pending.resolve(message)
|
||||
}
|
||||
}
|
||||
if (message.sid === protocol.SID.settings) this.applySettings(message)
|
||||
if (event.identifier === this.ids.right) {
|
||||
const input = protocol.decodeGlassesInput(message)
|
||||
if (input) {
|
||||
if (input.kind === 'sys-event' && [5, 6, 7].includes(input.eventType) && this.state.phase === 'connected') {
|
||||
// Respect an explicit glasses-side exit; the user can reconnect.
|
||||
void this.stop(); continue
|
||||
}
|
||||
this.onInput(input)
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) { this.log(`Ignored malformed BLE notification: ${this.message(error)}`) }
|
||||
}
|
||||
private applySettings(message: protocol.ProtocolMessage): void {
|
||||
const values = protocol.readBytes(message.payload, 4)
|
||||
const capabilities = protocol.readString(message.payload, 100)
|
||||
if (capabilities) this.state.capabilities = capabilities
|
||||
if (values) {
|
||||
const left = protocol.readString(values, 5), right = protocol.readString(values, 6)
|
||||
if (left) this.state.leftVersion = left
|
||||
if (right) this.state.rightVersion = right
|
||||
const battery = protocol.readInteger(values, 12, -1), charging = protocol.readInteger(values, 13, -1)
|
||||
if (battery >= 0 && battery <= 100) this.state.battery = battery
|
||||
if (charging >= 0) this.charging = charging > 0
|
||||
}
|
||||
this.onState({ ...this.state })
|
||||
}
|
||||
setFrame(gray: Uint8Array): void {
|
||||
if (this.state.phase !== 'connected') return
|
||||
this.latest = protocol.packGray4(gray, 640, 480); this.schedule(0)
|
||||
}
|
||||
private schedule(delay = 1000): void {
|
||||
if (delay === 0 && this.timer !== null) { clearTimeout(this.timer); this.timer = null }
|
||||
if (this.timer !== null || this.pumping || this.state.phase !== 'connected') return
|
||||
this.timer = setTimeout(() => { this.timer = null; void this.pump() }, delay)
|
||||
}
|
||||
private async pump(): Promise<void> {
|
||||
if (this.pumping || this.state.phase !== 'connected') return
|
||||
this.pumping = true; const generation = this.generation
|
||||
try {
|
||||
const now = Date.now()
|
||||
if (now - this.lastHeartbeat >= 4000) {
|
||||
await this.request('right', protocol.SID.hub, protocol.heartbeat, 'Heartbeat', 1500); this.lastHeartbeat = Date.now()
|
||||
}
|
||||
if (now - this.lastLease >= 45_000) { await this.lease(true); this.lastLease = Date.now() }
|
||||
if (now - this.lastSettings >= (this.charging ? 30_000 : 300_000)) {
|
||||
const settings = await this.request('right', protocol.SID.settings, protocol.settingsQuery, 'Battery query')
|
||||
this.applySettings(settings); this.lastSettings = Date.now()
|
||||
}
|
||||
const frame = this.latest; this.latest = null
|
||||
if (frame && !this.charging && (!this.displayed || !frame.every((value, i) => value === this.displayed![i]))) {
|
||||
const payload = protocol.concat(new Uint8Array([6]), this.deflate(protocol.rle4(frame)))
|
||||
if (payload.length > 165888) throw new Error('Compressed frame exceeds the glasses image buffer')
|
||||
for (let offset = 0; offset < payload.length; offset += 3800) {
|
||||
await this.request('left', protocol.SID.hub, magic => protocol.imageFragment(magic, payload, offset), 'Display frame')
|
||||
this.check(generation); this.lastHeartbeat = Date.now()
|
||||
}
|
||||
this.displayed = frame; this.state.frames++; this.onState({ ...this.state })
|
||||
this.log(`Display frame ${this.state.frames} acknowledged (${payload.length} bytes)`)
|
||||
} else if (frame && this.charging) this.latest = frame
|
||||
} catch (error) { if (generation === this.generation) this.fail(error, true) }
|
||||
finally { if (generation === this.generation) { this.pumping = false; this.schedule(this.latest && !this.charging ? 0 : 1000) } }
|
||||
}
|
||||
private reset(): void {
|
||||
++this.generation
|
||||
if (this.timer !== null) clearTimeout(this.timer)
|
||||
if (this.retryTimer !== null) clearTimeout(this.retryTimer)
|
||||
this.timer = this.retryTimer = null; this.pumping = false
|
||||
const pending = [...this.pending.values()]; this.pending.clear()
|
||||
for (const request of pending) { if (request.timer) clearTimeout(request.timer); request.reject(new Error('Connection ended')) }
|
||||
this.receiver.clear(); this.writes.clear(); this.latest = this.displayed = null
|
||||
}
|
||||
private closeLinks(): void {
|
||||
const ids = this.ids; this.ids = {}; this.limits = {}
|
||||
for (const identifier of Object.values(ids)) this.transport.disconnect(identifier)
|
||||
this.transport.stopScan(); this.layoutCreated = false; this.state.ring = false
|
||||
}
|
||||
private fail(error: unknown, retry: boolean): void {
|
||||
const message = this.message(error); this.reset(); this.closeLinks()
|
||||
if (retry && this.wanted && this.addresses && this.retryCount < 5) {
|
||||
const delay = Math.min(30_000, 2000 * 2 ** this.retryCount++)
|
||||
this.update('retrying', `${message} Retrying in ${delay / 1000}s…`)
|
||||
this.retryTimer = setTimeout(() => { this.retryTimer = null; if (this.wanted && this.addresses) void this.start(this.addresses, true) }, delay)
|
||||
} else { this.wanted = false; this.update('error', message) }
|
||||
}
|
||||
async stop(): Promise<void> {
|
||||
this.wanted = false
|
||||
const cleanup = this.layoutCreated && this.state.phase === 'connected'
|
||||
this.update('disconnecting', 'Disconnecting…'); this.reset()
|
||||
try {
|
||||
if (cleanup) {
|
||||
if (this.state.capabilities.split(/\s+/).includes('cleanup11'))
|
||||
await this.request('left', protocol.SID.hub, magic => protocol.imageFragment(magic, new Uint8Array([11]), 0), 'Display cleanup', 1200)
|
||||
else {
|
||||
await this.request('right', protocol.SID.hub, protocol.shutdown, 'Close display', 1200)
|
||||
await this.lease(false)
|
||||
}
|
||||
}
|
||||
} catch (error) { this.log(`Disconnect cleanup: ${this.message(error)}`) }
|
||||
finally { this.reset(); this.closeLinks(); this.update('disconnected', 'Preview only') }
|
||||
}
|
||||
dispose(): void { void this.stop().finally(() => this.off()) }
|
||||
private message(error: unknown): string { return error instanceof Error ? error.message : String(error) }
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import { classifyAdvertisement, hasEvenManufacturerSignature, type EvenAdvertisement } from './even-advertisement'
|
||||
import { hexToBytes } from '../util/hex-util'
|
||||
|
||||
export type IosAdvertisement = { identifier: string; name: string; manufacturerData: string; rssi: number; connectable: boolean }
|
||||
export type IosDevice = EvenAdvertisement & { identifier: string }
|
||||
|
||||
/** Only a full vendor-advertised MAC may bind a manually entered address. */
|
||||
export function identifyIosPeripheral(raw: IosAdvertisement): IosDevice | null {
|
||||
if (!/^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i.test(raw.identifier)) return null
|
||||
if (!hasEvenManufacturerSignature(hexToBytes(raw.manufacturerData))) return null
|
||||
const candidate = classifyAdvertisement({ ...raw, address: '', source: 'scan', bonded: false,
|
||||
txPower: null, seenAtMs: Date.now() })
|
||||
if (!candidate?.embeddedMac) return null
|
||||
return { ...candidate, address: candidate.embeddedMac, embeddedMacMismatch: false, identifier: raw.identifier.toUpperCase() }
|
||||
}
|
||||
|
||||
export function deviceAddressError(addresses: { left: string; right: string; ring: string }): string | null {
|
||||
for (const role of ['right', 'left', 'ring'] as const) {
|
||||
const address = addresses[role]
|
||||
if (role === 'ring' && !address) continue
|
||||
if (!/^[0-9A-F]{2}(?::[0-9A-F]{2}){5}$/.test(address)) return `${role === 'ring' ? 'Ring' : role === 'left' ? 'Left arm' : 'Right arm'} MAC address is invalid.`
|
||||
}
|
||||
const values = Object.values(addresses).filter(Boolean)
|
||||
return new Set(values).size === values.length ? null : 'Each device must have a different MAC address.'
|
||||
}
|
||||
@@ -1,4 +1,8 @@
|
||||
import { Dialogs, type ImageSource } from '@nativescript/core'
|
||||
import { Dialogs, File, knownFolders, path, type ImageSource } from '@nativescript/core'
|
||||
import { iosBluetooth } from '../native/ios-bluetooth'
|
||||
import { GlassesSession, type SessionState } from './glasses-session'
|
||||
import { loadDeviceAddresses } from './device-addresses'
|
||||
import { deviceAddressError } from './ios-peripheral-identity'
|
||||
import { createLauncherWindow, LAUNCHER_SURFACE_ID } from '../apps/launcher/launcher-app'
|
||||
import { CalculatorLayer } from '../apps/calculator/calculator'
|
||||
import { MathCoordinator } from '../apps/calculator/math/coordinator'
|
||||
@@ -10,7 +14,7 @@ import { makeInputEvent, type InputEventPayload } from '../ui/gestures'
|
||||
import { noopLayerActions, type LayerActions } from '../ui/layers'
|
||||
import { MenuLayer } from '../ui/menu'
|
||||
import { createInProcessWindow } from '../ui/shell/in-process-window'
|
||||
import { shell, type ShellWindow } from '../ui/shell/shell'
|
||||
import { shell, rawInputEventToInputEvent, type ShellWindow } from '../ui/shell/shell'
|
||||
import { appViewportRect, SIDEBAR_WIDTH, sidebarStripVisible } from '../ui/shell/geometry'
|
||||
import { DISPLAY_MODE_VALUES, displayModeLabel, displayModeSetting, onAnySettingChanged,
|
||||
previewColorSetting, timeFormatSetting, verticalPositionSetting } from '../ui/dashboard-settings'
|
||||
@@ -27,6 +31,11 @@ export class IosPreviewController {
|
||||
private inputQueue: Promise<void> = Promise.resolve()
|
||||
private lastLayout = ''
|
||||
private prompting = false
|
||||
private session: GlassesSession | null = null
|
||||
private resumeConnection = false
|
||||
private suspendedDisconnect: Promise<void> = Promise.resolve()
|
||||
private logLines: string[] = []
|
||||
private logTimer: ReturnType<typeof setTimeout> | null = null
|
||||
private readonly actions: LayerActions = {
|
||||
...noopLayerActions,
|
||||
requestRender: () => this.requestShellRender(),
|
||||
@@ -38,7 +47,8 @@ export class IosPreviewController {
|
||||
}
|
||||
|
||||
constructor(private readonly onFrame: (image: ImageSource, focus: string) => void,
|
||||
private readonly onError: (message: string) => void) {
|
||||
private readonly onError: (message: string) => void,
|
||||
private readonly onConnectionState: (state: SessionState) => void = () => {}) {
|
||||
this.compositor.configureSurface('shell', { x: 0, y: 0, width: 640, height: 480, zOrder: 1, transparency: 'color-key' })
|
||||
shell.configure({
|
||||
actions: this.actions,
|
||||
@@ -78,8 +88,14 @@ export class IosPreviewController {
|
||||
shell.foregroundWindow()?.requestRender()
|
||||
this.requestShellRender()
|
||||
console.log('[ios-preview] Main screen active')
|
||||
if (this.resumeConnection) {
|
||||
this.resumeConnection = false
|
||||
void this.suspendedDisconnect.then(() => { if (this.active) void this.connect() })
|
||||
}
|
||||
}
|
||||
pause(): void {
|
||||
this.resumeConnection = !!this.session && ['connected', 'connecting', 'retrying'].includes(this.session.state.phase)
|
||||
if (this.session) this.suspendedDisconnect = this.session.stop()
|
||||
this.active = false
|
||||
this.offSettings?.(); this.offSettings = null
|
||||
if (this.clockTimer) clearInterval(this.clockTimer)
|
||||
@@ -119,7 +135,9 @@ export class IosPreviewController {
|
||||
this.compositor.submitSurfaceFrame('shell', image.pixels, { x: 0, y: 0, width: 640, height: 480 })
|
||||
this.compositor.setUnderlayDim(1, shell.underlayDim())
|
||||
}
|
||||
const image = previewPixels(this.compositor.composite(), 640, 480, previewColorSetting.get() === 'green')
|
||||
const pixels = this.compositor.composite()
|
||||
this.session?.setFrame(pixels)
|
||||
const image = previewPixels(pixels, 640, 480, previewColorSetting.get() === 'green')
|
||||
this.onFrame(image, `${shell.foregroundWindow()?.title ?? 'Apps'} · ${shell.getFocus() === 'sidebar' ? 'App switcher' : 'App'}`)
|
||||
} catch (error) { this.fail(error) }
|
||||
}, 33)
|
||||
@@ -203,6 +221,42 @@ export class IosPreviewController {
|
||||
displayModeSetting.set(values[(values.indexOf(displayModeSetting.get()) + 1) % values.length])
|
||||
}
|
||||
get displayModeLabel(): string { return displayModeLabel(displayModeSetting.get()) }
|
||||
get connectionState(): SessionState | null { return this.session?.state ?? null }
|
||||
get connectionDetails(): string {
|
||||
const state = this.connectionState
|
||||
return state ? `${state.status}\nL: ${state.leftVersion || 'unknown'}\nR: ${state.rightVersion || 'unknown'}\nBattery: ${state.battery ?? '?'}%\nRing: ${state.ring ? 'connected' : 'disconnected'}\nFrames acknowledged: ${state.frames}\n${state.capabilities}\n\n${this.logLines.slice(-12).join('\n')}` : 'Preview only. Configure devices, then connect.'
|
||||
}
|
||||
async connect(): Promise<void> {
|
||||
if (!this.active) return
|
||||
const addresses = loadDeviceAddresses(), error = deviceAddressError(addresses)
|
||||
if (error) { this.onError(error); return }
|
||||
if (!this.session) {
|
||||
const { deflate } = require('pako') as { deflate: (data: Uint8Array) => Uint8Array }
|
||||
this.session = new GlassesSession(iosBluetooth(), deflate, state => {
|
||||
this.onConnectionState(state)
|
||||
if (state.phase === 'connected') this.requestShellRender()
|
||||
}, input => {
|
||||
this.inputQueue = this.inputQueue.then(async () => {
|
||||
if (!this.active) return
|
||||
await shell.receiveInput(rawInputEventToInputEvent(input)); this.requestShellRender()
|
||||
this.logBluetooth(`Input ${input.eventType} source ${input.eventSource}`)
|
||||
}).catch(error => this.fail(error))
|
||||
}, message => this.logBluetooth(message))
|
||||
}
|
||||
await this.session.start(addresses)
|
||||
}
|
||||
async disconnect(): Promise<void> { this.resumeConnection = false; await this.session?.stop() }
|
||||
private logBluetooth(message: string): void {
|
||||
const line = `${new Date().toISOString()} ${message}`
|
||||
console.log(`[ios-ble] ${line}`); this.logLines.push(line)
|
||||
if (this.logLines.length > 200) this.logLines.shift()
|
||||
if (this.logTimer !== null) return
|
||||
this.logTimer = setTimeout(() => {
|
||||
this.logTimer = null
|
||||
try { File.fromPath(path.join(knownFolders.documents().path, 'bluetooth.log')).writeTextSync(this.logLines.join('\n')) }
|
||||
catch (error) { console.warn(`Bluetooth log: ${error}`) }
|
||||
}, 500)
|
||||
}
|
||||
async typeIntoApp(): Promise<void> {
|
||||
if (this.prompting || !shell.foregroundWindow()?.receiveTextInput) return
|
||||
this.prompting = true
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
import { ApplicationSettings } from '@nativescript/core'
|
||||
import { identifyIosPeripheral, type IosAdvertisement, type IosDevice } from '../g2/ios-peripheral-identity'
|
||||
import { hexToBytes } from '../util/hex-util'
|
||||
|
||||
declare const FaceclawBluetooth: any
|
||||
type NativeEvent = { kind: string; id?: number; error?: string; details?: ConnectionDetails; state?: number;
|
||||
identifier?: string; characteristic?: string; data?: string; message?: string } & Partial<IosAdvertisement>
|
||||
export type ConnectionDetails = { characteristics: string[]; maxWrite: number }
|
||||
export type BluetoothEvent = NativeEvent | { kind: 'device'; device: IosDevice }
|
||||
type Pending = { resolve: (details: ConnectionDetails) => void; reject: (error: Error) => void; timer: ReturnType<typeof setTimeout> }
|
||||
|
||||
/** One central manager per app, with asynchronous main-queue GATT operations. */
|
||||
export class IosBluetooth {
|
||||
private readonly native = FaceclawBluetooth.new()
|
||||
private readonly listeners = new Set<(event: BluetoothEvent) => void>()
|
||||
private readonly pending = new Map<number, Pending>()
|
||||
private readonly advertisements = new Map<string, IosAdvertisement>()
|
||||
readonly devices = new Map<string, IosDevice>()
|
||||
private requestId = 0
|
||||
private scanTimer: ReturnType<typeof setTimeout> | null = null
|
||||
state = 0
|
||||
scanning = false
|
||||
constructor() {
|
||||
this.native.eventHandler = (json: string) => {
|
||||
try { this.receive(JSON.parse(json)) } catch (error) { console.error(`[ios-ble] ${error}`) }
|
||||
}
|
||||
}
|
||||
onEvent(listener: (event: BluetoothEvent) => void): () => void {
|
||||
this.listeners.add(listener); return () => { this.listeners.delete(listener) }
|
||||
}
|
||||
private emit(event: BluetoothEvent): void { for (const listener of [...this.listeners]) listener(event) }
|
||||
private receive(event: NativeEvent): void {
|
||||
if (event.kind === 'completion') {
|
||||
const pending = this.pending.get(event.id!)
|
||||
if (pending) {
|
||||
this.pending.delete(event.id!); clearTimeout(pending.timer)
|
||||
if (event.error) pending.reject(new Error(event.error)); else pending.resolve(event.details!)
|
||||
}
|
||||
return
|
||||
}
|
||||
if (event.kind === 'state') this.state = event.state!
|
||||
if (event.kind === 'advertisement') {
|
||||
const previous = this.advertisements.get(event.identifier!)
|
||||
const raw: IosAdvertisement = { identifier: event.identifier!, name: event.name || previous?.name || '',
|
||||
manufacturerData: event.manufacturerData || previous?.manufacturerData || '', rssi: event.rssi!, connectable: event.connectable !== false }
|
||||
// Bound the cache even in a crowded radio environment.
|
||||
if (this.advertisements.size >= 512 && !this.advertisements.has(raw.identifier)) this.advertisements.delete(this.advertisements.keys().next().value!)
|
||||
this.advertisements.set(raw.identifier, raw)
|
||||
const device = identifyIosPeripheral(raw)
|
||||
if (device) {
|
||||
this.devices.set(device.address, device)
|
||||
ApplicationSettings.setString(`ios.ble.peripheral.${device.address}`, JSON.stringify({ identifier: device.identifier, role: device.role }))
|
||||
this.emit({ kind: 'device', device })
|
||||
}
|
||||
return
|
||||
}
|
||||
this.emit(event)
|
||||
}
|
||||
ensureReady(): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const finish = (error?: Error) => { clearTimeout(timer); off(); error ? reject(error) : resolve() }
|
||||
const off = this.onEvent(event => {
|
||||
if (event.kind !== 'state') return
|
||||
const state = (event as NativeEvent).state
|
||||
if (state === 5) finish()
|
||||
else if (state === 2) finish(new Error('Bluetooth is unavailable on this device. Use the physical iPhone.'))
|
||||
else if (state === 3) finish(new Error('Allow Faceclaw to use Bluetooth in iPhone Settings.'))
|
||||
else if (state === 4) finish(new Error('Turn on Bluetooth, then try again.'))
|
||||
})
|
||||
const timer = setTimeout(() => finish(new Error('Waiting for Bluetooth permission timed out. Try again after allowing Bluetooth.')), 30_000)
|
||||
this.native.initializeBluetooth()
|
||||
})
|
||||
}
|
||||
async startScan(durationMs = 12_000): Promise<void> {
|
||||
await this.ensureReady()
|
||||
this.stopScan(); this.scanning = true; this.native.scan(true)
|
||||
this.scanTimer = setTimeout(() => this.stopScan(), durationMs)
|
||||
this.emit({ kind: 'scan-started' })
|
||||
}
|
||||
stopScan(): void {
|
||||
if (this.scanTimer) clearTimeout(this.scanTimer)
|
||||
this.scanTimer = null; this.native.scan(false)
|
||||
const wasScanning = this.scanning; this.scanning = false
|
||||
if (wasScanning) this.emit({ kind: 'scan-stopped' })
|
||||
}
|
||||
async resolveDevices(addresses: { left: string; right: string; ring: string }): Promise<Record<string, string>> {
|
||||
await this.ensureReady()
|
||||
const result: Record<string, string> = {}
|
||||
for (const role of ['left', 'right', 'ring'] as const) {
|
||||
const address = addresses[role]
|
||||
if (!address) continue
|
||||
const live = this.devices.get(address)
|
||||
if (live && live.role !== role) throw new Error(`${address} advertises as ${live.role}, not ${role}. Check Configure devices.`)
|
||||
try {
|
||||
const saved = JSON.parse(ApplicationSettings.getString(`ios.ble.peripheral.${address}`, '{}'))
|
||||
if (saved.role === role && typeof saved.identifier === 'string') result[role] = saved.identifier
|
||||
} catch { /* A fresh scan can recover malformed settings. */ }
|
||||
}
|
||||
const missing = () => (['left', 'right'] as const).filter(role => !result[role])
|
||||
if (!missing().length && (!addresses.ring || result.ring)) return result
|
||||
await this.startScan(15_000)
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const off = this.onEvent(event => {
|
||||
if (event.kind === 'device') {
|
||||
const device = (event as { device: IosDevice }).device
|
||||
for (const role of ['left', 'right', 'ring'] as const) {
|
||||
if (addresses[role] === device.address && device.role === role) result[role] = device.identifier
|
||||
}
|
||||
if (!missing().length && (!addresses.ring || result.ring)) { off(); this.stopScan(); resolve() }
|
||||
} else if (event.kind === 'scan-stopped') {
|
||||
off()
|
||||
if (missing().length) reject(new Error(`Could not find the ${missing().join(' and ')} arm. Wake the glasses, disconnect other apps, and scan again.`))
|
||||
else resolve() // An optional missing ring must not block the glasses.
|
||||
}
|
||||
})
|
||||
})
|
||||
return result
|
||||
}
|
||||
private operation(identifier: string, call: (id: number) => void, timeoutMs = 10_000): Promise<ConnectionDetails> {
|
||||
const id = ++this.requestId
|
||||
return new Promise((resolve, reject) => {
|
||||
const timer = setTimeout(() => {
|
||||
this.pending.delete(id); this.native.disconnect(identifier)
|
||||
reject(new Error('Bluetooth operation timed out. Wake the device and try connecting again.'))
|
||||
}, timeoutMs)
|
||||
this.pending.set(id, { resolve, reject, timer })
|
||||
try { call(id) } catch (error) { clearTimeout(timer); this.pending.delete(id); reject(error) }
|
||||
})
|
||||
}
|
||||
connect(identifier: string): Promise<ConnectionDetails> { return this.operation(identifier, id => this.native.connectRequestId(identifier, id), 20_000) }
|
||||
async subscribe(identifier: string, characteristic: string): Promise<void> {
|
||||
await this.operation(identifier, id => this.native.subscribeCharacteristicRequestId(identifier, characteristic, id))
|
||||
}
|
||||
async write(identifier: string, characteristic: string, bytes: Uint8Array): Promise<void> {
|
||||
const copy = new Uint8Array(bytes)
|
||||
const data = NSData.dataWithBytesLength(interop.handleof(copy.buffer), copy.byteLength)
|
||||
await this.operation(identifier, id => this.native.writeCharacteristicDataRequestId(identifier, characteristic, data, id))
|
||||
}
|
||||
disconnect(identifier: string): void { this.native.disconnect(identifier) }
|
||||
forget(address: string): void { ApplicationSettings.remove(`ios.ble.peripheral.${address}`); this.devices.delete(address) }
|
||||
}
|
||||
let instance: IosBluetooth | null = null
|
||||
export function iosBluetooth(): IosBluetooth { return instance ??= new IosBluetooth() }
|
||||
export { hexToBytes }
|
||||
@@ -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