有线耳机与蓝牙耳机的连接监听(插入、拔出、处于连接状态)

编程入门 行业动态 更新时间:2024-10-10 10:27:57

有线耳机与<a href=https://www.elefans.com/category/jswz/34/1766283.html style=蓝牙耳机的连接监听(插入、拔出、处于连接状态)"/>

有线耳机与蓝牙耳机的连接监听(插入、拔出、处于连接状态)

有线耳机与蓝牙耳机的连接监听(插入、拔出、处于连接状态)

author —Dabin


  
  项目要做插入耳机的判断进行一些逻辑操作,刚开始只写入了,插入或者拔出的监听,但是呢,耳机处于连接状态的改怎么判断呢,把声音管理器给忘记了,搜索了一下,代码如下,注释很清楚了。在这记录一下。

package com.example.dabin.headset;import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothHeadset;
import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.AudioManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);int i = getheadsetStatsu();  //刚进来获取状态if(i == 1){Toast.makeText(this, "有线耳机处于连接状态", Toast.LENGTH_SHORT).show();}else if(i == 2){Toast.makeText(this, "蓝牙耳机处于连接状态", Toast.LENGTH_SHORT).show();}else {Toast.makeText(this, "无耳机正在连接", Toast.LENGTH_SHORT).show();}registerHeadsetPlugReceiver();}/*** 获取耳机的连接状态* @return 根据返回的int值进行自己的逻辑操作*/public int getheadsetStatsu(){AudioManager audoManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); //获取声音管理器if(audoManager.isWiredHeadsetOn()){  //有限耳机是否连接return 1;}BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter();  //蓝牙耳机if (ba == null){ //若蓝牙耳机无连接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) {return 2;}}return -2;}/*** 注册监听*/private void registerHeadsetPlugReceiver() {IntentFilter intentFilter = new IntentFilter();intentFilter.addAction(Intent.ACTION_HEADSET_PLUG);registerReceiver(headsetPlugReceiver, intentFilter);IntentFilter bluetoothFilter = new IntentFilter(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);registerReceiver(headsetPlugReceiver, bluetoothFilter);}private BroadcastReceiver headsetPlugReceiver = new BroadcastReceiver() {@Overridepublic 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)) {Toast.makeText(context, ";蓝牙耳机断开连接了", Toast.LENGTH_SHORT).show();}else{Toast.makeText(context, "蓝牙耳机连接上了", Toast.LENGTH_SHORT).show();}} else if ("android.intent.action.HEADSET_PLUG".equals(action)) {if (intent.hasExtra("state")) {if (intent.getIntExtra("state", 0) == 0) {Toast.makeText(context, "有线拔出", Toast.LENGTH_SHORT).show();}else if(intent.getIntExtra("state", 0) == 1){Toast.makeText(context, "有线插入", Toast.LENGTH_SHORT).show();}}}}};@Overrideprotected void onDestroy() {super.onDestroy();unregisterReceiver(headsetPlugReceiver);}
}

更多推荐

有线耳机与蓝牙耳机的连接监听(插入、拔出、处于连接状态)

本文发布于:2024-03-08 16:58:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1721529.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:蓝牙耳机   耳机   状态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!