Skip to content

Add unrooted Android example - #830

Closed
Peter-St wants to merge 11 commits into
libusb:masterfrom
Peter-St:master
Closed

Peter-St wants to merge 11 commits into
libusb:masterfrom
Peter-St:master

Conversation

@Peter-St

Copy link
Copy Markdown
Contributor

Adding another approach for connecting UsbDevices on Android.

Adding another approach for connecting UsbDevices on Android.
@Peter-St

Copy link
Copy Markdown
Contributor Author

Using LibUsb on non rooted Android devices is explained in this PullRequest.
Should there eventually added a seperate C sample in the example folder for non rooted devices too?

@mcuee

mcuee commented Dec 30, 2020

Copy link
Copy Markdown
Member

If you can, please have another pull request to add the example for non root device. Thanks.

@Peter-St

Copy link
Copy Markdown
Contributor Author

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 ...)

@avantgardnerio

Copy link
Copy Markdown

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.

@mcuee mcuee mentioned this pull request Jan 30, 2021

@dickens dickens left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@xloem

xloem commented Feb 11, 2021 •

Copy link
Copy Markdown
Contributor

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:

  • Given most devices are unrooted, it would make sense for LIBUSB_OPTION_WEAK_AUTHORITY to be the default behavior on android if permissions are not present to enumerate devices.
  • This example code casually references a field called mPermissionIntent. It looks like this is not hard to construct but responding to it might require subclassing BroadcastReceiver: https://developer.android.com/guide/topics/connectivity/usb/host#permission-d
  • It's notable from that same page that permission can also be acquired by registering to respond to a usb connection event, and it sounds like then no request need be made; worth mentioning.

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.

Peter-St and others added 2 commits February 11, 2021 20:02
- added libusb_set_option documentation
@Peter-St

Copy link
Copy Markdown
Contributor Author

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.

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.
To guarantee booth ways to work, we need different files I think.

Peter

Comment thread android/examples/unrooted_android.c Outdated
#include <jni.h>
#include <string.h>
#include "libusb.h"
#ifdef __ANDROID__

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really unnecessary now since this is an Android example.

Comment thread android/examples/unrooted_android.c Outdated
}

// fileDescriptor = is the native File Descriptor obtained in Java and transfered to native over JNA for Example.
extern int main(int fileDescriptor) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • This deserves a better name than main().
  • Please be consistent with the formatting and move the open curly brace to the next line.

Comment thread android/examples/unrooted_android.c Outdated
LOGD("libusb_wrap_sys_device failed: %d\n", r);
return r;
}
else if (devh == NULL) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistent formatting this should be on the previous line with the closing curly brace.

Peter-St added 2 commits February 16, 2021 20:58
- 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
Comment thread android/examples/unrooted_android.c Outdated
Comment thread android/examples/unrooted_android.c Outdated
Comment thread android/examples/unrooted_android.c Outdated
@mcuee

mcuee commented Jun 8, 2021

Copy link
Copy Markdown
Member

Just wondering whether we should merge just the improved Readme for 1.0.25RC1 and leave the examples for next release.

@mcuee mcuee mentioned this pull request Jun 8, 2021
@Peter-St

Peter-St commented Jun 27, 2021 •

Copy link
Copy Markdown
Contributor Author

If we do pass a pointer (to a pointer) to a context, like so libusb_init(&ctx), then ctx in libusb_init is not NULL, so default_context_options[LIBUSB_OPTION_WEAK_AUTHORITY] that was set before doesn't seem actually used at all, is it?

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.

@harbulot

Copy link
Copy Markdown
Contributor

I think the following patch would make LIBUSB_OPTION_WEAK_AUTHORITY have an effect, and it also fixes the app crash problems I was mentioning at first:

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 usbi_backend.set_option(...) (which is what actually uses LIBUSB_OPTION_WEAK_AUTHORITY in linux_usbfs.c) when there is no default context initialised.

Please let me know if you'd like me to create a PR for this.

@Peter-St

Copy link
Copy Markdown
Contributor Author

Please let me know if you'd like me to create a PR for this.

ok, try to create the PR. Eventually add #ifdef __ANDROID__ to not influence the regular way of other platforms.

@tormodvolden

Copy link
Copy Markdown
Contributor

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.

tormodvolden pushed a commit to tormodvolden/libusb that referenced this pull request Sep 23, 2021
Add another approach for connecting USB devices on Android.

References libusb#830
Closes libusb#996
@tormodvolden tormodvolden changed the title Update Android Readme Update Android example Sep 23, 2021
Comment thread android/examples/unrooted_android.c Outdated
@Peter-St

Copy link
Copy Markdown
Contributor Author

The PR has been updated and the libusb_set_option method has been changed.

@tormodvolden tormodvolden changed the title Update Android example Add unrooted Android example Oct 1, 2021

@mcuee mcuee left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not see any issues merging this.

@mcuee
mcuee removed the request for review from dickens October 2, 2021 00:59
@tormodvolden

Copy link
Copy Markdown
Contributor

Only that it doesn't work... due to #942. I can push this as soon as 942 has been sorted out.

@mcuee

mcuee commented Oct 2, 2021

Copy link
Copy Markdown
Member

Only that it doesn't work... due to #942. I can push this as soon as 942 has been sorted out.

#942 seems to be okay based on my limited understanding of libusb internals. Probably we can merge that first.

@harbulot

Copy link
Copy Markdown
Contributor

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 libusb project if there's a good place for it.)

@tormodvolden

Copy link
Copy Markdown
Contributor

Please confirm that this builds and runs fine from git master now.

@Peter-St

Peter-St commented Nov 2, 2021

Copy link
Copy Markdown
Contributor Author

Hello tormodvolden,

I can confirm that the last build compiles and runs fine.
I have tested the build with following Android Studio Project: https://github.com/Peter-St/unRootedSample

Thanks, Peter

@tormodvolden

Copy link
Copy Markdown
Contributor

@Peter-St, thanks a lot for verifying and, of course, for contributing this example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants