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

来源:互联网 发布:sfda数据查询 编辑:程序博客网 时间:2024/05/21 09:02

最近在公司用到外设,需要判断接入的外设的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里面。


[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. import com.example.usbusb.utils.ToastUtils;  
  2. import android.app.Activity;  
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.content.IntentFilter;  
  7. import android.os.Bundle;  
  8. import android.widget.Toast;  
  9. public class MainActivity extends Activity {  
  10.     //耳机的广播  
  11.     public static final String TAGLISTEN = "android.intent.action.HEADSET_PLUG";  
  12.     //usb线的广播  
  13.     private final static String TAGUSB = "android.hardware.usb.action.USB_STATE";  
  14.     //外设的广播  
  15.     public static final String TAGIN = "android.hardware.usb.action.USB_DEVICE_ATTACHED";  
  16.     public static final String TAGOUT = "android.hardware.usb.action.USB_DEVICE_DETACHED";  
  17.    private boolean BOOLEAN=false;   
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_main);  
  22.             //赛选器  
  23.         IntentFilter filter = new IntentFilter();  
  24.         //筛选的条件  
  25.         filter.addAction(TAGIN);  
  26.         filter.addAction(TAGOUT);  
  27.         filter.addAction(TAGUSB);  
  28.         //注册广播 动态注册  
  29.         registerReceiver(receiver, filter);  
  30.     }  
  31.       
  32.     /** 
  33.      * 创建广播的类 
  34.      */  
  35.     BroadcastReceiver receiver = new BroadcastReceiver() {  
  36.         @Override  
  37.         public void onReceive(Context context, Intent intent) {  
  38.             String action = intent.getAction();  
  39.             //判断外设  
  40.             if (action.equals(TAGIN)) {  
  41.                 ToastUtils.shwotoast(context, "外设已经连接");  
  42.                 //Toast.makeText(context, "外设已经连接", Toast.LENGTH_SHORT).show();  
  43.                   
  44.             }  
  45.             if (action.equals(TAGOUT)) {  
  46.                 if (BOOLEAN) {  
  47.                     ToastUtils.shwotoast(context,  "外设已经移除");  
  48.                     //Toast.makeText(context, "外设已经移除", Toast.LENGTH_SHORT).show();  
  49.                 }  
  50.             }  
  51.             //判断存储usb  
  52.             if (action.equals(TAGUSB)) {  
  53.                 boolean connected = intent.getExtras().getBoolean("connected");  
  54.                 if (connected) {  
  55.                     ToastUtils.shwotoast(context, "USB 已经连接");  
  56.                     //Toast.makeText(MainActivity.this, "USB 已经连接",Toast.LENGTH_SHORT).show();  
  57.                   
  58.                 } else {  
  59.                     if (BOOLEAN) {  
  60.                         ToastUtils.shwotoast(context, "USB 断开");  
  61.                         //Toast.makeText(MainActivity.this, "USB 断开",Toast.LENGTH_SHORT).show();  
  62.                     }  
  63.                   
  64.                 }  
  65.             }  
  66.             //判断耳机  
  67.             if (action.equals(TAGLISTEN)) {  
  68.                 int intExtra = intent.getIntExtra("state"0);  
  69.                 // state --- 0代表拔出,1代表插入  
  70.                 // name--- 字符串,代表headset的类型。  
  71.                 // microphone -- 1代表这个headset有麦克风,0则没有  
  72.                 // int i=intent.getIntExtra("",0);  
  73.                 if (intExtra == 0) {  
  74.                     if (BOOLEAN) {  
  75.                         ToastUtils.shwotoast(context,"拔出耳机");  
  76.                         //Toast.makeText(context, "拔出耳机", Toast.LENGTH_SHORT).show();  
  77.                     }  
  78.                 }  
  79.                 if (intExtra == 1) {  
  80.                     ToastUtils.shwotoast(context, "耳机插入");  
  81.                     //Toast.makeText(context, "耳机插入", Toast.LENGTH_SHORT).show();  
  82.                     int intType = intent.getIntExtra("microphone"0);  
  83.                     if (intType == 0) {  
  84.                         ToastUtils.shwotoast(context, "没有麦克风");  
  85.                         //Toast.makeText(context, "没有麦克风" + intType,Toast.LENGTH_SHORT).show();  
  86.                     }  
  87.                     if (intType == 1) {  
  88.                         ToastUtils.shwotoast(context,"有话筒" );  
  89.                         //Toast.makeText(context, "有话筒" + intType,Toast.LENGTH_SHORT).show();  
  90.                     }  
  91.                 }  
  92.                   
  93.             }  
  94.             BOOLEAN=true;  
  95.         }  
  96.           
  97.     };  
  98.     /** 
  99.      * 注销广播 
  100.      */  
  101.     protected void onDestroy() {  
  102.         unregisterReceiver(receiver);  
  103.     };  
  104.       
  105.       
  106. }  


ToastUtils工具类



[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. import android.content.Context;  
  2. import android.widget.Toast;  
  3. public class ToastUtils {  
  4.     public static Toast toast=null;  
  5.     private ToastUtils toastUtils=new ToastUtils();  
  6.     private ToastUtils(){}  
  7.     public static void shwotoast(Context context,String msg){  
  8.         if (toast==null) {  
  9.             toast=Toast.makeText(context, msg, Toast.LENGTH_SHORT);  
  10.         }else {  
  11.             if (toast!=null) {  
  12.                 toast.setText(msg);  
  13.             }  
  14.         }  
  15.         toast.show();     
  16.   }  
  17. }  


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

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. import java.util.HashMap;  
  2.   
  3. import android.annotation.SuppressLint;  
  4. import android.content.Context;  
  5. import android.hardware.usb.UsbDevice;  
  6. import android.hardware.usb.UsbManager;  
  7. import android.os.Bundle;  
  8. import android.support.v7.app.ActionBarActivity;  
  9. import android.util.Log;  
  10.   
  11.   
  12.  public class MainActivity extends ActionBarActivity {  
  13.   
  14.     protected void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.activity_main);  
  17.         UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);  
  18.         HashMap<String, UsbDevice> map = usbManager.getDeviceList();  
  19.         System.out.println("......................befor....................................");  
  20.         for(UsbDevice device : map.values()){  
  21.              System.out.println(".......one..........dName: " + device.getDeviceName());  
  22.              System.out.println(".......tow.........vid: " + device.getVendorId() + "\t pid: " + device.getProductId());  
  23.         }  
  24.         System.out.println("........................after..................................");  
  25.           
  26.     }  
  27.       

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


0 0
原创粉丝点击