Add custom maven repository
repositories {
mavenCentral()
maven {
url 'https://packages.eraga.net/repository/eraga-public-maven-releases/'
}
}
Dependency for Desktop apps:
compile "net.eraga.rxusb:libusb:1.0.5"
Dependency for Android apps (libusb):
compile "net.eraga.rxusb:libusb-android:1.0.5"
List all connected USB devices:
val usbManager = UsbService.instance
usbManager.getDeviceList().forEach {
println(it)
}
Subscribe to bulk transfers' content and print it to stdout
as soon as it comes with the power of rxjava2
:
val usbManager = UsbService.instance
val device = usbManager.findDevice(0x1a86, 0x7523)
val deviceConnection = usbManager.openDevice(device)
val interfaceConnection = deviceConnection.claimInterface(device.getInterface(0))
val bulkEndpoint = interfaceConnection.open(
interfaceConnection.usbInterface.getEndpoint(0)
) as BulkReadableChannel
bulkEndpoint.subscribeOn(Schedulers.io())
.map<String> { byteArray ->
// Lets think that this usb device sends UTF-8 text
byteArray.toString(Charsets.UTF_8)
}
.observeOn(Schedulers.single())
.subscribe({ text ->
// output incoming text
println(text)
})
Share bulk transfer data between two different parts of code and treat it differentely:
val sharedEndpoint = bulkEndpoint.share()
sharedEndpoint
.subscribeOn(Schedulers.io())
.subscribe {
// do some incredible stuff
}
sharedEndpoint
.subscribeOn(Schedulers.io())
.subscribe {
// do some other stuff
}
Send data to USB device:
val bulkOutEndpoint = interfaceConnection.open(
interfaceConnection.usbInterface.getEndpoint(1)
) as BulkWritableChannel
val text = "Hello World!"
val textByteBuffer = ByteBuffer.allocateDirect(text.length)
textByteBuffer.put(text.toByteArray())
bulkOutEndpoint.send(textByteBuffer)
.subscribe ({
log.info("Data successfully sent")
},{
log.error("Error: {}", it)
})