80,362
社区成员
发帖
与我相关
我的任务
分享
package com.example.testapp;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
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.os.Build;
import android.os.Message;
import android.util.Log;
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
@SuppressLint({ "DefaultLocale", "NewApi" })
public class adwd {
static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";// 权限
public static UsbManager myUsbManager;
public static UsbEndpoint epBulkOut;
public static UsbInterface interface2;
public static UsbDevice myUsbDevice;
public static UsbInterface interface1;
public static UsbEndpoint epBulkIn;
public static UsbDeviceConnection mDeviceConnection;
static int vid = 7358, pid = 3;// 筛选设备vid、pid
private static Context context;
/***
* 枚举设备
*
* @param mUsbManager
*/
public static void enumerateDevice(UsbManager mUsbManager) {
if (mUsbManager == null) {
return;
} else {
HashMap<String, UsbDevice> deviceList = mUsbManager.getDeviceList();
if (!(deviceList.isEmpty())) {
// deviceList不为空
Iterator<UsbDevice> deviceIterator = deviceList.values()
.iterator();
while (deviceIterator.hasNext()) {
UsbDevice device = deviceIterator.next();
// 输出设备信息
Log.i("TAG", "DeviceInfo: " + device.getVendorId() + " , "
+ device.getProductId());
// 保存设备VID和PID
int VendorID = device.getVendorId();
int ProductID = device.getProductId();
// 保存匹配到的设备
if (VendorID == vid && ProductID == pid) {
myUsbDevice = device;
break;
}
}
} else {
}
}
}
/***
* 获取接口
*/
public static void getDeviceInterface() {
if (myUsbDevice != null) {
Log.d("TAG", "interfaceCounts : " + myUsbDevice.getInterfaceCount());
for (int i = 0; i < myUsbDevice.getInterfaceCount(); i++) {
UsbInterface intf = myUsbDevice.getInterface(i);
if (i == 0) {
interface1 = intf;
}
if (i == 1) {
interface2 = intf;
}
}
if (!myUsbManager.hasPermission(myUsbDevice)) {
PendingIntent pi = PendingIntent.getBroadcast(context, 0,
new Intent(ACTION_USB_PERMISSION), 0);
adwd.myUsbManager.requestPermission(adwd.myUsbDevice, pi);
}
} else {
}
}
/***
* 分配端点
*
* @param mInterface
* @return
*/
public static UsbEndpoint assignEndpoint(UsbInterface mInterface) {
UsbEndpoint epControl = null;
UsbEndpoint epIntEndpointOut = null;
UsbEndpoint epIntEndpointIn = null;
UsbEndpoint ep0 = mInterface.getEndpoint(0);
UsbEndpoint ep1 = mInterface.getEndpoint(1);
epBulkOut = ep1;
epBulkIn = ep0;
if (epBulkOut == null && epBulkIn == null && epControl == null
&& epIntEndpointOut == null && epIntEndpointIn == null) {
throw new IllegalArgumentException("not endpoint is founded!");
}
return epIntEndpointIn;
}
/***
* 打开设备、获取连接对象
*
* @param mInterface
*/
public static void openDevice(UsbInterface mInterface) {
if (mInterface != null) {
UsbDeviceConnection conn = null;
// 在open前判断是否有连接权限;对于连接权限可以静态分配,也可以动态分配权限
if (myUsbManager.hasPermission(myUsbDevice)) {
conn = myUsbManager.openDevice(myUsbDevice);
}
if (conn != null && conn.claimInterface(mInterface, true)) {
mDeviceConnection = conn;
Message message = new Message();
message.what = 3;
MainActivity.handler.sendMessage(message);
if (mDeviceConnection != null)// 到此你的android设备已经连上硬件设备
;
// final String mySerial = mDeviceConnection.getSerial();
} else {
}
}
}
/***
* 初始化
*
* @param cont
* @param mUsbManager
* @param use
*/
public static void init(Context context, int use) {
myUsbManager = (UsbManager) context
.getSystemService(Context.USB_SERVICE);
// 枚举设备
enumerateDevice(myUsbManager);
// 查找设备接口
getDeviceInterface();
if (interface1 != null) {
// 获取设备endpoint
assignEndpoint(interface1);
// 打开conn连接
openDevice(interface1);
}
}
/***
* 下发数据
*
* @param buffer
*/
public static void dSend(final byte[] buffer) {
int ret = mDeviceConnection.bulkTransfer(epBulkOut, buffer,
buffer.length, 0);
if (ret < 0) {
mDeviceConnection.close();
} else {
Log.e("下发", "下发成功");
}
}
/***
* 关闭连接对象
*/
public static void close() {
mDeviceConnection.releaseInterface(interface1);
mDeviceConnection.close();
}
/***
* 下发方法
*
* @param lst2
*/
public static void issued(final Context context, final String ls,
final String lst) {
Thread t = new Thread() {
public void run() {
assignEndpoint(interface1);// 获取端点
openDevice(interface1);// 获取设备连接
send(0, ls, lst);
close();// 关闭连接 释放接口
}
};
t.start();
}
/***
* 发送
*
* @param index
* 索引条数
* @param id
* 身份证号
* @param name
* 名字
* @param x16
* 16进制虹膜
*/
public static void send(int index, String info, String x16) {
byte[] code = ArrayAdd(Arrays.copyOf(info.getBytes(), 26),
x16.getBytes());
Log.e("code", " " + code.length);// 总长应为2074 (18+8)+2048
int pack_total = 256;// 单包256-14头长度=242有效长度
int num;
if (code.length % (pack_total - 14) == 0) {
num = (code.length / (pack_total - 14));
} else {
num = (code.length / (pack_total - 14)) + 1;
}
int end_weight = (pack_total - 14)
- (num * (pack_total - 14) - code.length);// 不满包的配重
for (int piece = 0; piece < num; piece++) {
int start = (pack_total - 14) * piece;// 起始位
int end = (pack_total - 14) * (piece + 1);// 结束位
if ((piece + 1) == num && code.length % (pack_total - 14) != 0) {
end = start + end_weight;// 不满包结束位
}
byte[] a = make(index, piece, cutbyte(code, start, end));
dSend(a);
// 包头格式 D I _ _ _ 例: D I 第1条 第0包 有效数据242 D I 第1条 第8包 有效数据138
Log.e("包头",
new String(cutbyte(a, 0, 2)) + " "
+ bytes2Int(cutbyte(a, 2, 6), 0) + " "
+ bytes2Int(cutbyte(a, 6, 10), 0) + " "
+ bytes2Int(cutbyte(a, 10, 14), 0));
Log.e("包长", "" + bytes2Int(cutbyte(a, 10, 14), 0));
if (bytes2Int(cutbyte(a, 6, 10), 0) == 0) {
Log.e("包数据", new String(cutbyte(a, 14, 40)));
Log.e("包数据", new String(cutbyte(a, 40, a.length)));
} else {
Log.e("包数据", new String(cutbyte(a, 14, a.length)));
}
}
}
/***
* 头封装
*/
public static byte[] make(int index, int piece, byte[] n4) {
// "DI" 0000 0000 0000
byte[] di = ArrayAdd("D".getBytes(), "I".getBytes());
byte[] n1 = int2Bytes(index);
byte[] n2 = int2Bytes(piece);
byte[] n3 = int2Bytes(n4.length);
return ArrayAdd(ArrayAdd(ArrayAdd(ArrayAdd(di, n1), n2), n3), n4);
}
/***
* int转byte
*/
public static byte[] int2Bytes(int value) {
byte[] byte_src = new byte[4];
byte_src[3] = (byte) ((value & 0xFF000000) >> 24);
byte_src[2] = (byte) ((value & 0x00FF0000) >> 16);
byte_src[1] = (byte) ((value & 0x0000FF00) >> 8);
byte_src[0] = (byte) ((value & 0x000000FF));
return byte_src;
}
/**
* byte[]转int(高位前低位后)的顺序。
*/
public static int bytes2Int(byte[] ary, int offset) {
int value;
value = (int) ((ary[offset] & 0xFF) | ((ary[offset + 1] << 8) & 0xFF00)
| ((ary[offset + 2] << 16) & 0xFF0000) | ((ary[offset + 3] << 24) & 0xFF000000));
return value;
}
/***
* 数组拼接
*
* @param str1
* String2Byte[]
* @param str2
* String2Byte[]
* @return str1+str2 String2Byte[]
*/
public static byte[] ArrayAdd(byte[] str1, byte[] str2) {
int strLen1 = str1.length;// 保存第一个数组长度
int strLen2 = str2.length;// 保存第二个数组长度
str1 = Arrays.copyOf(str1, strLen1 + strLen2);// 扩容
System.arraycopy(str2, 0, str1, strLen1, strLen2);// 将第二个数组与第一个数组合并
return str1;
}
/***
* 截取byte[]数组
*
* @param by
* 原数组
* @param start
* 起始位置
* @param end
* 结束位置
* @return 新数组
*/
public static byte[] cutbyte(byte[] by, int start, int end) {
byte[] bt = new byte[end - start];
for (int i = start; i < end; i++) {
bt[i - start] = by[i];
}
return bt;
}
}
issued(getApplicationContext(), nam, x16);