参考:http://blog.csdn.net/mu399/article/details/38516039

程序加载时获取有线耳机状态:

 AudioManager  audoManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

      mContext = getApplicationContext();
      mContext.registerReceiver(mReceiver, makeFilter());
      if(audoManager.isWiredHeadsetOn()){Toast.makeText(MainActivity.this,"耳机ok",Toast.LENGTH_SHORT).show();
      }else{Toast.makeText(MainActivity.this,"耳机不ok",Toast.LENGTH_SHORT).show();
      }

程序加载时获取蓝牙耳机与有限耳机的状态:

 public int getheadsetStatsu(){AudioManager  audoManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

//      IntentFilter iFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
//      Intent iStatus = registerReceiver(null, iFilter);
//      boolean isConnected = iStatus.getIntExtra("state", 0) == 1;
//
//      if(isConnected){
//         Toast.makeText(MainActivity.this,"耳机ok",Toast.LENGTH_SHORT).show();
//      }

      if(audoManager.isWiredHeadsetOn()){return 1;
      }else{Toast.makeText(MainActivity.this,"耳机不ok",Toast.LENGTH_SHORT).show();
      }ba = BluetoothAdapter.getDefaultAdapter();

//      int isBlueCon;//蓝牙适配器是否存在,即是否发生了错误
      if (ba == null){
//         isBlueCon = -1;     //error
         return -1;
      }else if(ba.isEnabled()) {int a2dp = ba.getProfileConnectionState(BluetoothProfile.A2DP);              //可操控蓝牙设备,如带播放暂停功能的蓝牙耳机
         int headset = ba.getProfileConnectionState(BluetoothProfile.HEADSET);        //蓝牙头戴式耳机,支持语音输入输出
         int health = ba.getProfileConnectionState(BluetoothProfile.HEALTH);          //蓝牙穿戴式设备

         //查看是否蓝牙是否连接到三种设备的一种,以此来判断是否处于连接状态还是打开并没有连接的状态
         int flag = -1;
         if (a2dp == BluetoothProfile.STATE_CONNECTED) {flag = a2dp;
         } else if (headset == BluetoothProfile.STATE_CONNECTED) {flag = headset;
         } else if (health == BluetoothProfile.STATE_CONNECTED) {flag = health;
         }//说明连接上了三种设备的一种
         if (flag != -1) {
//            isBlueCon = 1;            //connected
            return 2;
         }}return -2;
   }

添加蓝牙耳机与有限耳机的监听:

private int isheadset=2;

//默认值为2,这样,软件启动时,默认耳机是正常的:

