您可以使用 USB 串行库,例如 https://github.com/mik3y/usb-serial-for-android,并提供您的 USB OTG 设备的供应商和产品 ID 来控制它。因此,您可以在任何监视器中捕获左、右、上、下和十六进制代码,并根据原始字节进行类似切换的操作。
UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback() {
//Defining a Callback which triggers whenever data is read.
@Override
public void onReceivedData(byte[] arg0) {
String data = null;
try {
data = new String(arg0, "UTF-8");
data.concat("/n");
switch() // etc
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
};
让我们首先尝试将我们的 android 手机连接到 USB otg 设备。至于产品和供应商 ID,您可以在将其插入 PC 时找到它。如果建立了连接,那么剩下的就来了。如果您能在此处分享 USB Otg 设备数据表,将会很有帮助。
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
ProbeTable customTable = new ProbeTable();
customTable.addProduct(0x0403, 0x6001, CdcAcmSerialDriver.class);
List<UsbSerialDriver> availableDrivers = new UsbSerialProber(customTable).findAllDrivers(manager);
ArrayAdapter<String> deviceList = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_2);
ListView listDevices = (ListView) findViewById(R.id.listView);
if(!availableDrivers.isEmpty()) {
for(UsbSerialDriver driver: availableDrivers) {
deviceList.add(driver.getDevice().getDeviceName());
}
listDevices.setAdapter(deviceList);
} else {
Toast.makeText(getApplicationContext(), "No devices found", Toast.LENGTH_LONG).show();
}
}
}
如果是 USB 隐藏设备,尝试此代码可能会有所帮助;
UsbManager mManager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = mManager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
while (deviceIterator.hasNext())
{
UsbDevice device = deviceIterator.next();
Log.i(TAG,"Model: " + device.getDeviceName());
Log.i(TAG,"ID: " + device.getDeviceId());
Log.i(TAG,"Class: " + device.getDeviceClass());
Log.i(TAG,"Protocol: " + device.getDeviceProtocol());
Log.i(TAG,"Vendor ID " + device.getVendorId());
Log.i(TAG,"Product ID: " + device.getProductId());
Log.i(TAG,"Interface count: " + device.getInterfaceCount());
Log.i(TAG,"---------------------------------------");
// Get interface details
for (int index = 0; index < device.getInterfaceCount(); index++)
{
UsbInterface mUsbInterface = device.getInterface(index);
Log.i(TAG," ***** *****");
Log.i(TAG," Interface index: " + index);
Log.i(TAG," Interface ID: " + mUsbInterface.getId());
Log.i(TAG," Inteface class: " + mUsbInterface.getInterfaceClass());
Log.i(TAG," Interface protocol: " + mUsbInterface.getInterfaceProtocol());
Log.i(TAG," Endpoint count: " + mUsbInterface.getEndpointCount());
// Get endpoint details
for (int epi = 0; epi < mUsbInterface.getEndpointCount(); epi++)
{
UsbEndpoint mEndpoint = mUsbInterface.getEndpoint(epi);
Log.i(TAG," ++++ ++++ ++++");
Log.i(TAG," Endpoint index: " + epi);
Log.i(TAG," Attributes: " + mEndpoint.getAttributes());
Log.i(TAG," Direction: " + mEndpoint.getDirection());
Log.i(TAG," Number: " + mEndpoint.getEndpointNumber());
Log.i(TAG," Interval: " + mEndpoint.getInterval());
Log.i(TAG," Packet size: " + mEndpoint.getMaxPacketSize());
Log.i(TAG," Type: " + mEndpoint.getType());
}
}
}
Log.i(TAG," No more devices connected.");
}