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

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

最近在公司用到外设,需要判断接入的外设的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..................................");
  }

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

总结

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

本文由 华域联盟 原创撰写:华域联盟 » Android Usb设备的监听(Dev)外设端口的判定以及耳机的插拔

转载请保留出处和原文链接:https://www.cnhackhy.com/106271.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们