private void registerHeadsetPlugReceiver() {IntentFilter intentFilter = new IntentFilter();
   intentFilter.addAction("android.intent.action.HEADSET_PLUG");
   registerReceiver(headsetPlugReceiver, intentFilter);

   // for bluetooth headset connection receiver
   IntentFilter bluetoothFilter = new IntentFilter(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
   registerReceiver(headsetPlugReceiver, bluetoothFilter);
}private BroadcastReceiver headsetPlugReceiver = new BroadcastReceiver() {@Override
   public void onReceive(Context context, Intent intent) {String action = intent.getAction();
      if (BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED.equals(action)) {BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
         if(BluetoothProfile.STATE_DISCONNECTED == adapter.getProfileConnectionState(BluetoothProfile.HEADSET)) {isheadset--; //Bluetooth headset is now disconnected
         }else{isheadset++;
         }} else if ("android.intent.action.HEADSET_PLUG".equals(action)) {if (intent.hasExtra("state")) {if (intent.getIntExtra("state", 0) == 0) {isheadset--;
            }else if(intent.getIntExtra("state", 0) == 1){isheadset++;
            }}}}};

android 判断有线耳机、蓝牙耳机连接相关推荐

  1. Android 判断是否网络连接, 判断是否为WIFI,移动网络以及跳转网络设置界面

    这个自己在项目中总结了一个工具类 如下 public class NetWorkUtils {private NetWorkUtils() {/* cannot be instantiated */t ...

  2. Android判断当前手机连接网络

    下面来看一段代码connManager获取系统连接的服务,在通过getNetworkInfo获取当前网络的信息,最后通过getState获取当前网络的状态,从而判断手机当前连接的网络是wifi还是数据 ...

  3. android判断usb已连接,android 判断是否有网络连接(usb连接电脑问题)

    找到很多判断网络连接的工具类,如下: package client.verbank.mtp.allone.util; import android.app.Activity; import andro ...

  4. android判断蓝牙的连接情况

    需求 android系统车机上的播放器,连接手机蓝牙播放音乐,有时候需要判断蓝牙是否已经连接,但是不想麻烦的去注册一个广播,希望通过同步的查询方法判断蓝牙是否已经和其他的设备连接. 方法一 通过Blu ...

  5. android判断耳机

    原文:http://blog.csdn.net/qinghua0706/article/details/8296276 这篇文章写的是获取耳机连接状态的几种方式,不包括蓝牙耳机的连接,当然,声音从哪个 ...

  6. android监控网络是否连接(android监控网络请求apk)

    Android使用广播监听网络状态变化 一直收到系统广播 因为这个系统广播在不停的发送,系统网络状态在不停的变化.比如关闭,打开,扫描,正在连接,已连接等状态. android 代码怎么实时监控连接当 ...

  7. android 蓝牙耳机 判断,Android实现蓝牙耳机连接

    前言 最近看了下蓝牙耳机连接的问题,查阅了相关资料,再此做一个总结. 本文参考以下链接: Android实现主动连接蓝牙耳机 再次对作者表示感谢. 今天涉及的内容有: 流程讲解 新建广播Bluetoo ...

  8. Android实现蓝牙耳机连接

    代码地址如下: http://www.demodashi.com/demo/13259.html 前言 讲讲android对于蓝牙耳机连接技术的实现 今天涉及的内容有: 1. 流程讲解 2. 新建广播 ...

  9. android中怎么网络判断,Android中判断网络是否连接实例详解

    Android中判断网络是否连接实例详解 在android中,如何监测网络的状态呢,这个有的时候也是十分重要的,方法如下: public class ConnectionDetector { priv ...

最新文章

  1. htc在ubuntu上找不到devieces,提示权限不够的解决方法
  2. 如果MySQL事务中发生了网络异常?
  3. 雷达多普勒频率计算公式_非接触式雷达在线测流系统是由什么组成的?
  4. 奇奇seo优化软件_西藏seo关键词优化软件
  5. WebBrowser设置打印页眉页眉和页边距
  6. ApacheCN 学习资源汇总 2019.1
  7. IOS开发一些资源收集
  8. 数据治理需要注意什么问题
  9. python将list转为数组_python如何将list中的字符转为数字
  10. python网络编程知识_python六十七课——网络编程(基础知识了解)
  11. 对包含HttpContext.Current.Cache的代码进行单元测试
  12. 【紫书第六章】链表(list)、栈和双向队列(deque)
  13. Unity设置鼠标指针图片
  14. Ubuntu 16.04 安装opencv3及其扩展模块
  15. ES安装中文IK分词器
  16. 计算思维-卡内基梅隆大学计算机系主任周以真
  17. linux 空白进程,Linux空白行:行号显示、空白行数统计、删除空白行
  18. 陪伴我成长的学习工作邮箱品牌——TOM邮箱
  19. 语义角色标注 Semantic Role Labeling(SRL) 初探(整理英文tutorial)
  20. 2021-10-29PS自学第4天——移动工具的使用

热门文章

  1. 专题2 文件系统结构
  2. leetcode84 巨魔卡柱子
  3. php service原理,轻松搞懂WebService工作原理
  4. avrorecord.java,失败,但发生异常java.io.IOException:org.apache.avro.AvroTypeException:发现的很长,期望在配置单元中实现联合...
  5. h5如何动态获取键盘高度_动态获取键盘高度
  6. 计算机对应的自然科学,计算机类书稿加工应注意的几个问题
  7. Spring Boot 2.x基础教程:使用Elastic Job实现定时任务
  8. 90%的开发都不太考虑这个,但只要出问题直接公司完蛋!
  9. 每日一皮:程序员最讨厌的四件事!
  10. 字节跳动每一轮都会考算法吗?已拿Offer的兄弟分享经验!