MediaButtonReceive只是作为一种通俗的命名方式来响应插入耳机后,点击耳机上的按键(MEDIA_BUTTON)接受该广播事件类。
点击MEDIA_BUTTON发送的Intent Action 为 ACTION_MEDIA_BUTTON="android.intent.action.MEDIA_BUTTON”
Intent 附加值为(Extra)点击MEDIA_BUTTON的按键码:
//获得KeyEvent对象
KeyEvent keyEvent = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
//获得Action
String intentAction = intent.getAction() ;
直接上代码
public class BlueToothIntercom {public static final int START_SPEAK = 0x1;public static final int STOP_SPEAK = 0x2;/*** Local Bluetooth adapter*/ private BluetoothAdapter mBluetoothAdapter = null;private final String TAG = "BlueToothIntercom";private AudioManager mAudioManager = null;private Handler callbackHandler;private Context context;private static Object lockObject = new Object();public int scoState = AudioManager.SCO_AUDIO_STATE_DISCONNECTED; //Sco状态 public boolean isPTTDown; //PTT键是否是按下状态 private BroadcastReceiver blueToothScoBroadcast;private static BlueToothIntercom thisObject;private BlueToothIntercom() {mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();}public static BlueToothIntercom getInstance() {if (null == thisObject) {synchronized (lockObject) {if (null == thisObject) {thisObject = new BlueToothIntercom();}}}return thisObject;}public void start(Context context, Handler handler) {this.callbackHandler = handler;this.context = context;if (null == mAudioManager) {mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);}if (null == blueToothScoBroadcast) {blueToothScoBroadcast = new BlueToothBroadcast();IntentFilter filter = new IntentFilter(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);filter.addCategory(VENDOR_CATEGORY);filter.addAction(BluetoothHeadset.ACTION_VENDOR_SPECIFIC_HEADSET_EVENT);//另一款设备的监听(手咪)context.registerReceiver(blueToothScoBroadcast, filter);}open(context);}public void stop() {if (null != blueToothScoBroadcast) {context.unregisterReceiver(blueToothScoBroadcast);blueToothScoBroadcast = null;}close(context);}public boolean isPTTDown() {return isPTTDown;}public void startSco(int delay) {if (!mAudioManager.isBluetoothScoAvailableOffCall()) {Toast.makeText(context.getApplicationContext(), R.string.bluetooth_not_have, Toast.LENGTH_SHORT).show();LogUtil.i(TAG, "系统不支持蓝牙录音");return;}Handler handler = new Handler(context.getMainLooper());handler.postDelayed(new Runnable(){@Override public void run() {if (isPTTDown && !mAudioManager.isBluetoothScoOn() && AudioManager.SCO_AUDIO_STATE_CONNECTING != scoState) {//mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);scoState = AudioManager.SCO_AUDIO_STATE_CONNECTING;mAudioManager.startBluetoothSco();//蓝牙录音的关键,启动SCO连接,耳机话筒才起作用mAudioManager.setBluetoothScoOn(true);} else if (mAudioManager.isBluetoothScoOn()) {//mAudioManager.setMode(AudioManager.MODE_NORMAL);mAudioManager.setBluetoothScoOn(false);mAudioManager.stopBluetoothSco();}}}, delay);}public void stopSco() {// mAudioManager.setMode(AudioManager.MODE_NORMAL); mAudioManager.stopBluetoothSco();}public void startSpeak() {isPTTDown = true;callbackHandler.sendEmptyMessage(START_SPEAK);}public void stopSpeak() {isPTTDown = false;callbackHandler.sendEmptyMessage(STOP_SPEAK);}//注册耳机监听public void open(Context context){if(Device.getInstance().getBulueSelect()!=0){AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);ComponentName name = new ComponentName(context.getPackageName(),BlueToothBroadcast.class.getName());//Intent intent=new Intent(context, BlueToothBroadcast.class); audioManager.registerMediaButtonEventReceiver(name);}}/*** 关闭耳机线控监听* @param context*/ public void close(Context context){if(Device.getInstance().getBulueSelect()!=0){AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);ComponentName name = new ComponentName(context.getPackageName(),BlueToothBroadcast.class.getName());audioManager.unregisterMediaButtonEventReceiver(name);}}}
public class BlueToothBroadcast extends BroadcastReceiver {Timer timer = null;private static boolean isTimerStart = false;private static MyTimer myTimer = null;BlueToothBroadcast blueToothBroadcast;private OnBlueSetListener nnn = null;public BlueToothBroadcast() {timer = new Timer(true);}public void setOnHeadSetListener(OnBlueSetListener headSetListener){this.nnn = headSetListener;}public interface OnBlueSetListener{public void gogo();}@Override public void onReceive(Context context, Intent intent) {// TODO: This method is called when the BroadcastReceiver is receiving // an Intent broadcast. if (AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED.equals(intent.getAction())) {int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -1);BlueToothIntercom.getInstance().scoState = state;if(state==0&&BlueToothIntercom.getInstance().isPTTDown()){BlueToothIntercom.getInstance().stopSpeak();}}if (BluetoothHeadset.ACTION_VENDOR_SPECIFIC_HEADSET_EVENT.equals(intent.getAction())) {if(Device.getInstance().isbulueHands()){Bundle eventExtra = intent.getExtras();// get the arguments that the headset passed outObject[] args = (Object[]) eventExtra.get(BluetoothHeadset.EXTRA_VENDOR_SPECIFIC_HEADSET_EVENT_ARGS);String eventName = (String) args[0];if (eventName.equals(TALK_STRING)) {Integer eventValue = (Integer) args[1];if (eventValue == 1) {BlueToothIntercom.getInstance().startSpeak();} else if (eventValue == 0) {BlueToothIntercom.getInstance().stopSpeak();}}}}if(Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())){if(Device.getInstance().getBulueSelect()!=0){//获得KeyEvent对象KeyEvent keyEvent = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);try {if(keyEvent.getAction() == KeyEvent.ACTION_UP){if(isTimerStart){myTimer.cancel();isTimerStart = false;}else{myTimer = new MyTimer();timer.schedule(myTimer,1000);isTimerStart = true;}}} catch (Exception e) {}//终止广播(不让别的程序收到此广播,免受干扰)abortBroadcast();}}}/** 定时器,用于延迟1秒,内若无操作则为单击*/ class MyTimer extends TimerTask {@Override public void run() {try {myHandle.sendEmptyMessage(0);} catch (Exception e) {// TODO: handle exception }}};/** 此handle的目的主要是为了将接口在主线程中触发* ,为了安全起见把接口放到主线程触发*/ Handler myHandle = new Handler(){@Override public void handleMessage(Message msg) {super.handleMessage(msg);BlueToothIntercom.getInstance().startSpeak();isTimerStart = false;}};}注意我遇到的问题来了: 当按下耳机后发送 MEDIA_BUTTON ,开始获取
mAudioManager.startBluetoothSco();//蓝牙录音的关键,启动SCO连接,耳机话筒才起作用开始录音,当在点击一下准备结束录音的时候却监听不到 MEDIA_BUTTON 按键了,监听到的是
AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED ,因为当启动
scoState = AudioManager.SCO_AUDIO_STATE_CONNECTING的值改变了所以会监听
ACTION_SCO_AUDIO_STATE_UPDATED顺便加个别人的链接 /qinjuning/article/details/6938436