我認爲試圖編譯一個特定的C程序,它允許通過USB控制Gembird SilverShield電源插座爲Android。在我的android HDMI電視棒上,這將非常有用。有一個開放的項目this。它在Linux下工作,取決於libusb。目標平臺是android ICS。我想在Ubuntu Linux上開發。我有什麼機會讓它工作?什麼是必需的步驟。安裝android SDK,NDK,交叉編譯器...
有一箇舊的問題here,與Android上的libusb相關,但沒有信息如何。
將應用程序移植到androids自己的usb庫可能更容易嗎?編譯並鏈接到Android的libusb
回答
Libusb可以在非root的android上工作(假設設備支持USB主機...這是非常重要的,因爲並非所有的設備都是如此)。你需要使用標準的android USB棧。然後您可以從USBDevice獲取設備描述符並將其傳遞給libusb。
不幸的是,您還需要修改libusb。幸運的是,其他人已經解釋了您需要如何修改LibUSB。
LibUSB已被修改here。
祝你好運!
編輯:
首先,你需要定義一個廣播接收器:
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)
{
UsbDeviceConnection deviceConnection = mUsbManager.openDevice(device);
Log.d("USB", deviceConnection.getSerial());
}
}
else
{
Log.d("USB", "permission denied for device " + device);
}
}
}
}
}
現在,你需要創建一個USBManager和枚舉的設備:
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap< String, UsbDevice > stringDeviceMap = mUsbManager.getDeviceList();
Collection<UsbDevice> usbDevices = stringDeviceMap.values();
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
Iterator<UsbDevice> usbDeviceIter = usbDevices.iterator();
while(usbDeviceIter.hasNext())
{
if (USBDeviceValid(usbDevice))
{
// Request permission to access the device.
mUsbManager.requestPermission(usbDevice, mPermissionIntent);
// Open the device.
UsbDeviceConnection connection = mUsbManager.openDevice(usbDevice);
int fd = connection.getFileDescriptor();
// Now pass the file descriptor to libusb through a native call.
}
}
編輯2:
獲得libusb的建立只是把文件放在方便(我把它們放在JNI/libusb的),然後添加以下行到你的Android.mk的事情:
include $(CLEAR_VARS)
LOCAL_MODULE := libusb
LOCAL_SRC_FILES := libusb/core.c libusb/descriptor.c libusb/io.c libusb/sync.c libusb/os/linux_usbfs.c
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
我可以在某處找到創建和傳遞設備描述符。 – highsciguy 2013-04-26 10:31:35
@highsciguy:發佈編輯。 – Goz 2013-04-26 11:08:10
@highsciguy:這有幫助嗎? – Goz 2013-04-26 19:29:39
即使你編譯了它,Android可能也不會讓你通過libusb訪問USB設備,除非你的設備是根植的。如果將應用程序移植到Android's native USB stack是可行的,那幾乎肯定會是更穩定的解決方案。
設備已生根。對於所有其他具有最新固件的Rikomagic III HDMI支架也是如此。如果這是不使用libusb的唯一原因,我會試一試。但是,知道類似Android的USB堆棧和libusb是多麼的有趣,如果你對它有所瞭解......另外:我怎樣才能在C中使用它。 – highsciguy 2013-04-26 08:27:37
你也可以嘗試使用android serial port api
這裏是串口init的例子。
private FileDescriptor mFd;
private FileInputStream mFileInputStream;
private FileOutputStream mFileOutputStream;
public SerialPort(File device, int baudrate, int flags) throws SecurityException, IOException {
/* Check access permission */
if (!device.canRead() || !device.canWrite()) {
try {
/* Missing read/write permission, trying to chmod the file */
Process su;
su = Runtime.getRuntime().exec("/system/bin/su");
String cmd = "chmod 666 " + device.getAbsolutePath() + "\n"
+ "exit\n";
su.getOutputStream().write(cmd.getBytes());
if ((su.waitFor() != 0) || !device.canRead()
|| !device.canWrite()) {
throw new SecurityException();
}
} catch (Exception e) {
e.printStackTrace();
throw new SecurityException();
}
}
mFd = open("/dev/ttyACM0", 9600, 0);
if (mFd == null) {
Log.e(TAG, "native open returns null");
throw new IOException();
}
mFileInputStream = new FileInputStream(mFd);
mFileOutputStream = new FileOutputStream(mFd);
}
在USB主機上使用設備也是如此。
我實現的解決方案是使用Java API打開USB設備,然後使用帶有libusb的文件描述符。我由PrimeSense的(https://github.com/OpenNI/OpenNI2)使用的libusb從openni項目
碼位:
打開設備(使用痛飲):
int LibUsbAndroid::android_open(libusb_device *device, libusb_device_handle **devHandle)
{
int fd = USBJNICallbacks::getCallback()->getDeviceFd(device->bus_number, device->device_address);
__android_log_print(ANDROID_LOG_VERBOSE,"USB","Got FD:%d",fd);
if(fd==-1)
{
__android_log_print(ANDROID_LOG_ERROR,"USB","android_open, bad fd");
return -1;
}
return libusb_open(device, devHandle, fd);
}
開放Java代碼的設備(在主線程不運行! ):
public int getDeviceFd(int busNumber, int deviceAddress) {
UsbDevice device = findDevice(busNumber, deviceAddress);
if(device!=null)
{
mReceivedPermission = false;
PermissionRequester pr = new PermissionRequester(device);
pr.run();
if(!mUsbManager.hasPermission(device))
{
Log.v("USB", "Requesting permissiom to device");
mPermissionIntent = PendingIntent.getBroadcast(mContext, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filterPermission = new IntentFilter(ACTION_USB_PERMISSION);
mContext.registerReceiver(mUsbPermissionReceiver, filterPermission);
mUsbManager.requestPermission(device, mPermissionIntent);
}
else
{
Log.v("USB", "Already has permission");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mReceivedPermission = true;
Log.v("USB", "Opening device");
OpenDevice od = openDevice(device);
Log.v("USB", "Adding to open devices");
mOpenDevices.put(""+busNumber+"/"+deviceAddress, od);
}
Log.v("USB", "Waiting for permission");
waitForPermissionResult();
OpenDevice od = mOpenDevices.get(""+busNumber+"/"+deviceAddress);
if(od!=null)
{
Log.v("USB", "Getting FD");
int result = od.mConnection.getFileDescriptor();
Log.i("USB","USB File desc:"+result);
return result;
}
else
{
Log.v("USB", "Error getting FD");
return -1;
}
}
return -1;
}
權限處理代碼:
private BroadcastReceiver mUsbPermissionReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
Log.v("USB", "Received permission result");
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)) {
Log.v("USB", "Received permission result OK");
if(device != null){
Log.v("USB", "Device OK");
mContext.unregisterReceiver(this);
Log.v("USB", "Openning device");
OpenDevice od = openDevice(device);
Log.v("USB", "Adding to open device list");
mOpenDevices.put(""+od.mBus+"/"+od.mAddress,od);
Log.v("USB", "Received permission is true");
mReceivedPermission = true;
}
else {
Log.d(TAG, "permission denied for device " + device);
}
}
}
}
}
};
開放功能:
public OpenDevice openDevice(UsbDevice device) {
UsbDeviceConnection connection = mUsbManager.openDevice(device);
Log.i("USB","Device name="+device.getDeviceName());
int bus = getBusNumber(device.getDeviceName());
int address = getAddress(device.getDeviceName());
return new OpenDevice(device, connection, bus, address);
}
USB LIB變化(core.c):
int API_EXPORTED libusb_open(libusb_device *dev,
libusb_device_handle **handle, int fd)
{
struct libusb_context *ctx = DEVICE_CTX(dev);
struct libusb_device_handle *_handle;
size_t priv_size = usbi_backend->device_handle_priv_size;
int r;
usbi_dbg("open %d.%d", dev->bus_number, dev->device_address);
_handle = malloc(sizeof(*_handle) + priv_size);
if (!_handle)
return LIBUSB_ERROR_NO_MEM;
r = usbi_mutex_init(&_handle->lock, NULL);
if (r) {
free(_handle);
return LIBUSB_ERROR_OTHER;
}
_handle->dev = libusb_ref_device(dev);
_handle->claimed_interfaces = 0;
memset(&_handle->os_priv, 0, priv_size);
r = usbi_backend->open(_handle,fd);
if (r < 0) {
usbi_dbg("open %d.%d returns %d", dev->bus_number, dev->device_address, r);
libusb_unref_device(dev);
usbi_mutex_destroy(&_handle->lock);
free(_handle);
return r;
}
usbi_mutex_lock(&ctx->open_devs_lock);
list_add(&_handle->list, &ctx->open_devs);
usbi_mutex_unlock(&ctx->open_devs_lock);
*handle = _handle;
/* At this point, we want to interrupt any existing event handlers so
* that they realise the addition of the new device's poll fd. One
* example when this is desirable is if the user is running a separate
* dedicated libusb events handling thread, which is running with a long
* or infinite timeout. We want to interrupt that iteration of the loop,
* so that it picks up the new fd, and then continues. */
usbi_fd_notification(ctx);
return 0;
}
op_open(libusb_fs.c)變化:
static int op_open(struct libusb_device_handle *handle, int fd)
{
struct linux_device_handle_priv *hpriv = _device_handle_priv(handle);
char filename[PATH_MAX];
_get_usbfs_path(handle->dev, filename);
usbi_dbg("opening %s", filename);
hpriv->fd = fd;
if (hpriv->fd < 0) {
if (errno == EACCES) {
usbi_err(HANDLE_CTX(handle), "libusb couldn't open USB device %s: "
"Permission denied.", filename);
usbi_err(HANDLE_CTX(handle),
"libusb requires write access to USB device nodes.");
return LIBUSB_ERROR_ACCESS;
} else if (errno == ENOENT) {
usbi_err(HANDLE_CTX(handle), "libusb couldn't open USB device %s: "
"No such file or directory.", filename);
return LIBUSB_ERROR_NO_DEVICE;
} else {
usbi_err(HANDLE_CTX(handle),
"open failed, code %d errno %d", hpriv->fd, errno);
return LIBUSB_ERROR_IO;
}
}
return usbi_add_pollfd(HANDLE_CTX(handle), hpriv->fd, POLLOUT);
}
謝謝,這很有幫助。您還可以分享如何創建調用libusb_open()所需的libusb_device *? – rsp1984 2016-10-22 15:45:34
- 1. 編譯並將代碼鏈接到Scilab
- 2. 編譯器錯誤的libusb
- 3. libusb的編譯錯誤
- 4. 編譯反對的libusb-dev的Ubuntu上
- 5. C++編譯鏈接
- 6. 鏈接到編譯爲C++的C庫
- 7. 編譯Fortran和C++程序並從intel編譯器鏈接
- 8. 用libusb而不是hidraw編譯hidapi
- 9. 在編譯時鏈接vs在運行時鏈接到std ::庫
- 10. 編譯鏈接錯誤?
- 11. 鏈接和編譯pcl
- 12. 鏈接錯誤,而編譯
- 13. 編譯動態鏈接庫
- 14. iOS編譯和鏈接庫
- 15. 鏈接libC++編譯提升
- 16. 編譯和鏈接miracl C++
- 17. 鏈接列表未編譯
- 18. 編譯靜態鏈接VS2015
- 19. 編譯動態鏈接庫
- 20. 編譯和鏈接錯誤
- 21. 將libusb-1.0.9鏈接到windows中的代碼塊
- 22. Android NDK r6b爲Android交叉編譯libfaac(鏈接libsupC++問題)
- 23. 直接鏈接到資產管道中的編譯資源?
- 24. 編譯wget與靜態鏈接,自我編譯(打開)ssl庫鏈接問題
- 25. 是Android SDK編譯/鏈接到APK還是安裝在Android設備上?
- 26. 編譯NDK項目時Android Studio中的鏈接器錯誤
- 27. 的Xerces-C++編譯/鏈接問題
- 28. 編譯/鏈接攪拌機的窗口
- 29. 的Lua編譯鏈接錯誤
- 30. Borland C++編譯器中的鏈接DLL
這裏有一個項目叫libusbdroid http://sourceforge.net/projects/libusbdroid/。我將如何鏈接sispmctl並進行安裝? – highsciguy 2013-04-19 12:36:43