有个关于蓝牙耳机的按键怎么映射到系统里面的问题,刚刚看了一下。
1)如果是HFP按键,
则是耳机是通过AT命令发送到手机侧的。
代码位于BluetoothHandsfree.java
Hang-up 命令:
// Hang-up command
parser.register("+CHUP", new AtCommandHandler() { @Override public AtCommandResult handleActionCommand() { sendURC("OK"); if (isVirtualCallInProgress()) { terminateScoUsingVirtualVoiceCall(); } else { if (mCM.hasActiveFgCall()) { PhoneUtils.hangupActiveCall(mCM.getActiveFgCall()); } else if (mCM.hasActiveRingingCall()) { PhoneUtils.hangupRingingCall(mCM.getFirstActiveRingingCall()); } else if (mCM.hasActiveBgCall()) { PhoneUtils.hangupHoldingCall(mCM.getFirstActiveBgCall()); } } return new AtCommandResult(AtCommandResult.UNSOLICITED); } });
volumn - up声音大小
// Speaker Gain parser.register("+VGS", new AtCommandHandler() { @Override public AtCommandResult handleSetCommand(Object[] args) { // AT+VGS=in range [0,15] if (args.length != 1 || !(args[0] instanceof Integer)) { return new AtCommandResult(AtCommandResult.ERROR); } mScoGain = (Integer) args[0]; int flag = mAudioManager.isBluetoothScoOn() ? AudioManager.FLAG_SHOW_UI:0; mAudioManager.setStreamVolume(AudioManager.STREAM_BLUETOOTH_SCO, mScoGain, flag); return new AtCommandResult(AtCommandResult.OK); } });
2)如果是AVRCP按键:
则在,external\bluetooth\bluez\audio\control.c
1./* operands in passthrough commands */2.#define VOL_UP_OP 0x413.#define VOL_DOWN_OP 0x424.#define MUTE_OP 0x435.#define PLAY_OP 0x446.#define STOP_OP 0x457.#define PAUSE_OP 0x468.#define RECORD_OP 0x479.#define REWIND_OP 0x4810.#define FAST_FORWARD_OP 0x4911.#define EJECT_OP 0x4a12.#define FORWARD_OP 0x4b13.#define BACKWARD_OP 0x4c
1.staticstruct{2. constchar*name;3. uint8_t avrcp;4. uint16_t uinput;5.} key_map[]={6. { "PLAY", PLAY_OP, KEY_PLAYCD },7. { "STOP", STOP_OP, KEY_STOPCD },8. { "PAUSE", PAUSE_OP, KEY_PAUSECD },9. { "FORWARD", FORWARD_OP, KEY_NEXTSONG },10. { "BACKWARD", BACKWARD_OP, KEY_PREVIOUSSONG },11. { "REWIND", REWIND_OP, KEY_REWIND },12. { "FAST FORWARD", FAST_FORWARD_OP, KEY_FASTFORWARD },13. { NULL}14.};
1. for(i=0; key_map[i].name!= NULL; i++){2. uint8_t key_quirks;3. 4. if((operands[0]&0x7F)!= key_map[i].avrcp)5. continue;6. 7. DBG("AVRCP: %s %s", key_map[i].name, status);8. 9. key_quirks= control->key_quirks[key_map[i].avrcp];10. 11. if(key_quirks& QUIRK_NO_RELEASE){12. if(!pressed){13. DBG("AVRCP: Ignoring release");14. break;15. }16. 17. DBG("AVRCP: treating key press as press + release");18. send_key(control->uinput, key_map[i].uinput,1);19. send_key(control->uinput, key_map[i].uinput,0);20. break;21. }22. 23. send_key(control->uinput, key_map[i].uinput, pressed);24. break;25. }
1.staticvoid send_key(int fd, uint16_t key,int pressed)2.{3. if(fd<0)4. return;5. 6. send_event(fd, EV_KEY, key, pressed);7. send_event(fd, EV_SYN, SYN_REPORT,0);8.}