#oasis_code

1 messages ยท Page 1 of 1 (latest)

oak wolfBOT
#

๐Ÿ‘‹ 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/1346225710032752701

๐Ÿ“ Have more to share? Add more details, code, screenshots, videos, etc. below.

wispy marlin
#

Hello there

#

That is the proper way to handle these disconnects if they occur

mild marten
#

yes, honestly kotlin is a new language for me so the learning curve is pretty steep. i already make a loop with a timeout to reconnect it, but i dont really know why i can't connect to the reader. i can send you my code (unfortunately there is some chat code inside). but i dont know why the error occurs every time and i cant connect to it

wispy marlin
#

Sure if you can share your code then we can look -- I'll loop in a teammate who is more familiar with Kotlin

mild marten
#
private fun discoverUSBReader() {
        val config: DiscoveryConfiguration = UsbDiscoveryConfiguration()
        Log.d(TAG, "Starting discoverUSB...")

        if (ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_FINE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
                this,
                Manifest.permission.ACCESS_COARSE_LOCATION
            ) != PackageManager.PERMISSION_GRANTED
        ) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            Log.d(TAG, "Permission Error")
            return
        }
        Terminal.getInstance().discoverReaders(config, object : DiscoveryListener {
            override fun onUpdateDiscoveredReaders(readers: List<Reader>) {
                if (readers.isNotEmpty()) {  // โœ… Fixed the check
                    connectToReader(readers[0])
                }
                Log.d(TAG, "USB Readers: ${readers}")
            }
        }, object : Callback {
            override fun onSuccess() {
                Log.d(TAG, "โœ… USB Reader found and ready")
            }

            override fun onFailure(e: TerminalException) {
                Log.e(TAG, "โŒ USB Reader discovery failed: ${e.message}")
            }
        })
    }

#

discoverUsbReader gives me my reader so that is working, but as soon as i want to connect to it, i get the mentioned error message

wispy marlin
#

Hmm I don't see you handling onReaderReconnectStarted anywhere?

#

That is what will fire from an unexpected disconnect

#

And you need to reconnect the reader at that point

#

Oh sorry, we do handle the reconnect automatically at that point

#

But you sould then listen for onReaderReconnectSucceeded

mild marten
#

oh okay, i will look into it, maybe i can make it work

mild marten
#

i added the terminalListerer

private val terminalListener = object : TerminalListener {

        fun onUnexpectedReaderDisconnect(reader: Reader) {
            Log.e(TAG, "Reader unexpectedly disconnected: ${reader.serialNumber}")

            if (reader.batteryLevel != null) {
                Log.e(TAG, "Battery Level Before Disconnect: ${reader.batteryLevel}%")
            }

            
            retryUSBConnection()
        }

         fun onReaderReconnectStarted(reader: Reader, cancelReconnect: Cancelable, reason: DisconnectReason) {
            Log.w(TAG, "๐Ÿ”„ Reader reconnecting... Reason: $reason")

            
            if (reason == DisconnectReason.CRITICALLY_LOW_BATTERY) {
                Log.e(TAG, "๐Ÿšจ Reader has low battery, stopping reconnection.")
                cancelReconnect.cancel(object : Callback {
                    override fun onSuccess() {
                        Log.d(TAG, "Reader reconnection cancelled.")
                    }
                    override fun onFailure(e: TerminalException) {
                        Log.e(TAG, "Failed to cancel reader reconnection: ${e.message}")
                    }
                })
            } else {
                Log.d(TAG, "Allowing reader to reconnect automatically.")
            }
        }

        fun onReaderReconnectSucceeded(reader: Reader) {
            Log.d(TAG, "โœ… Reader successfully reconnected: ${reader.serialNumber}")
            connectionRetryCount = 0 // Reset retry attempts
        }
    }
#
            context,
            LogLevel.VERBOSE,
            object : ConnectionTokenProvider {
                override fun fetchConnectionToken(callback: ConnectionTokenCallback) {
                    callback.onSuccess(fetchTokenFromServer())  // Fetch token from your backend
                }
            },
            terminalListener
        )
#

but i still get

Failed to connect USB reader: USB unexpectedly disconnected during operation, reconnecting.
wispy marlin
#

Where do you see that exactly?

#

On your PoS?

#

Or in your logs?

mild marten
#

in my logs

wispy marlin
#

That seems like it is the original connection failing?

#

Like is it ever connecting successfully here?

mild marten
#

nope that is the problem

#

it is finding the correct reader but i cant connect to it

wispy marlin
#

Gotcha I misunderstood initially -- thought you were having trouble reconnecting after a disconnect.

mild marten
#

i think that is now working haha, but yes the first one is the problem

wispy marlin
#

Did you grant permissions to access the reader in your app?

mild marten
#

you mean the usb?

wispy marlin
#

Hmm actually you are clearly able to discover the reader based on the above so that shouldn't be the issue...

#

Oh wait wait wait

#

In the code you shared before you have location commented out?

mild marten
#

just security, i have the correct id in my code ๐Ÿ˜„

wispy marlin
#

Oh okay those object IDs aren't sensitive

#

No one can do anything with them without your secret key

#

Just fyi

mild marten
#

oh that makes sense, haha

#

paranoid i guess

#

but wait, do i need the secret key also in my android application?

wispy marlin
#

No

mild marten
#

okay okay

wispy marlin
#

You use the Connection Token from your server to initialize the SDK

#

What version of the Android Terminal SDK are you using?

mild marten
#

4.2.0

oak wolfBOT
tall burrow
#

๐Ÿ‘‹ stepping in for my teammate