android 判断有线耳机、蓝牙耳机连接

参考: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++;
            }
         }
      }
   }

};

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值