当前位置: 移动技术网 > IT编程>移动开发>Android > Android Usb设备的监听(Dev)外设端口的判定以及耳机的插拔

Android Usb设备的监听(Dev)外设端口的判定以及耳机的插拔

2019年07月31日  | 移动技术网IT编程  | 我要评论

陈奕迅 黄伟文,菠崃史特,c1008

最近在公司用到外设,需要判断接入的外设的vendorid和productid,然后给大家说一下自己的学习成果把 ,首先我门可以通过android.hardware.usb.action.usb_state监听自己的usb连接的设备,只针对usb设备。而想要监听外部设备的时候却需要另外的两个广播进行监听"android.hardware.usb.action.usb_device_attached""android.hardware.usb.action.usb_device_detached"。要是想对耳机或者耳机的状态进行监听的时候需要的广播是"android.intent.action.headset_plug" 通过

int inttype=intent.getintextra("microphone",0)来获取耳机是否有麦克风。inttype==0表示没有耳机inttype==1表示有耳机

我个人的建议就是将一部分代码(根据个人情况而定)放到服务里面,或者是application里面。

import com.example.usbusb.utils.toastutils;
import android.app.activity;
import android.content.broadcastreceiver;
import android.content.context;
import android.content.intent;
import android.content.intentfilter;
import android.os.bundle;
import android.widget.toast;
public class mainactivity extends activity {
 //耳机的广播
 public static final string taglisten = "android.intent.action.headset_plug";
 //usb线的广播
 private final static string tagusb = "android.hardware.usb.action.usb_state";
 //外设的广播
 public static final string tagin = "android.hardware.usb.action.usb_device_attached";
 public static final string tagout = "android.hardware.usb.action.usb_device_detached";
  private boolean boolean=false; 
 @override
 protected void oncreate(bundle savedinstancestate) {
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.activity_main);
  //赛选器
 intentfilter filter = new intentfilter();
 //筛选的条件
 filter.addaction(tagin);
 filter.addaction(tagout);
 filter.addaction(tagusb);
 //注册广播 动态注册
 registerreceiver(receiver, filter);
 }
 /**
 * 创建广播的类
 */
 broadcastreceiver receiver = new broadcastreceiver() {
 @override
 public void onreceive(context context, intent intent) {
  string action = intent.getaction();
  //判断外设
  if (action.equals(tagin)) {
  toastutils.shwotoast(context, "外设已经连接");
  //toast.maketext(context, "外设已经连接", toast.length_short).show();
  }
  if (action.equals(tagout)) {
  if (boolean) {
   toastutils.shwotoast(context, "外设已经移除");
   //toast.maketext(context, "外设已经移除", toast.length_short).show();
  }
  }
  //判断存储usb
  if (action.equals(tagusb)) {
  boolean connected = intent.getextras().getboolean("connected");
  if (connected) {
   toastutils.shwotoast(context, "usb 已经连接");
   //toast.maketext(mainactivity.this, "usb 已经连接",toast.length_short).show();
  
  } else {
   if (boolean) {
   toastutils.shwotoast(context, "usb 断开");
   //toast.maketext(mainactivity.this, "usb 断开",toast.length_short).show();
   }
  }
  }
  //判断耳机
  if (action.equals(taglisten)) {
  int intextra = intent.getintextra("state", 0);
  // state --- 0代表拔出,1代表插入
  // name--- 字符串,代表headset的类型。
  // microphone -- 1代表这个headset有麦克风,0则没有
  // int i=intent.getintextra("",0);
  if (intextra == 0) {
   if (boolean) {
   toastutils.shwotoast(context,"拔出耳机");
   //toast.maketext(context, "拔出耳机", toast.length_short).show();
   }
  }
  if (intextra == 1) {
   toastutils.shwotoast(context, "耳机插入");
   //toast.maketext(context, "耳机插入", toast.length_short).show();
   int inttype = intent.getintextra("microphone", 0);
   if (inttype == 0) {
   toastutils.shwotoast(context, "没有麦克风");
   //toast.maketext(context, "没有麦克风" + inttype,toast.length_short).show();
   }
   if (inttype == 1) {
   toastutils.shwotoast(context,"有话筒" );
   //toast.maketext(context, "有话筒" + inttype,toast.length_short).show();
   }
  } 
  }
  boolean=true;
 }
 };
 /**
 * 注销广播
 */
 protected void ondestroy() {
 unregisterreceiver(receiver);
 };
}

toastutils工具类

import android.content.context;
import android.widget.toast;
public class toastutils {
 public static toast toast=null;
 private toastutils toastutils=new toastutils();
 private toastutils(){}
  public static void shwotoast(context context,string msg){
   if (toast==null) {
  toast=toast.maketext(context, msg, toast.length_short);
 }else {
  if (toast!=null) {
  toast.settext(msg);
  }
 }
   toast.show(); 
 }
}

下面的一个就是获取每一个id的端口号通过在usb的广播里面调用这个方法判断是否是自己的设备,这样就可完成自己想要的操作了(注意当看到设备的id是以0x开头的是十六位的 然后转化成十进制的数就能看到自己的东西了)

import java.util.hashmap;
import android.annotation.suppresslint;
import android.content.context;
import android.hardware.usb.usbdevice;
import android.hardware.usb.usbmanager;
import android.os.bundle;
import android.support.v7.app.actionbaractivity;
import android.util.log;
 public class mainactivity extends actionbaractivity {
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);
    usbmanager usbmanager = (usbmanager) getsystemservice(context.usb_service);
    hashmap<string, usbdevice> map = usbmanager.getdevicelist();
    system.out.println("......................befor....................................");
    for(usbdevice device : map.values()){
     system.out.println(".......one..........dname: " + device.getdevicename());
     system.out.println(".......tow.........vid: " + device.getvendorid() + "\t pid: " + device.getproductid());
    }
    system.out.println("........................after..................................");
  }

结果我们都能看到有两个设备

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对移动技术网的支持。如果你想了解更多相关内容请查看下面相关链接

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网