#oasis_code
1 messages ยท Page 1 of 1 (latest)
๐ 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.
Hello there
Hmmm seems like you are already handling disconnects as shown here: https://docs.stripe.com/terminal/payments/connect-reader?terminal-sdk-platform=android&reader-type=usb#handle-disconnects ?
That is the proper way to handle these disconnects if they occur
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
Sure if you can share your code then we can look -- I'll loop in a teammate who is more familiar with Kotlin
have to send a txt, dont have nitro haha
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
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
oh okay, i will look into it, maybe i can make it work
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.
in my logs
That seems like it is the original connection failing?
Like is it ever connecting successfully here?
nope that is the problem
it is finding the correct reader but i cant connect to it
Gotcha I misunderstood initially -- thought you were having trouble reconnecting after a disconnect.
i think that is now working haha, but yes the first one is the problem
Did you grant permissions to access the reader in your app?
you mean the usb?
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?
just security, i have the correct id in my code ๐
Oh okay those object IDs aren't sensitive
No one can do anything with them without your secret key
Just fyi
oh that makes sense, haha
paranoid i guess
but wait, do i need the secret key also in my android application?
No
okay okay
You use the Connection Token from your server to initialize the SDK
What version of the Android Terminal SDK are you using?
4.2.0
๐ stepping in for my teammate
Can you try using our example app? https://github.com/stripe/stripe-terminal-android/tree/master/Example
If you still run into connection issues, there's likely something wrong with the reader