Skip to content

Instantly share code, notes, and snippets.

@Sardorbekcyber
Created September 28, 2021 11:34
Show Gist options
  • Save Sardorbekcyber/7848c3030d2810fd8f9bb4abee99ca5f to your computer and use it in GitHub Desktop.
Save Sardorbekcyber/7848c3030d2810fd8f9bb4abee99ca5f to your computer and use it in GitHub Desktop.
gist read bulk transfer
if (manager.hasPermission(device)) {
try {
val openUsbDevice = manager.openDevice(device)
logArray.add(
LogItem(
"openUsbDevice Connection",
"$openUsbDevice"
)
)
try {
val usbInterface = device.getInterface(0)
val isClaim =
openUsbDevice.claimInterface(usbInterface, true)
val setInterface = openUsbDevice.setInterface(usbInterface)
logArray.add(LogItem("isClaim", "$isClaim"))
logArray.add(
LogItem("set Interface", "$setInterface")
)
logArray.add(
LogItem(
"Endpoint Count",
"${usbInterface.endpointCount}"
)
)
try {
val sendEndpoint =
if (usbInterface.getEndpoint(0).direction == UsbConstants.USB_DIR_OUT) {
usbInterface.getEndpoint(0)
} else {
usbInterface.getEndpoint(1)
}
val receiveEndpoint =
if (usbInterface.getEndpoint(0).direction == UsbConstants.USB_DIR_IN) {
usbInterface.getEndpoint(0)
} else {
usbInterface.getEndpoint(1)
}
logArray.add(
LogItem(
"sendEndPoint Dir",
sendEndpoint.direction.toString()
)
)
logArray.add(
LogItem(
"ReceiveEndpoint Dir",
receiveEndpoint.direction.toString()
)
)
val iccPower = "62000000000000000000".removeWhiteSpaces().fromHexStringToByteArray()
val connectSamCard =
"80 CA 9F 7F 00".removeWhiteSpaces()
.fromHexStringToByteArray()
var openSamCardResponse = ByteArray(64)
val openApplet =
"00 A4 04 00 0E 66697363616C6472697665533031".removeWhiteSpaces()
.fromHexStringToByteArray()
// val openAppletBuffer = ByteBuffer.wrap(openApplet)
// val result = ByteBuffer.allocate(64)
try {
val sendBulkTransfer = openUsbDevice.bulkTransfer(
sendEndpoint,
iccPower,
iccPower.size,
500
)
logArray.add(
LogItem(
"sendBulkTransfer",
"$sendBulkTransfer"
)
)
val receiveResponse =
openUsbDevice.bulkTransfer(
receiveEndpoint,
openSamCardResponse,
47,
500
)
Toast.makeText(
applicationContext,
"$receiveResponse",
Toast.LENGTH_SHORT
).show()
logArray.add(
LogItem(
"receiveBulkTransfer",
"$receiveResponse"
)
)
// val sendRequest = UsbRequest()
//
// val isSendRequestOpen =
// sendRequest.initialize(openUsbDevice, sendEndpoint)
//
// logArray.add(
// LogItem(
// "isRequestOpen",
// "$isSendRequestOpen"
// )
// )
//
// val sendRequestQueue =
// sendRequest.queue(openAppletBuffer)
//
// logArray.add(
// LogItem(
// "sendRequestQueue",
// "$sendRequestQueue"
// )
// )
//
// if (openUsbDevice.requestWait() != null) {
// sendRequest.close()
// val receiveRequest = UsbRequest()
// val receiveReq = receiveRequest.initialize(
// openUsbDevice,
// receiveEndpoint
// )
//
// logArray.add(LogItem("receiveReq", "$receiveReq"))
//
// val receiveQueue = receiveRequest.queue(result)
//
// logArray.add(
// LogItem(
// "receiveQueue",
// "$receiveQueue"
// )
// )
// if (openUsbDevice.requestWait() != null) {
// logArray.add(
// LogItem(
// "resultBuffer",
// result.array()
// .fromByteArrayToHexString()
// )
// )
// }
// }
logList.value = logArray
} catch (exception: Exception) {
Toast.makeText(
applicationContext,
"Error in bulk transfer",
Toast.LENGTH_SHORT
).show()
} finally {
logList.value = logArray
}
} catch (exception: Exception) {
Toast.makeText(
applicationContext,
"Error in getting endpoints",
Toast.LENGTH_SHORT
).show()
}
} catch (exception: Exception) {
Toast.makeText(
applicationContext,
"Exception in getting interface",
Toast.LENGTH_SHORT
).show()
}
} catch (exception: Exception) {
Toast.makeText(
applicationContext,
"Error in opening device",
Toast.LENGTH_SHORT
).show()
}
} else {
Toast.makeText(
applicationContext,
"Looks like manager does not have permission to device",
Toast.LENGTH_LONG
).show()
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment