服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android Usb设备的监听(Dev)外设端口的判定以及耳机的插拔

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

2022-09-01 13:27ChaoLi_Chen Android

今天小编就为大家分享一篇关于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里面。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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工具类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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开头的是十六位的 然后转化成十进制的数就能看到自己的东西了)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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://blog.csdn.net/ChaoLi_Chen/article/details/51313616

延伸 · 阅读

精彩推荐