Libusb库在Android下的使用例程

时间:2022-02-17 07:10:18

转载请注明:http://blog.csdn.net/hubbybob1/article/details/54863662
阅读本文前清先了解相关基础内容,操作步骤请阅读博客:libusb1.0在android内的移植和使用方法
在讲解了libusb库在Android下的移植和基本应用后,下面来讲解Libusb的Android工程;

其工程步骤:
1.建立带有JNI工程的eclipse工程,网上有很多教程
2.根据上一篇博客生成libusb.a库文件和相关头文件(libusb.h等),并导入JNI工程.
3.编写Android.mk,可以通过ndk-build进行编译C文件到工程里面去如下所示:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)


LOCAL_CFLAGS := -D__STDC_CONSTANT_MACROS
CROSS_DIR := /opt/gstreamer
spice_objs := \
$(LOCAL_PATH)/libs/lib/libusb.a

LOCAL_SRC_FILES := android-jni-interface.c

LOCAL_LDLIBS += $(spice_objs) -llog

LOCAL_C_INCLUDES += \
$(LOCAL_PATH)/libs/include/libusb \
$(CROSS_DIR)/include/glib-2.0 \
$(CROSS_DIR)/lib/glib-2.0/include

#LOCAL_SHARED_LIBRARIES := libmyaudio

LOCAL_MODULE := libxxtest

include $(BUILD_SHARED_LIBRARY)

4.编写C文件并进行ndk-build编译,如下所示:

#include <jni.h>
#include <android/log.h>
#include <stddef.h>
#include "libusb.h"
#include <glib.h>
#define LOG_TAG "MediaRecorder"
#define LOG(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG, __VA_ARGS__)
//#define FALSE 0
JNIEXPORT void JNICALL
Java_com_example_testcusejava_Testcusenative_libusbcallfunc(JNIEnv* env, jobject obj)
{
//openUsbDevice(4703,12587);
int rc,r;
libusb_device_handle *handle = NULL;
libusb_device *device;
libusb_context *ctx = NULL;
r = libusb_init(&ctx);//启动libusb

jclass class = (*env)->FindClass (env, "com/example/testcusejava/Testcusenative");
jmethodID openUsbDevice = (*env)->GetStaticMethodID(env, class, "openUsbDevice", "(II)I");//GetStaticMethodID GetMethodID,通过jni获取Java的方法openUsbDevice
int fd =(*env)->CallStaticIntMethod(env, class,openUsbDevice ,4703,12587);//CallVoidMethod,CallStaticIntMethod调用openUsbDevice 获取fd
LOG("!!!!!一二三!!!!! fd = %d",fd);
__android_log_write(6, "android-service", "Trying to open USB device.get_usb_device_fd");
//rc = libusb_open_fd(device, &handle,fd);//打开usb设备
if (rc != 0) { __android_log_write(6, "channel-usbredir", "Failed to open USB device."); }

//libusb_close(handle);
libusb_exit(ctx); //结束libusb
}

5.编写Java程序

package com.example.testcusejava;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

import com.example.testcusejava.R;
import com.example.testcusejava.Testcusenative.MyThread2;
import com.example.testcusejava.Testcusenative.MyThread3;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;
import android.hardware.usb.UsbInterface;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbRequest;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

//bhw 2016.07.19 e

public class Testcusenative extends Activity {
public native void libusbfunc();// 调用native方法
static {
System.loadLibrary("xxtest");//加载ndk-build生成的libxxtest.so库
}

private Button button,button2;
private UsbManager usbManager;
private UsbDevice usbDevice;
private static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
private PendingIntent pendingIntent;
static int fd = -1;

private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what) {
case 3:
Toast.makeText(Testcusenative.this, "33333333333333333", 0).show();
break;
case 4:
Toast.makeText(Testcusenative.this, "44444444444444444", 0).show();
break;

}
}
};

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testcusenative);
try {
Process process = Runtime.getRuntime().exec("su");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
button = (Button) findViewById(R.id.buttonPlay);
usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);

button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.e("device","call MyThread2 ");
new MyThread2().start();

// libusbfunc();// 调用native方法 }
});


}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.testcusenative, menu);
return true;
}


public static native void test();
static{
//System.loadLibrary("lianyinbao-jni");
}

class MyThread2 extends Thread{

@Override
public void run() {
// TODO Auto-generated method stub
super.run();
try {
UsbDevice ud= null;
//链接到设备的USB设备列表
HashMap<String, UsbDevice> map = usbManager.getDeviceList();

Collection<UsbDevice> usbDevices = map.values();
Iterator<UsbDevice> usbDeviceIter = usbDevices.iterator();
while (usbDeviceIter.hasNext()){
ud = usbDeviceIter.next();
//VendorID 和ProductID 十进制
if(4703 == ud.getVendorId() && 12587 == ud.getProductId()){//vid pid 3141 25409 ; 4703 12587
usbDevice = ud;
Log.e("device","find device"); //bhw
}
//Log.e("device","find device"); //bhw
}

//检测USB设备权限受否授权
if(usbManager.hasPermission(usbDevice)){
handler.sendEmptyMessage(3);
new MyThread3().start();
Log.e("device","usbManager.hasPermission");//bhw
}else{
//如果没有授权就授予权限
handler.sendEmptyMessage(4);
usbManager.requestPermission(usbDevice, pendingIntent); //在此Android系统会弹出对话框,选择是否授予权限
Log.e("device","usbManager.requestPermission"); //bhw
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

class MyThread3 extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
Log.e("device","MyThread3");//bhw
UsbDeviceConnection connection = usbManager.openDevice(usbDevice);
fd = connection.getFileDescriptor();//获取文件描述符
Log.e("device","MyThread3 "+fd);
jnicallfunc();//调用native方法 }
}
public static int openUsbDevice(int vid, int pid) throws InterruptedException {

Log.e("device","openUsbDevice "+fd);
return fd;

}
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {

public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.e( "action", action);

if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
usbDevice = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
handler.sendEmptyMessage(1)
if(usbDevice != null){
new MyThread3().start();//MyThread3
}
}
else {
Log.d("denied", "permission denied for device " + usbDevice);
}
}
}
}
};
}

当编写完后的运行结果如下图:
Libusb库在Android下的使用例程
打印的信息顺序为下图:
Libusb库在Android下的使用例程
到此主要的程序已经完毕,本程序eclipse的工程代码下载