feat(contacts): синхронный показ карточки + статус обмена
This commit is contained in:
@@ -36,6 +36,7 @@ class BleContactExchange(private val context: Context) {
|
||||
}
|
||||
|
||||
var onReceived: ((Long) -> Unit)? = null
|
||||
var onSent: ((Long) -> Unit)? = null
|
||||
var onError: ((String) -> Unit)? = null
|
||||
|
||||
private val main = Handler(Looper.getMainLooper())
|
||||
@@ -53,6 +54,7 @@ class BleContactExchange(private val context: Context) {
|
||||
|
||||
@Volatile private var selfId: Long = 0L
|
||||
@Volatile private var selfSession: String = ""
|
||||
@Volatile private var peerIdForWrite: Long = 0L
|
||||
@Volatile private var connecting = false
|
||||
@Volatile private var running = false
|
||||
|
||||
@@ -68,8 +70,9 @@ class BleContactExchange(private val context: Context) {
|
||||
startGattServer()
|
||||
}
|
||||
|
||||
fun connectTo(peerSession: String) {
|
||||
fun connectTo(peerSession: String, peerId: Long) {
|
||||
if (!running || connecting) return
|
||||
peerIdForWrite = peerId
|
||||
startScan(peerSession)
|
||||
}
|
||||
|
||||
@@ -313,6 +316,10 @@ class BleContactExchange(private val context: Context) {
|
||||
characteristic: BluetoothGattCharacteristic?,
|
||||
status: Int,
|
||||
) {
|
||||
if (status == BluetoothGatt.GATT_SUCCESS) {
|
||||
val id = peerIdForWrite
|
||||
if (id > 0L) emitSent(id)
|
||||
}
|
||||
gatt?.disconnect()
|
||||
}
|
||||
}
|
||||
@@ -321,6 +328,10 @@ class BleContactExchange(private val context: Context) {
|
||||
main.post { onReceived?.invoke(id) }
|
||||
}
|
||||
|
||||
private fun emitSent(id: Long) {
|
||||
main.post { onSent?.invoke(id) }
|
||||
}
|
||||
|
||||
private fun emitError(reason: String) {
|
||||
main.post { onError?.invoke(reason) }
|
||||
}
|
||||
|
||||
@@ -43,6 +43,7 @@ class MainActivity : FlutterActivity() {
|
||||
private var ble: BleContactExchange? = null
|
||||
private var pendingSelfId = 0L
|
||||
private var pendingSession = ""
|
||||
@Volatile private var exchangingEmitted = false
|
||||
|
||||
private companion object {
|
||||
const val LOG_TAG = "VpnBypass"
|
||||
@@ -212,6 +213,7 @@ class MainActivity : FlutterActivity() {
|
||||
NfcExchange.active = true
|
||||
NfcExchange.onServed = { onNfcServed() }
|
||||
seenPeers.clear()
|
||||
exchangingEmitted = false
|
||||
pendingSelfId = selfId
|
||||
pendingSession = session
|
||||
nfcCycling = true
|
||||
@@ -258,12 +260,19 @@ class MainActivity : FlutterActivity() {
|
||||
private fun startBle() {
|
||||
val exchange = ble ?: BleContactExchange(applicationContext).also {
|
||||
it.onReceived = { id -> onBleReceived(id) }
|
||||
it.onSent = { id -> onBleReceived(id) }
|
||||
it.onError = { reason -> onBleError(reason) }
|
||||
ble = it
|
||||
}
|
||||
exchange.start(pendingSelfId, pendingSession)
|
||||
}
|
||||
|
||||
private fun emitExchanging() {
|
||||
if (exchangingEmitted) return
|
||||
exchangingEmitted = true
|
||||
nfcEvents?.success(mapOf("event" to "exchanging"))
|
||||
}
|
||||
|
||||
private fun onBleReceived(id: Long) {
|
||||
if (id == NfcExchange.selfId || !seenPeers.add(id)) return
|
||||
nfcEvents?.success(mapOf("event" to "received", "id" to id))
|
||||
@@ -278,6 +287,7 @@ class MainActivity : FlutterActivity() {
|
||||
if (!NfcExchange.active) return@post
|
||||
nfcCycling = false
|
||||
nfcReaderDisable()
|
||||
emitExchanging()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -349,10 +359,9 @@ class MainActivity : FlutterActivity() {
|
||||
if (peer.id == NfcExchange.selfId) return@post
|
||||
nfcCycling = false
|
||||
nfcReaderDisable()
|
||||
ble?.connectTo(peer.session)
|
||||
if (seenPeers.add(peer.id)) {
|
||||
nfcEvents?.success(mapOf("event" to "received", "id" to peer.id))
|
||||
}
|
||||
emitExchanging()
|
||||
ble?.connectTo(peer.session, peer.id)
|
||||
nfcHandler.postDelayed({ onBleReceived(peer.id) }, 3000L)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user