SDL中文论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 3470|回复: 1
打印 上一主题 下一主题

[Discuss] kOS:USB

[复制链接]

187

主题

346

帖子

2450

积分

版主

Rank: 7Rank: 7Rank: 7

积分
2450
跳转到指定楼层
楼主
发表于 2019-5-30 20:11:03 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
一、获得访问usb设备的句柄
  1. UsbManager mUsbManager;
  2. UsbDevice mDevice;
复制代码
1.1)mUsbManager = (UsbManager)getSystemService(Context.USB_SERVICE);
1.2)mUsbManager.getDeviceList()得到设备列表,UsbDevice表示当中的一个设备。UsbDevice中有vid、pid字段,app需预先知道你想操作设备的vid、pid,当一致时得到一个device,这个device就是此次操作这设备的句柄。

1.3) 向系统请求操作该device设备的权限。
  1. Intent intent = new Intent(ACTION_USB_PERMISSION);
  2. PendingIntent mPermissionIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
  3. mUsbManager.requestPermission(device, mPermissionIntent);
复制代码
device就是你想操作的UsbDevice,以上3条语句的作用是请求权限,执行它们之前需先执行以下代码,用于向系统注册个广播接收器。
  1. IntentFilter filter = new IntentFilter();
  2. filter.addAction(ACTION_USB_PERMISSION);
  3. filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
  4. registerReceiver(mUsbReceiver, filter);
复制代码
上面都用到了一个字符串常量ACTION_USB_PERMISSION,它不是Android内定,是app自定义。
  1. String ACTION_USB_PERMISSION = "com.Routon.HIDTest.USB_PERMISSION"
复制代码

registerReceiver是app需自写的BroadcastReceiver。
  1. private BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
  2.   public void onReceive(Context context, Intent intent) {
  3.     String action = intent.getAction();
  4.     if (ACTION_USB_PERMISSION.equals(action)) {
  5.       UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
  6.       if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
  7.         // 设备已授权。可以访问
  8.         mDevice = device; // mDeivce是app定义的变量,此至得到了操作该设备要用到的句柄。
  9.       } else {
  10.         // 设备未授权。不能访问
  11.       }
  12.     }
  13.   }
  14. }
复制代码


二、读数据
三、写数据
四、关闭设备句柄
回复

使用道具 举报

187

主题

346

帖子

2450

积分

版主

Rank: 7Rank: 7Rank: 7

积分
2450
沙发
 楼主| 发表于 2019-5-30 20:16:19 | 只看该作者

访问USB身份证模块

本帖最后由 ancientcc 于 2019-5-30 20:18 编辑

1、mHIDDev.GetSamStaus();
{0, 3, 17, -1, -19}

2、ret = mHIDDev.GetSamId(samIDInfo);
{0, 3, 18, -1, -18}

3、ret = mHIDDev.Authenticate();
  1. public int Authenticate() {
  2.   byte[] inn = new byte[4];
  3.   byte[] sn = new byte[8];
  4.   return this.findIDCard(inn) < 0?-1:(this.selectIDCard(sn) < 0?-2:0);
  5. }
复制代码
ret >=0 时表示找到卡

4、ReadBaseFPMsg
  1. sIDInfo = mHIDDev.new SecondIDInfo();
  2. byte[] fingerPrint = new byte[1024];
  3. ret = mHIDDev.ReadBaseFPMsg(sIDInfo, fingerPrint);
复制代码
读取身份证数据。在selectIDCard和ReadBaseFPMsg之间挪开身份证时,ReadBaseFPMsg就可能返回失败。得到的sIDInfo保存着读到的身份证

5、峰鸣。有些身份证模块自带
ret = mHIDDev.BeepLed(true, true, 500);
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|丽谷软件|libsdl.cn

GMT+8, 2024-5-20 03:04 , Processed in 0.053482 second(s), 19 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表