哈罗大家好。生活总是这样计划赶不上变化,今天为大家分享一下新加的小功能--使用Android设备连接HID设备。
安卓内部已经内置了丰富的驱动,所以一般的设备我们只需要简单是设置就可灵活使用。

- 首先对清单文件做简单修改 
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="cool.wipi.myapplication" >   <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/Theme.WiBox" >        <activity            android:name=".MainActivity"            android:label="@string/title_activity_main" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.HOME" />                <category android:name="android.intent.category.LAUNCHER" />                <category android:name="android.intent.category.DEFAULT" />            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"                />        activity>    application>manifest>- 编写java代码 
public class SerialFragment extends Fragment {    UsbManager usbManager;    HashMap deviceList;    String info="";    TextView tvInfo;    TextView svInfo;    UsbDevice myDevice = null;    UsbDeviceConnection myConnection;    UsbInterface myUsbInterface;    //------------------------------    UsbEndpoint writeEP,readEP;    Button bt_Write,bt_read,bt;    //------------------------------    byte [] writerBuf = new byte[4096];    byte [] readBuf   = new byte[4096];    //-----------------------------    private SerialViewModel mViewModel;    public static SerialFragment newInstance() {        return new SerialFragment();    }    @Override    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,                             @Nullable Bundle savedInstanceState) {        return inflater.inflate(R.layout.fragment_serial, container, false);    }    @Override    public void onActivityCreated(@Nullable Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);        mViewModel = new ViewModelProvider(this).get(SerialViewModel.class);        // TODO: Use the ViewModel        tvInfo   = getActivity().findViewById(R.id.tv1);        svInfo   = getActivity().findViewById(R.id.tv2);        bt_read  = getActivity().findViewById(R.id.bt_read);        bt_Write = getActivity().findViewById(R.id.bt_write);        bt       = getActivity().findViewById(R.id.button);        //------------------------------------------------       // TextView tv = (TextView) getView().findViewById(R.id.bluetooth_tv);        usbManager  = (UsbManager)getActivity().getApplicationContext().getSystemService(Context.USB_SERVICE);        deviceList = usbManager.getDeviceList();        showInfo("find"+deviceList.size()+"device");        for (UsbDevice device:deviceList.values()){            showInfo("VID"+device.getVendorId()+"PID"+device.getProductId());            if (device.getVendorId()== 0x03C3 && device.getProductId() == 0x1F01){                myDevice = device;            }            if (myDevice != null && usbManager.hasPermission(myDevice)){                showInfo("hasPermission");                myConnection = usbManager.openDevice(myDevice);                myUsbInterface = myDevice.getInterface(0);                myConnection.claimInterface(myUsbInterface,false);                int interfaceCount = myUsbInterface.getEndpointCount();                showInfo("interfaceCount"+interfaceCount);               //----------------------------------------------                for(int i = 0;i                    if(myUsbInterface.getEndpoint(i).getDirection() == UsbConstants.USB_DIR_IN){                        readEP = myUsbInterface.getEndpoint(i);                    }else{                        writeEP = myUsbInterface.getEndpoint(i);                    }                }            }else{                showInfo("no hasPermission");            }            bt_Write.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View v) {                    int checkSum = 0;                    Random rd  = new Random();                    for (int i = 0 ; i < 4096 ; i++){                         writerBuf[i] = (byte) rd.nextInt(255);                        checkSum += (writerBuf[i]&0x0ff);                    }                    String firstTen = "firsTen:";                    String lastTen = "lastTen:";                    for(int i=0;i < 10;i++){                        firstTen +=(writerBuf[i] & 0X0ff)+",";                        lastTen +=(writerBuf[4086+i] & 0x0ff)+",";                    }                    showInfo(firstTen);                    showInfo(lastTen);                    showInfo("sheckSum:"+checkSum);                    if (writeEP != null){//if (writerBuf != null){                        int count = 0;                        count = myConnection.bulkTransfer(writeEP,writerBuf,4096,500);                        showInfo("BulkTranster count :" + count);                    }                }            });            bt_read.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View v) {                    if (readEP != null){//if (writerBuf != null){                        int count = 0,checkSum = 0;                        count = myConnection.bulkTransfer(readEP,readBuf,4096,500);                        showInfo("Read BulkTranster count :" + count);                        //---------------------                        for (int i = 0 ; i < 4096 ; i++){                            checkSum += (readBuf[i]&0x0ff);                        }                        String firstTen = "firsTen:";                        String lastTen = "lastTen:";                        for(int i=0;i < 10;i++){                            firstTen +=(readBuf[i] & 0X0ff)+",";                            lastTen +=(readBuf[4086+i] & 0x0ff)+",";                        }                        showInfo(firstTen);                        showInfo(lastTen);                        showInfo("sheckSum:"+checkSum);                        showInfo("bt_read");                    }                }            });            bt.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View v) {                    showInfo("button");                }            });        }    }    void showInfo(String str){        info += str +"\n";        tvInfo.setText(info);        svInfo.setText(info);    }}- 运行效果 

点击查看原文了解更多www.wipi.cool