Android-蓝牙AVRCP功能实现【转】

 

AVRCP的按键定义:

 

\sdk\emulator\keymaps\AVRCP.kl

 

key 200   MEDIA_PLAY_PAUSE    WAKE

key 201   MEDIA_PLAY_PAUSE    WAKE

key 166   MEDIA_STOP          WAKE

key 163   MEDIA_NEXT          WAKE

key 165   MEDIA_PREVIOUS      WAKE

key 168   MEDIA_REWIND        WAKE

key 208   MEDIA_FAST_FORWARD  WAKE

 

BCM(broadcom)宏定义需要打开:

BOARD_HAVE_BLUETOOTH_BCM := true

 

BT音频控制的代码

external\bluetooth\bluez\audio\control.c

(1) 按键的MAP

static struct {

    const char *name;

    uint8_t avrcp;

    uint16_t uinput;

} key_map[] = {

    { "PLAY",     PLAY_OP,      KEY_PLAYCD },

    { "STOP",     STOP_OP,      KEY_STOPCD },

    { "PAUSE",    PAUSE_OP,     KEY_PAUSECD },

    { "FORWARD",      FORWARD_OP,       KEY_NEXTSONG },

    { "BACKWARD",     BACKWARD_OP,      KEY_PREVIOUSSONG },

    { "REWIND",       REWIND_OP,    KEY_REWIND },

    { "FAST FORWARD", FAST_FORWARD_OP,  KEY_FASTFORWARD },

    { NULL }

};

(2) 按键处理

