forked from Blendman974/faceclaw
Refactor iOS version to use a bunch more shared code
This commit is contained in:
1 parent
c3a0ba9f56
commit
4a194e37bb
40 files changed
+1874
-2605
No files matched your search
@@ -182,8 +182,14 @@ class IosBleCentral(private val platform: ProtocolPlatform = IosProtocolPlatform
|
||||
central = CBCentralManager(delegate = delegate, queue = queue)
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the scan/state listener and creates the central if needed, so a listener that
|
||||
* only waits for readiness (no scan) still receives the state report; an existing central's
|
||||
* current state is re-reported to the new listener on the main queue.
|
||||
*/
|
||||
fun setScanListener(listener: IosBleScanListener?) {
|
||||
scanListener = listener
|
||||
if (listener != null && !closed) ensureCentral()
|
||||
}
|
||||
|
||||
/** Starts (or keeps) scanning for all peripherals with duplicates; events reach the scan listener on main. */
|
||||
|
||||
@@ -15,6 +15,11 @@ interface IosAncsListener {
|
||||
fun onRelayFrame(data: NSData)
|
||||
}
|
||||
|
||||
/** Audio packets for the NativeScript side as NSData (a Kotlin ByteArray crosses per byte). Main queue. */
|
||||
interface IosAudioPacketListener {
|
||||
fun onAudioPacket(data: NSData, arm: String, arrivalMs: Long)
|
||||
}
|
||||
|
||||
/**
|
||||
* ObjC-facing facade over the shared [GlassesSessionCore] for the NativeScript app: the iOS
|
||||
* counterpart of Android's FaceclawBleCommunicator with the same method names. GATT goes
|
||||
@@ -119,6 +124,16 @@ class IosGlassesSession internal constructor(
|
||||
|
||||
fun stopG2AudioCapture() = core.stopG2AudioCapture()
|
||||
|
||||
/** As [startG2AudioCapture], with packets delivered as NSData on the main queue. */
|
||||
fun startG2AudioCaptureNsData(listener: IosAudioPacketListener): Boolean =
|
||||
core.startG2AudioCapture(NsDataAudioListener(listener))
|
||||
|
||||
/** The right arm's usable write payload (ATT MTU minus the 3-byte header), or 20 when unknown. */
|
||||
fun rightWriteLimit(): Int {
|
||||
val mtu = try { central?.negotiatedMtu(rightIdentifier) ?: 0 } catch (t: Throwable) { 0 }
|
||||
return if (mtu > 3) mtu - 3 else 20
|
||||
}
|
||||
|
||||
fun isAudioCaptureActive(): Boolean = core.isAudioCaptureActive()
|
||||
|
||||
fun setFaceclawWakeLeaseEnabled(enabled: Boolean): Boolean = core.setFaceclawWakeLeaseEnabled(enabled)
|
||||
@@ -237,6 +252,14 @@ class IosGlassesSession internal constructor(
|
||||
|
||||
internal fun coreForTests(): GlassesSessionCore = core
|
||||
|
||||
private class NsDataAudioListener(private val delegate: IosAudioPacketListener) : FaceclawAudioPacketListener {
|
||||
override fun onAudioPacket(data: ByteArray?, arm: String?, arrivalMs: Long) {
|
||||
val packet = (data ?: ByteArray(0)).data()
|
||||
val label = arm ?: ""
|
||||
dispatch_async(dispatch_get_main_queue()) { delegate.onAudioPacket(packet, label, arrivalMs) }
|
||||
}
|
||||
}
|
||||
|
||||
private class MainQueueAudioListener(private val delegate: FaceclawAudioPacketListener) : FaceclawAudioPacketListener {
|
||||
override fun onAudioPacket(data: ByteArray?, arm: String?, arrivalMs: Long) {
|
||||
dispatch_async(dispatch_get_main_queue()) { delegate.onAudioPacket(data, arm, arrivalMs) }
|
||||
|
||||
Reference in new issue
Block a user