平时我们对hdmi等设备插拔,只要硬件支持设备热插拔检测,android都有代码检测到,热插拔函数为:
frameworks/base/services/core/java/com/android/server/WiredAccessoryManager.java
在setDeviceStateLocked()函数中,有热插拔检测状态值,state=1,表示插入;为0表示拔掉。

接着调用AudioManager.java中函数,AudioManager只是一些实现类,具体的实现在AudioService.java中。

在AudioService.java中,onSetWiredDeviceConnectionState()调handleDeviceConnection()函数.

handleDeviceConnection()通过binder往下调用。具体实现如下:

具体实现有两个逻辑,总的来说,就是插入设备,把设备号加入数组,拔掉设备,在数组里删除设备号。
AudioSystem.setDeviceConnectionState()通过jni调用到AudioSystem.cpp,AudioSystem只是一些接口类,具体实现在AudioPolicyManager.cpp中的AudioPolicyManager::setDeviceConnectionStateInt()函数中,原理大致相同:


总结,插拔存取当前device设备的数组有两个,一个在java层,一个在nativce层,它们都用来保存当前连接可用,硬件支持的设备。热插拔就是把设备号分别在java层和native层在可用设备数组里进行添加和删除的过程。
网友评论