哈罗大家好。生活总是这样计划赶不上变化,今天为大家分享一下新加的小功能--使用Android设备连接HID设备。

安卓内部已经内置了丰富的驱动,所以一般的设备我们只需要简单是设置就可灵活使用。

  1. 首先对清单文件做简单修改

<?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>
  1. 编写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);    }}
  1. 运行效果

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

android byte[] 转string 好多问号_#WIPI# Android使用HID设备相关推荐

  1. android byte[] 转string 好多问号_Android 仿抖音实现动态壁纸

    code小生,一个专注 Android 领域的技术平台 公众号回复 Android 加入我的安卓技术群 作者:7_px 链接:https://www.jianshu.com/p/fc5cf284abb ...

  2. android byte[] 转string 好多问号_java程序员面试遇到string题如何不凉?

    原标题:java程序员面试遇到string题如何不凉? 最近看到好多同学都在储备面试知识,以备来年轻松应对面试官,拿到心仪offer,之前好多同学反映遇到string,都只能送给自己一首凉凉.别凉,今 ...

  3. android 一个字符串分两行显示_【Android】DataBindinglt;中gt;

    DataBindingUtil类 DataBinding不仅可以绑定Activity还可以绑定视图内容(View) // 视图static extends ViewDataBinding> T ...

  4. android 获取sd卡目录失败_解决Android手机自带内部存储路径存在但是却获取不到内容的问题...

    我有一台中兴的Android手机,型号是 ZTE U930HD,手机没有插入外置SD卡(也就是Micro SD Card,原名Trans-flash Card(TF卡),2004年正式更名为Micro ...

  5. android p wifi一直在扫描_在Android上的每次WiFi扫描之间我应该使用什么时间间隔?...

    我需要定期执行Wifi扫描.当时间间隔设置为1-2秒时,我遇到了问题.好像我没有得到任何ScanResult.是否有最短的时间设置,以便WifiManager能够执行成功的WiFi扫描? 这是代码.我 ...

  6. android刷新时的圆形动画_【Android】圆形揭露动画

    在Android系统中提供了一种圆形的揭露动画,具体表现为一个view以圆形的形式展开.揭示.所谓揭露动画,就是一种用于 View 之间,甚至界面之间的特殊过渡动画效果.通过ViewAnimation ...

  7. android控件的touch事件_聊聊Android嵌套滑动

    聊聊Android嵌套滑动 最近工作中遇到了需求是使用 Bottom-Sheet 交互的弹窗,使用了 design 包里面的 CoordinatorLayout 和 BottomSheetBehavi ...

  8. 以下哪些属于android控件的touch事件?_聊聊 Android 的 GUI 系统

    你长得辣么好看,我想着要更详细地了解你.今天,让我们一起来聊聊 Android 的 GUI 系统. 缘起 在2019年的 Google I/O 大会上,Jetpack 团队首次为大家介绍了 Jetpa ...

  9. android tf卡及u盘_在Android中读写U盘

    原文链接:https://www.zybuluo.com/Tyhj/note/1144629 最近工作中遇到数据从U盘导出的功能,网上找了一下,有个开源的框架可以拿来使用,U盘和内存卡什么的不一样,是 ...

最新文章

  1. 【PC工具】更新简单好用绿色IP地址扫描工具,内部网络设备查看工具,内网ip查询ip扫描工具,电脑IP地址查看方法...
  2. 每一个项目都可以有一个项目blog page
  3. 聊聊如何设计千万级吞吐量的.Net Core网络通信!
  4. 攻击者“完全自动化”发动NPM供应链攻击
  5. linux下LAMP环境的搭配
  6. SpringMVC中404错误解决方法总结
  7. Warning: The TensorFlow library wasn't compiled to use SSE,SSE2,SSE3,SSE4.1 instructions
  8. Mac上安装homebrew(类似于Linux上的apt-get)
  9. 普林斯顿大学计算机系,普林斯顿大学计算机科学系
  10. OpenCV_(Based on Sobel Filter to Detect edges) 基于Sobel算子的方向滤波器检查边缘
  11. WPF在资源内嵌入字体
  12. 【专题6: 其他知识】 之 【2.1.关于择业和就业_嵌入式学习路线和方法】
  13. 用idea使用struts和hibernate来实现CRUB的操作
  14. EasyBoot教程三:制作GHOST多重启动盘方法
  15. 关于Kaggle入门,看这一篇就够了
  16. 【第59篇】MetaFormer实际上是你所需要的视觉
  17. 机械指令 对应 汇编指令
  18. html输入某天得到周几,HTML “input week年周”输入控件简介说明
  19. 微信小程序之获取skey保持登陆状态
  20. 【微信】订阅号配置自定义菜单

热门文章

  1. 11月7日邀您参加成都微软MVP圆桌之夜!
  2. 一码阻塞,万码等待:ASP.NET Core 同步方法调用异步方法“死锁”的真相
  3. Entity Framework Core 使用HiLo生成主键
  4. 发达国家与发展中国家编程语言技术的分布差异性
  5. Python版九九乘法表
  6. C语言试题五十八之请编写函数fun,:计算并输出下列多项式的值(sn=1+1/1!+1/2!+1/3!+1/4!+…+1/n! )
  7. Fiddler之如何通过浏览器输入链接地址修改页面返回数据的内容
  8. linux之ubunt把启动栏底部和左边切换
  9. Android之导入项目提示Android requires compiler compliance level 5.0 or 6.0. Found ‘1.8‘ instead解决办法
  10. Android之切换账号登录依然能登录成功问题解决办法