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
- android 判断有线耳机、蓝牙耳机连接
- Android 蓝牙(有线)耳机监听的问题
- 连接蓝牙耳机过程
- windows10连接蓝牙耳机
- Android如何监听蓝牙耳机和有线耳机的插拔
- Android 检测是否连接蓝牙耳机
- Android实现主动连接蓝牙耳机
- Cubietruck---30.蓝牙耳机与有线耳机的切换
- Cubietruck---31.蓝牙耳机与有线耳机的声音输出
- IPhone4S与蓝牙耳机连接
- 笔记本电脑连接蓝牙耳机设备
- 树莓派3连接蓝牙耳机
- android判断耳机
- 蓝牙耳机
- 蓝牙耳机
- 蓝牙耳机
- 蓝牙耳机
- Android RecognizerIntent与蓝牙耳机
- Rest模式get,put,post,delete含义与区别(转)
- 启动exe 失败,
- swiper简单使用
- Hibernate的缓存
- 详解java设计模式(四)之迭代器模式(行为型)
- android 判断有线耳机、蓝牙耳机连接
- Windows程序设计Day 5
- opencv全景拼接源码解读与修改。
- LeetCode题解(Week 3):206. Reverse Linked List
- HTTP学习之路
- Java线程状态中BLOCKED和WAITING的区别
- 关于PHP获取私有属性(调用阿里云sdk,返回带私有属性的对象)
- 推荐!手把手教你使用Git
- java设计模式(3)观察者模式