Libusb库在Android下的使用例程
来源:互联网 发布:程序员年薪百万 编辑:程序博客网 时间:2024/06/06 01:49
转载请注明: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_MACROSCROSS_DIR := /opt/gstreamerspice_objs := \ $(LOCAL_PATH)/libs/lib/libusb.aLOCAL_SRC_FILES := android-jni-interface.cLOCAL_LDLIBS += $(spice_objs) -llogLOCAL_C_INCLUDES += \ $(LOCAL_PATH)/libs/include/libusb \ $(CROSS_DIR)/include/glib-2.0 \ $(CROSS_DIR)/lib/glib-2.0/include#LOCAL_SHARED_LIBRARIES := libmyaudioLOCAL_MODULE := libxxtestinclude $(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 0JNIEXPORT void JNICALLJava_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 epublic 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); } } } } }; }
当编写完后的运行结果如下图:
打印的信息顺序为下图:
到此主要的程序已经完毕,本程序eclipse的工程代码下载
2 0
- Libusb库在Android下的使用例程
- Libusb库的使用
- linux下USB驱动分析以及libusb库的使用
- linux下交叉编译libusb的方法及编译一个使用了libusb库的test程序的方法
- linux下交叉编译libusb的方法及编译一个使用了libusb库的test程序的方法
- 在vs2012中使用libusb-1.0库
- linux下libusb使用
- 关于libusb-win32-bin-1.2.6.0在QT环境下使用的配置方法
- libusb的使用
- 使用libusb库笔记
- android VideoView的使用例程
- android VideoView的使用例程
- 在linux下为QT安装libusb
- 使用libusb库枚举mac设备下面的USB设备
- win7 32位下安装使用libusb
- 在IVF中使用MKL的例程
- libusb使用
- libusb使用
- android图片加载框架ImageLoader涉及的设计模式
- 第十八章 并发登录人数控制——《跟我学Shiro》
- DirectX编程开发准备工作 ---Directx SDK安装,及在2010中的配置
- java 基本数据类型和String
- Coturn配置
- Libusb库在Android下的使用例程
- zabbix3.2 告警
- Python——pyExcelerator库和xlrd库
- Nagios check_snmp_int.pl 监控交换机端口状态和流量
- hdu 1214 圆桌会议
- 原生OKHttp以及OKHttpUtil的使用
- Spring4.x官方参考文档中文版——第21章 Web MVC框架(27)
- mysql redis
- 在Eclipse中应该怎样去修改Android应用程序的包名