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

来源:互联网 发布:matlab 随机矩阵 编辑:程序博客网 时间:2024/06/10 14:47

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

2 0
原创粉丝点击