我能够获得通过Android的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代码权限直接与该设备(这是需要的libusb)进行通信。 有什么办法来“转移”从Java到NDK的权限没有根?
PS我结束了使用Unix套接字从Java的原始文件Descirptor转移到Android原生可执行文件(我有GNU-ED项目在https://github.com/martinmarinov/rtl_tcp_andro- )。 对于一些人来说,因为他们可能会使用NDK与直接在设备(不是第三方应用程序),因此指针,他们在Java中使用可以是由NDK仍然可以访问,而无需使用UNIX不必浪费时间连接它会更容易插座。