static void handle_panel_passthrough(struct control *control,

{

    …

    for (i = 0; key_map[i].name != NULL; i++) {

       uint8_t key_quirks;

 

       if ((operands[0] & 0x7F) != key_map[i].avrcp)

           continue;

 

       DBG("AVRCP: %s %s", key_map[i].name, status);

 

       key_quirks = control->key_quirks[key_map[i].avrcp];

 

       if (key_quirks & QUIRK_NO_RELEASE) {

           if (!pressed) {

              DBG("AVRCP: Ignoring release");

              break;

           }

 

           DBG("AVRCP: treating key press as press + release");

           send_key(control->uinput, key_map[i].uinput, 1);

           send_key(control->uinput, key_map[i].uinput, 0);

           break;

       }

 

       send_key(control->uinput, key_map[i].uinput, pressed);

       break;

    }

}

HCIDUMP数据分析

以Sony耳机DRC-BT15为例

#adb shell

#hcidump –X

 

左键:

> ACL data: handle 12 flags 0x02 dlen 12

    L2CAP(d): cid 0x0042 len 8 [psm 0]

      0000: 30 11 0e 00 48 7c4c 00                           0...H|L.

< HCI Command: Exit Sniff Mode (0x02|0x0004) plen 2

  0000: 0c 00                                             ..

< ACL data: handle 12 flags 0x00 dlen 12

    0000: 08 00 4b 00 32 11 0e 09  48 7c4c 00              ..K.2...H|L.

> HCI Event: Max Slots Change (0x1b) plen 3

  0000: 0c 00 05                                          ...

> HCI Event: Mode Change (0x14) plen 6

  0000: 00 0c 00 00 00 00                                 ......

> HCI Event: Command Status (0x0f) plen 4

  0000: 0c 01 04 08                                       ....

> ACL data: handle 12 flags 0x02 dlen 12

    L2CAP(d): cid 0x0042 len 8 [psm 0]

      0000: 40 11 0e 00 48 7c cc 00                           @...H|?

< ACL data: handle 12 flags 0x00 dlen 12

    0000: 08 00 4b 00 42 11 0e 09  48 7c cc 00              ..K.B...H|?

> HCI Event: Number of Completed Packets (0x13) plen 5

  0000: 01 0c 00 02 00                                    .....

 

播放/暂停:

> ACL data: handle 12 flags 0x02 dlen 12

    L2CAP(d): cid 0x0042 len 8 [psm 0]

      0000: 50 11 0e 00 48 7c 4b 00                           P...H|K.

< ACL data: handle 12 flags 0x00 dlen 12

    0000: 08 00 4b 00 52 11 0e 09  48 7c 4b 00              ..K.R...H|K.

> ACL data: handle 12 flags 0x02 dlen 12

    L2CAP(d): cid 0x0042 len 8 [psm 0]

      0000: 60 11 0e 00 48 7c cb 00                           `...H|?

< ACL data: handle 12 flags 0x00 dlen 12

    0000: 08 00 4b 00 62 11 0e 09  48 7c cb 00              ..K.b...H|?

> HCI Event: Number of Completed Packets (0x13) plen 5

  0000: 01 0c 00 02 00                                    .....

 

右键:

> ACL data: handle 12 flags 0x02 dlen 12

    L2CAP(d): cid 0x0042 len 8 [psm 0]

      0000: 70 11 0e 00 48 7c 46 00                           p...H|F.

< ACL data: handle 12 flags 0x00 dlen 12

    0000: 08 00 4b 00 72 11 0e 09  48 7c 46 00              ..K.r...H|F.

> ACL data: handle 12 flags 0x02 dlen 12

    L2CAP(d): cid 0x0042 len 8 [psm 0]

      0000: 80 11 0e 00 48 7c c6 00                           ....H|?

< ACL data: handle 12 flags 0x00 dlen 12

    0000: 08 00 4b 00 82 11 0e 09  48 7c c6 00              ..K.....H|?

> HCI Event: Number of Completed Packets (0x13) plen 5

  0000: 01 0c 00 02 00                                    .....

> HCI Event: Max Slots Change (0x1b) plen 3

  0000: 0c 00 01                                          ...

> HCI Event: Mode Change (0x14) plen 6

  0000: 00 0c 00 02 c8 00                                 ....?

 

然后将control.c的日志打印出来:

按一次”“播放/暂停键”:

 

D/ACRVP   (  237): --- handle_panel_passthrough ----

D/ACRVP   (  237): operands[0] = 46   对应PAUSE_OP

D/ACRVP   (  237): key_quirks = 0, pressed = 1按键按下

D/ACRVP   (  237): control->uinput = fffffffe,send_key = 201对应MEDIA_PLAY_PAUSE

D/ACRVP   (  237): --- handle_panel_passthrough ----

D/ACRVP   (  237): operands[0] = c6  (= 0x46 | 0x80 表示按键释放了)

D/ACRVP   (  237): key_quirks = 0, pressed = 0按键释放

D/ACRVP   (  237): control->uinput = fffffffe,send_key = 201 对应MEDIA_PLAY_PAUSE

 

再按一次”“播放/暂停键”:

 

D/ACRVP   (  237): --- handle_panel_passthrough ----

D/ACRVP   (  237): operands[0] = 44   对应PLAY_OP

D/ACRVP   (  237): key_quirks = 0, pressed = 1 按键按下

D/ACRVP   (  237): control->uinput = fffffffe,send_key = 200  对应MEDIA_PLAY_PAUSE

D/ACRVP   (  237): --- handle_panel_passthrough ----

D/ACRVP   (  237): operands[0] = c4

D/ACRVP   (  237): key_quirks = 0, pressed = 0  按键释放

D/ACRVP   (  237): control->uinput = fffffffe,send_key = 200 对应MEDIA_PLAY_PAUSE

 

next key:

D/ACRVP   (  237): --- handle_panel_passthrough ----

D/ACRVP   (  237): operands[0] = 4b对应FORWARD_OP

D/ACRVP   (  237): key_quirks = 0, pressed = 1

D/ACRVP   (  237): control->uinput = fffffffe,send_key = 163 对应MEDIA_NEXT

D/ACRVP   (  237): --- handle_panel_passthrough ----

D/ACRVP   (  237): operands[0] = cb

D/ACRVP   (  237): key_quirks = 0, pressed = 0

D/ACRVP   (  237): control->uinput = fffffffe,send_key = 163

 

prev key:

D/ACRVP   (  237): --- handle_panel_passthrough ----

D/ACRVP   (  237): operands[0] = 4c 对应BACKWARD_OP

D/ACRVP   (  237): key_quirks = 0, pressed = 1

D/ACRVP   (  237): control->uinput = fffffffe,send_key = 165 对应

D/ACRVP   (  237): --- handle_panel_passthrough ----

D/ACRVP   (  237): operands[0] = cc

D/ACRVP   (  237): key_quirks = 0, pressed = 0

D/ACRVP   (  237): control->uinput = fffffffe,send_key = 165 MEDIA_PREVIOUS

从上面可以看到bluetooth的协议栈blueZ是没有问题的

 

将 frameworks\base\libs\ui\EventHub.cpp的LOG打开,只能看到了control.c的日志,EventHub的getEvent完全不响应

观察所有log日志发现,openDevice里也没有装载AVRCP.kl

 

初步判断event有问题

 

event分析:

$ adb shell

# cd /proc/bus/input

# cat devices

cat devices

# cat devices

cat devices

I: Bus=0019 Vendor=0001 Product=0001 Version=0001   参考s3c-keypad.c

N: Name="s3c-keypad"        input_dev->name = DEVICE_NAME;

P: Phys=s3c-keypad/input0       input_dev->phys = "s3c-keypad/input0";

S: Sysfs=/devices/virtual/input/input0   virtual的?

U: Uniq=

H: Handlers=event0

B: EV=3

B: KEY=4000400 0

 

I: Bus=0019 Vendor=0001 Product=0001 Version=0100参考vpad_buttons.c

N: Name="s3c-eintkey"          input->name = pdev->name,  gpio_keys_device_driver.name    = "s3c-eintkey",

P: Phys=gpio-keys/input0         input->phys = "gpio-keys/input0"

S: Sysfs=/devices/platform/s3c-eintkey/input/input1   为什么这里是platform目录?

U: Uniq=

H: Handlers=event1

B: EV=3

B: KEY=100000 0 0 0

 

I: Bus=0018 Vendor=0000 Product=0000 Version=0000 参考amri_ts.c

N: Name="amri_ts"        amri_ts_driver.name = "amri_ts"

P: Phys=            没有定义

S: Sysfs=/devices/platform/s3c2440-i2c.0/i2c-0/0-0033/input/input2   为什么这里是platform目录?

U: Uniq=

H: Handlers=event2

B: EV=b

B: KEY=400 0 0 0 0 0 40000800 40 0 0 10000

B: ABS=2650000 0

 

I: Bus=0000 Vendor=0000 Product=0000 Version=0000

N: Name="ecompass_data"

P: Phys=

S: Sysfs=/devices/virtual/input/input3

U: Uniq=

H: Handlers=event3

B: EV=9

B: ABS=307bf

 

从上面可以看到,完全没有AVRCP的event。

 

解决办法:

 

Kernel

$ make menuconfig

 

 

 

 

CONFIG_INPUT_UINPUT

 

解决后的状况:

 

$ adb shell

# cd /proc/bus/input

# cat devices

显示增加了一个event

 

I: Bus=0005 Vendor=0000 Product=0000 Version=0000

N: Name="AVRCP"

P: Phys=

S: Sysfs=/devices/virtual/input/input4

U: Uniq=

H: Handlers=event4

B: EV=100007

B: KEY=10300 168 0 0 0 0 0

B: REL=0

 

看openDevice的信息:

D/EventHub(   84): EventHub::readNotify nfd: 87

D/EventHub(   84): Opening device: /dev/input/event4

D/EventHub(   84): Getting keys...

D/EventHub(   84): Getting absolute controllers...

D/EventHub(   84): keylayoutFilename = /system/usr/keylayout/AVRCP.kl

I/EventHub(   84): New keyboard: device->id=0x10003 devname='AVRCP' propName='hw.keyboards.65539.devname' keylayout='/system/usr/keylayout/AVRCP.kl'

I/EventHub(   84): New device: path=/dev/input/event4 name=AVRCP id=0x10003 (of 0x4) index=4 fd=196 classes=0x1

D/EventHub(   84): Adding device /dev/input/event4 0x361800 at 4, id = 3, classes = 0x1

D/EventHub(   84): Reporting device opened: id=0x10003, name=/dev/input/event4

 

按键时也可以看到EventHub的信息了:

Log如下:

 

D/ACRVP   (  236): --- handle_panel_passthrough ----

D/ACRVP   (  236): operands[0] = 46

D/ACRVP   (  236): key_quirks = 0, pressed = 1

D/ACRVP   (  236): control->uinput = 14,send_key = 201     control.c发出201号键,按键按下了

D/EventHub(   84): /dev/input/event4 got: t0=937, t1=582930, type=1, code=201, v=1   表示从event4得到201号键 MEDIA_PLAY_PAUSE

D/EventHub(   84): iev.code=201 keyCode=85 flags=0x00000001 err=0

D/EventHub(   84): /dev/input/event4 got: t0=937, t1=600241, type=0, code=0, v=0

D/AudioHardware(   61): AudioStreamOutALSA::setParameters() routing=0

D/ACRVP   (  236): --- handle_panel_passthrough ----

D/ACRVP   (  236): operands[0] = c6

D/ACRVP   (  236): key_quirks = 0, pressed = 0

D/ACRVP   (  236): control->uinput = 14,send_key = c9

D/EventHub(   84): /dev/input/event4 got: t0=937, t1=664391, type=1, code=201, v=0 control.c发出201号键,按键释放了

D/EventHub(   84): iev.code=201 keyCode=85 flags=0x00000001 err=0

D/EventHub(   84): /dev/input/event4 got: t0=937, t1=664406, type=0, code=0, v=0

D/A2DP    (   61): a2dp_stop

D/A2DP    (   61): bluetooth_stop

E/BluetoothEventLoop.cpp(   84): event_filter: Received signal org.bluez.AudioSink:Stopped from /org/bluez/236/hci0/dev_00_1D_BA_A5_D8_1C

E/BluetoothEventLoop.cpp(   84): event_filter: Received signal org.bluez.AudioSink:PropertyChanged from /org/bluez/236/hci0/dev_00_1D_BA_A5_D8_1C

E/BluetoothEventLoop.cpp(   84): event_filter: Received signal org.bluez.AudioSink:PropertyChanged from /org/bluez/236/hci0/dev_00_1D_BA_A5_D8_1C

V/BluetoothEventRedirector(  244): Received android.bluetooth.a2dp.action.SINK_STATE_CHANGED

D/CachedBluetoothDevice(  244): onProfileStateChanged: profile A2DP newProfileState 2

D/BluetoothA2dpService(   84): A2DP state : device: 00:1D:BA:A5:D8:1C State:4->2

D/A2DP    (   61): Received BT_RESPONSE - BT_STOP_STREAM

D/dalvikvm(  285): GC_EXPLICIT freed 87K, 56% free 2640K/5895K, external 1625K/2137K, paused 75ms

 

转载于:https://www.cnblogs.com/chenyready/p/7986712.html

你可能感兴趣的:(Android-蓝牙AVRCP功能实现【转】)