Repository navigation
Conversation
Adding another approach for connecting UsbDevices on Android.
|
Using LibUsb on non rooted Android devices is explained in this PullRequest. |
|
If you can, please have another pull request to add the example for non root device. Thanks. |
|
So this is my Android Sample for Libusb. The Sample shows how to create an Shared Object with the NDK which can be accessed over JNI or JNA from Java or Kotlin. --> with Granted Permissions on Java/Kotlin and obtained FileDescriptor. The approach of including the full source of LibUsb to an Android Studio project is quite complicated. It is only possible over NDK for now I think. (eventually it's enough for only applying the precombiled LibUsb lib to an android device and accessing it over the unrooted sample ...) |
|
I only skimmed it, but it looks to be correct. I haven't been using libusb with android directly, but rather with the Rust FFI wrapper. I made a sample project that definitely worked and ran in CI here, in case that is useful to compare. |
dickens
left a comment
There was a problem hiding this comment.
The new file examples/unRootedAndroid.c is specific to Android. As such I'd prefer it to live in the android directory. However, since it is just a modified copy of examples/testlibusb.c then you can use preprocessor macros to build the same file for both.
Please also note the naming conventions used in the project. We do not use Camel Case so please find a more suitable name for the file.
|
II'm working on porting a library to android. Here are my notes working off of this readme; I'll edit as I bump into things:
All these things are just things I noticed, nothing serious. I would merge this simply because it helps people more than what is already there. See also my android pr #874 which provides for automating the java if the user can provide a jnienv pointer. |
- added libusb_set_option documentation
Adding preprocessor macros for Android in the original examples files would effect then the functionality for rooted devices, because here we have a different approach of initializing LibUsb. Peter |
| #include <jni.h> | ||
| #include <string.h> | ||
| #include "libusb.h" | ||
| #ifdef __ANDROID__ |
There was a problem hiding this comment.
This is really unnecessary now since this is an Android example.
| } | ||
|
|
||
| // fileDescriptor = is the native File Descriptor obtained in Java and transfered to native over JNA for Example. | ||
| extern int main(int fileDescriptor) { |
There was a problem hiding this comment.
- This deserves a better name than
main(). - Please be consistent with the formatting and move the open curly brace to the next line.
| LOGD("libusb_wrap_sys_device failed: %d\n", r); | ||
| return r; | ||
| } | ||
| else if (devh == NULL) { |
There was a problem hiding this comment.
For consistent formatting this should be on the previous line with the closing curly brace.
- rename the method main to unrooted_usb_description - link a header file to this method for accessing it over JNA for example. - format changes in the file
- link the unrooted_usb_description method for external access over JNA or JNI
|
Just wondering whether we should merge just the improved Readme for 1.0.25RC1 and leave the examples for next release. |
phew, for my understanding libusb_set_option to LIBUSB_OPTION_WEAK_AUTHORITY skipps the device enumeration. I must admit, I don't know how the internal way of libusb works. |
|
I think the following patch would make diff --git a/libusb/core.c b/libusb/core.c
index 054bc6a..90a1339 100644
--- a/libusb/core.c
+++ b/libusb/core.c
@@ -2205,14 +2205,11 @@ int API_EXPORTED libusb_set_option(libusb_context *ctx,
}
ctx = usbi_get_context(ctx);
- if (NULL == ctx) {
- return LIBUSB_SUCCESS;
- }
switch (option) {
case LIBUSB_OPTION_LOG_LEVEL:
#if defined(ENABLE_LOGGING) && !defined(ENABLE_DEBUG_LOGGING)
- if (!ctx->debug_fixed)
+ if (ctx && !ctx->debug_fixed)
ctx->debug = (enum libusb_log_level)arg;
#endif
break;
@@ -2230,6 +2227,10 @@ int API_EXPORTED libusb_set_option(libusb_context *ctx,
default:
return LIBUSB_ERROR_INVALID_PARAM;
}
+
+ if (NULL == ctx) {
+ return LIBUSB_SUCCESS;
+ }
return LIBUSB_SUCCESS;;
}The purpose of that patch is to call Please let me know if you'd like me to create a PR for this. |
ok, try to create the PR. Eventually add |
|
I extracted the README change here and fixed up formatting, in #996, so that that part can be merged while we wait for the example to be finalized. |
Add another approach for connecting USB devices on Android. References libusb#830 Closes libusb#996
… new LIBUSB_OPTION_NO_DEVICE_DISCOVERY
|
The PR has been updated and the libusb_set_option method has been changed. |
mcuee
left a comment
There was a problem hiding this comment.
I do not see any issues merging this.
|
Only that it doesn't work... due to #942. I can push this as soon as 942 has been sorted out. |
|
If that helps, I've added a sample app that tests #942 here: https://github.com/harbulot/LibUsbAndroidIssue942 (I've put it under MIT license there, but I'd certainly be happy to give to to the |
|
Please confirm that this builds and runs fine from git master now. |
|
Hello tormodvolden, I can confirm that the last build compiles and runs fine. Thanks, Peter |
|
@Peter-St, thanks a lot for verifying and, of course, for contributing this example. |
Adding another approach for connecting UsbDevices on Android.