还在百度Google导出搜索如何进行USB接口的HID进行开发吗?网站上很多文章并不完善,这方便的也介绍的不多,我看了很多资料,借助网上的一些代码,整理了以下信息,希望能给大家提供便捷
首先请大家仔细看看Google官方并不详细的SDK文档
Android系统3.1及以上版本才能支持USBHOST,这样我们才能连接HID设备进行通讯
项目新建完成之后,AndroidManifest.xml中加入以下代码
下面就是java代码了,直接贴完整代码吧
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
com.android.missilelauncher;
import
java.nio.ByteBuffer;
import
java.util.ArrayList;
import
java.util.Arrays;
import
java.util.HashMap;
import
java.util.Iterator;
import
android.app.Activity;
import
android.content.Context;
import
android.content.Intent;
import
android.hardware.Sensor;
import
android.hardware.SensorEvent;
import
android.hardware.SensorEventListener;
import
android.hardware.SensorManager;
import
android.hardware.usb.UsbConstants;
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.util.Log;
import
android.view.Gravity;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.ArrayAdapter;
import
android.widget.Button;
import
android.widget.ListView;
import
android.widget.TextView;
import
android.widget.Toast;
public
class
MissileLauncherActivity
extends
Activity {
private
static
final
String TAG =
"MissileLauncherActivity"
;
private
Button btsend;
// 发送按钮
private
UsbManager manager;
// USB管理器
private
UsbDevice mUsbDevice;
// 找到的USB设备
private
ListView lsv1;
// 显示USB信息的
private
UsbInterface mInterface;
private
UsbDeviceConnection mDeviceConnection;
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.launcher);
btsend = (Button) findViewById(R.id.btsend);
btsend.setOnClickListener(btsendListener);
lsv1 = (ListView) findViewById(R.id.lsv1);
// 获取USB设备
manager = (UsbManager) getSystemService(Context.USB_SERVICE);
if
(manager ==
null
) {
return
;
}
else
{
Log.i(TAG,
"usb设备:"
+ String.valueOf(manager.toString()));
}
HashMap<string, usbdevice=
""
> deviceList = manager.getDeviceList();
Log.i(TAG,
"usb设备:"
+ String.valueOf(deviceList.size()));
Iterator<usbdevice> deviceIterator = deviceList.values().iterator();
ArrayList<string> USBDeviceList =
new
ArrayList<string>();
// 存放USB设备的数量
while
(deviceIterator.hasNext()) {
UsbDevice device = deviceIterator.next();
USBDeviceList.add(String.valueOf(device.getVendorId()));
USBDeviceList.add(String.valueOf(device.getProductId()));
// 在这里添加处理设备的代码
if
(device.getVendorId() ==
6790
&& device.getProductId() ==
57360
) {
mUsbDevice = device;
Log.i(TAG,
"找到设备"
);
}
}
// 创建一个ArrayAdapter
lsv1.setAdapter(
new
ArrayAdapter<string>(
this
,
android.R.layout.simple_list_item_1, USBDeviceList));
findIntfAndEpt();
}
// 寻找接口和分配结点
private
void
findIntfAndEpt() {
if
(mUsbDevice ==
null
) {
Log.i(TAG,
"没有找到设备"
);
return
;
}
for
(
int
i =
0
; i < mUsbDevice.getInterfaceCount();) {
// 获取设备接口,一般都是一个接口,你可以打印getInterfaceCount()方法查看接
// 口的个数,在这个接口上有两个端点,OUT 和 IN
UsbInterface intf = mUsbDevice.getInterface(i);
Log.d(TAG, i +
" "
+ intf);
mInterface = intf;
break
;
}
if
(mInterface !=
null
) {
UsbDeviceConnection connection =
null
;
// 判断是否有权限
if
(manager.hasPermission(mUsbDevice)) {
// 打开设备,获取 UsbDeviceConnection 对象,连接设备,用于后面的通讯
connection = manager.openDevice(mUsbDevice);
if
(connection ==
null
) {
return
;
}
if
(connection.claimInterface(mInterface,
true
)) {
Log.i(TAG,
"找到接口"
);
mDeviceConnection = connection;
// 用UsbDeviceConnection 与 UsbInterface 进行端点设置和通讯
getEndpoint(mDeviceConnection, mInterface);
}
else
{
connection.close();
}
}
else
{
Log.i(TAG,
"没有权限"
);
}
}
else
{
Log.i(TAG,
"没有找到接口"
);
}
}
private
UsbEndpoint epOut;
private
UsbEndpoint epIn;
// 用UsbDeviceConnection 与 UsbInterface 进行端点设置和通讯
private
void
getEndpoint(UsbDeviceConnection connection, UsbInterface intf) {
if
(intf.getEndpoint(
1
) !=
null
) {
epOut = intf.getEndpoint(
1
);
}
if
(intf.getEndpoint(
0
) !=
null
) {
epIn = intf.getEndpoint(
0
);
}
}
private
byte
[] Sendbytes;
// 发送信息字节
private
byte
[] Receiveytes;
// 接收信息字节
private
OnClickListener btsendListener =
new
OnClickListener() {
int
ret = -
100
;
@Override
public
void
onClick(View v) {
String testString =
"010A"
;
//String testString = "C783CC30";
byte
[] bt = clsPublic.HexString2Bytes(testString);
Sendbytes = Arrays.copyOf(bt, bt.length);
// 1,发送准备命令
ret = mDeviceConnection.bulkTransfer(epOut, Sendbytes,
Sendbytes.length,
5000
);
Log.i(TAG,
"已经发送!"
);
// 2,接收发送成功信息
Receiveytes =
new
byte
[
32
];
ret = mDeviceConnection.bulkTransfer(epIn, Receiveytes,
Receiveytes.length,
10000
);
Log.i(TAG,
"接收返回值:"
+ String.valueOf(ret));
if
(ret !=
32
) {
DisplayToast(
"接收返回值"
+ String.valueOf(ret));
return
;
}
else
{
// 查看返回值
DisplayToast(clsPublic.Bytes2HexString(Receiveytes));
Log.i(TAG, clsPublic.Bytes2HexString(Receiveytes));
}
}
};
public
void
DisplayToast(CharSequence str) {
Toast toast = Toast.makeText(
this
, str, Toast.LENGTH_LONG);
// 设置Toast显示的位置
toast.setGravity(Gravity.TOP,
0
,
200
);
// 显示Toast
toast.show();
}
}
|
这里还有一个需要解决的问题就是如何设置输入输出报文,函数是controlTransfer(requestType, request, value, index, buffer, length, timeout)
但是始终没有弄清楚参数如何传递,还望高手相助