通过android.hardware.usb获得USB许可并不适用于NDK并不、适用于、usb、hardware

2023-09-05 23:26:51 作者:情话说到流泪

我能获得许可,通过Android的USB主机API的设备进行通信。

I was able to obtain a permission to communicate with a device via Android's USB Host API.

private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";

protected void execute(Context ctxt) {
    UsbManager manager = (UsbManager) viewer.getSystemService(Context.USB_SERVICE);
    HashMap<String, UsbDevice> deviceList = manager.getDeviceList();

    UsbDevice d = null;
    for (String s : deviceList.keySet()) {
        d = deviceList.get(s);
    }

    PendingIntent mPermissionIntent = PendingIntent.getBroadcast(ctxt, 0, new Intent(ACTION_USB_PERMISSION), 0);
    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
    viewer.registerReceiver(mUsbReceiver, filter);

    manager.requestPermission(d, mPermissionIntent);
}

private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (ACTION_USB_PERMISSION.equals(action)) {
            synchronized (this) {
                UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

                if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                    if(device != null){
                      Log.d(TAG, "Permission granted!");
                   }
                } 
                else {
                    Log.d(TAG, "permission denied for device " + device);
                }
            }
        }
    }
};

不幸的是,不给我NDK code允许直接与该装置(这是需要libusb的)进行通信。有什么办法来转移从Java的权限NDK无根?

Unfortunately that doesn't give my NDK code permission to directly communicate with this device (which is needed for libusb). Is there any way to "transfer" the permissions from Java to NDK without root?

P.S。 我结束了使用Unix套接字从Java原始文件Descirptor转移到Android原生可执行文件(我有GNU-ED该项目在的 https://github.com/martinmarinov/rtl_tcp_andro- )。 对于一些人来说,因为他们可能会使用NDK即可直接与他们在Java中使用可能仍然由NDK访问,而不必惹UNIX周围的设备(而非第三方应用程序),因此指针连接,将会变得更容易插座。

P.S. I ended up using UNIX sockets to transfer the original File Descirptor from Java to an Android native executable (I have GNU-ed the project at https://github.com/martinmarinov/rtl_tcp_andro- ). For some people it would be even easier since they might be using NDK to connect with the device directly (not a third party app) and therefore the pointer that they use in Java may be still accessible by the NDK without having to mess around with UNIX sockets.

推荐答案

有可能的权限没有转移,因为Android安全模型要求每个应用程序静态地声明其要求的权限和用户同意在安装时设置的权限。无动态权限的支持。每个应用程序映射到一个UID,其清单中静态声明的权限。 Java是不是权限边界,Dalvik虚拟机是毫无阻挡地通过NDK本地运行code。所有权限必须列在清单,和APK可能不包含的Java(DEX)code在所有。

There is no "transfer" of permissions possible because the Android security model demands that each application statically declare its required permissions and the user approves the permission set at install time. No dynamic permissions are supported. Each app maps to a single UID, with its statically declared permissions in the manifest. Java is not a permission boundary, and the Dalvik VM is no barrier to running native code via NDK. All the permissions must be listed in the manifest, and the apk may contain no Java (dex) code at all.

http://developer.android.com/guide/topics/security/ permissions.html

http://osdir.com/ml/android-ndk/ 2012-09 / msg00094.html