#aneeb_code

1 messages · Page 1 of 1 (latest)

flat thornBOT
#

đź‘‹ Welcome to your new thread!

⏲️ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.

⏱️ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.

đź”— This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1423573339452084324

📝 Have more to share? Add more details, code, screenshots, videos, etc. below.

errant spoke
#

Hi there, can you tell me more about " discovery flow doesn’t always stick to that saved reader." ?

wet bear
#

okay sure let me check that out

#

also i provide the details what i have already did and what i want to achieve

#

I’m seeing an intermittent issue where the app discovers multiple Bluetooth readers at discovery time and sometimes connects to a different reader than the one previously paired. We store the reader’s serial in local storage, but the discovery flow doesn’t always stick to that saved reader.

flat thornBOT
errant spoke
#

" connects to a different reader than the one previously paired. " -> did your app call terminal.connectReader to connect to that reader?

wet bear
#

we call connect card reader after ddiscovering the card reader

proven birch
#

hi! I'm taking over this thread.

wet bear
#

private val discoveryListener by lazy {
object : DiscoveryListener {

        override fun onUpdateDiscoveredReaders(readers: List<Reader>) {

            // 1) Publish list to UI
            _discoveredReaders.value = readers

            /**
            No Available Readers
             **/
            if (readers.isEmpty()) {
                updateTerminalState(TerminalState.Exception("No readers available!"))
            } else {
                /**
                Discovered Readers
                 **/
                updateTerminalState(
                    TerminalState.Discovered(
                        message = "Discovered: ${
                            readers.map {
                                "DeviceName=>${it.deviceType.deviceName},Simulated=>${it.isSimulated},SN=>${it.serialNumber},Reader Name=>${it.deviceType.name}IsUSBConnected=>${it.isUsbConnected}"
                            }
                        }"
                    )
                )
            }

            val savedSerial = getPreferredReader()

            if (savedSerial != null) {
                // Next time → look for saved reader
                val matched = readers.firstOrNull { it.serialNumber.equals(savedSerial, true) }
                if (matched != null) {
                    connectReader(matched)
                } else {
                    updateTerminalState(TerminalState.Exception("Previously connected reader ($savedSerial) not found. Please make it available."))
                }
            }
        }
    }
}

this is the code i am using for the discovery and connecting to card reader

proven birch
wet bear
#

yes i follow the same, my question is why it is going on discovering the the new card reader when it is already paired to one

proven birch
#

can you share your code that calls terminal.connectReader?

wet bear
#

private val discoveryListener by lazy {
object : DiscoveryListener {

        override fun onUpdateDiscoveredReaders(readers: List<Reader>) {

            // 1) Publish list to UI
            _discoveredReaders.value = readers

            /**
            No Available Readers
             **/
            if (readers.isEmpty()) {
                updateTerminalState(TerminalState.Exception("No readers available!"))
            } else {
                /**
                Discovered Readers
                 **/
                updateTerminalState(
                    TerminalState.Discovered(
                        message = "Discovered: ${
                            readers.map {
                                "DeviceName=>${it.deviceType.deviceName},Simulated=>${it.isSimulated},SN=>${it.serialNumber},Reader Name=>${it.deviceType.name}IsUSBConnected=>${it.isUsbConnected}"
                            }
                        }"
                    )
                )
            }

            val savedSerial = getPreferredReader()

            if (savedSerial != null) {
                // Next time → look for saved reader
                val matched = readers.firstOrNull { it.serialNumber.equals(savedSerial, true) }
                if (matched != null) {
                    connectReader(matched)
                } else {
                    updateTerminalState(TerminalState.Exception("Previously connected reader ($savedSerial) not found. Please make it available."))
                }
            }
        }
    }
}
#

private fun connectReader(reader: Reader) {
val connectionConfig = ConnectionConfiguration.BluetoothConnectionConfiguration(
locationId = userInfo.value?.locationId.default,
autoReconnectOnUnexpectedDisconnect = true,
bluetoothReaderListener = mobileReaderListener
)
Terminal.getInstance().connectReader(
reader = reader,
config = connectionConfig,
connectionCallback = readerCallback
)
}

proven birch
#

well you can see in your code that you are selecting the reader yourself:

 Terminal.getInstance().connectReader(
            reader = reader,

so next steps is to debug your code to see where reader is set.

wet bear
#

brother connect part is not my concern right now, i am concerned about the discovery part that it is discovering different readers other than the already discovered and paired reader

#

override fun onUpdateDiscoveredReaders(readers: List<Reader>) {

            val savedSerial = getPreferredReader()

            if (savedSerial != null) {
                // Next time → look for saved reader
                val matched = readers.firstOrNull { it.serialNumber.equals(savedSerial, true) }
                if (matched != null) {
                    connectReader(matched)
                } else {
                    updateTerminalState(TerminalState.Exception("Previously connected reader ($savedSerial) not found. Please make it available."))
                }
            }
        }

see this code for reference

proven birch
#

in this case, can you share your code that is calling the discoverReaders method? I don't see it in everything you shared so far.

wet bear
#

/**
Discovering readers via Bluetooth
* **/
@SuppressLint("MissingPermission")
private fun discoverBluetoothReaders() {
Terminal
.getInstance()
.discoverReaders(
config = bluetoothDiscoveryConfig,
discoveryListener = discoveryListener,
callback = discoveryCallback
)
}

proven birch
#

thanks! your code looks correct.

Expected: if a saved reader serial exists, discovery should prefer — and immediately attempt to connect to — that reader only.
why do you have that expectation? was it working like this before? or did you see it mentioned in the documentation somewhere?

#

if you want to reconnect to a previously connected reader, that's a logic you should implement on your end.

wet bear
#

can you help me with that, what logic should i write for that

proven birch
#

what logic do you want exactly? you want to always connect to the same reader (and ignore the other readers)?

wet bear
#

yes

proven birch
#

using the serial number (like you already do) seem like a good idea.