问题描述
我正在尝试通过OTG获取Freestyle Libre-方丈(血糖仪)的数据。
此设备是HID设备。它有2个接口:
{"interfaceNumber":0,"direction":"in","maximumPacketSize":64,"pollingInterval":1,"type":"interrupt"}
{"interfaceNumber":1,"type":"interrupt"}
您可以将这两个接口的方向设置为“输入”,每个接口的端点为“中断”( UsbConstants.USB_ENDPOINT_XFER_INT )
我试图改写“ Missile Launcher”的代码
下面是我的代码:
公共类MainActivity扩展了Activity {
private static final String TAG = "MainActivity";
private UsbManager mUsbManager;
private UsbDevice mDevice;
private UsbDeviceConnection mConnection;
private UsbEndpoint mReadEndpoint;
private UsbEndpoint mWriteEndpoint;
private static final int COMMAND_STATUS = 0x04;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mUsbManager = (UsbManager)getSystemService(Context.USB_SERVICE);
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onResume() {
super.onResume();
Intent intent = getIntent();
Log.d(TAG,"intent: " + intent);
String action = intent.getAction();
UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
setDevice(device);
} else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
if (mDevice != null && mDevice.equals(device)) {
setDevice(null);
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
}
private void setDevice(UsbDevice device) {
Log.d(TAG,"setDevice " + device);
if (device.getInterfaceCount() <= 0) {
Log.e(TAG,"Could not find interface" + device.getInterfaceCount());
return;
}
UsbInterface intf = device.getInterface(0);
// device should have one endpoint
if (intf.getEndpointCount() <= 0) {
Log.e(TAG,"Could not find endpoint");
return;
}
// endpoint should be of type interrupt
UsbEndpoint ep = intf.getEndpoint(0);
if (ep.getType() != UsbConstants.USB_ENDPOINT_XFER_INT) {
Log.e(TAG,"endpoint is not interrupt type");
return;
}
mDevice = device;
mReadEndpoint = ep;
if (device != null) {
UsbDeviceConnection connection = mUsbManager.openDevice(device);
if (connection != null && connection.claimInterface(intf,true)) {
Log.d(TAG,"open SUCCESS");
mConnection = connection;
Thread thread = new Thread(){ public void run() {
ByteBuffer buffer = ByteBuffer.allocate(64);
UsbRequest request = new UsbRequest();
request.initialize(mConnection,mReadEndpoint);
byte status = -1;
while (true) {
// queue a request on the interrupt endpoint
request.queue(buffer,64);
// send poll status command
sendCommand(COMMAND_STATUS);
// wait for status event
if (mConnection.requestWait() == request) {
// Log.d(TAG,"transfer going on");
// Log.d(TAG,"byte s " + new String(buffer.array()));
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
} else {
Log.d(TAG,"transfer is finished");
break;
}
}
Log.d(TAG,"buffer size " + buffer.array().length);
Log.d(TAG," final byte s " + new String(buffer.array()));
}};
thread.start();
} else {
Log.d(TAG,"open FAIL");
mConnection = null;
}
}
}
private void sendCommand(int control) {
synchronized (this) {
if (control != COMMAND_STATUS) {
Log.d(TAG,"Command " + control);
}
if (mConnection != null) {
byte[] message = new byte[64];
message[0] = 0x04;
// message[0] = (byte)control;
// Send command via a control request on endpoint zero
int test = mConnection.controlTransfer(0x21,0x9,0x200,message,message.length,0);
Log.d(TAG,"test control " + test);
if (test > 0 ) {
int testb = mConnection.controlTransfer(0x81,150);
if (testb > 0) {
for (int i = 0; i< message.length; i++){
Log.d("message","i:" + i + " = " + message[i]);
}
} else {
Log.d(TAG,"test bulk " + testb);
}
} else {
Log.d(TAG,"test control " + test);
}
}
}
}
}
我的清单:
<?xml version="1.0" encoding="utf-8"?>
<uses-feature android:name="android.hardware.usb.host" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<Meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />
</activity>
</application>
数据传输永远不会结束(传输完成后应该中断循环)。
获取缓冲区的结果:MainActivity: byte s 0����������������������������������������������������������������������������������������������������������������������
所以我的问题是:最终能够从该设备读取数据时我缺少什么?我做错了什么?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)