1. java中加入打开bluetooth的操作
首先在AndroidManifest.xml中加入bluetooth的权限
-
<uses-permission android:name="android.permission.BLUETOOTH" />
-
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
然后在java中加入以下几句,先获取adapter,然后发一个广播。
-
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
-
if(!adapter.isEnabled())
-
{
-
//如果蓝牙设备不可用的话,创建一个intent对象,该对象用于启动一个Activity,提示用户启动蓝牙适配器
-
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
-
startActivity(intent);
-
}
-
2. framework层对于打开的处理
2.1 这个
getDefaultAdapter是获取的什么东东呢?
在./frameworks/base/core/java/android/bluetooth/BluetoothAdapter.java中
-
public static synchronized BluetoothAdapter getDefaultAdapter()
-
{
-
if (sAdapter == null) {
-
IBinder b = ServiceManager.getService(BLUETOOTH_MANAGER_SERVICE);
-
IBluetoothManager managerService = IBluetoothManager.Stub.asInterface(b);
-
sAdapter = new BluetoothAdapter(managerService);
-
}
-
}
-
return sAdapter;
-
}
先获取了BLUETOOTH_MANAGER_SERVICE服务,然后在构造函数中
将代理IBluetoothManager赋给了mManagerService。
-
BluetoothAdapter(IBluetoothManager managerService)
-
{
-
mService = managerService.registerAdapter(mManagerCallback);
-
mManagerService = managerService;
-
mServiceRecordHandler = null;
-
}
以后就用这个mManagerService来进行操作了。
2.1.1 这个
BLUETOOTH_MANAGER_SERVICE服务是什么呢?
在./frameworks/base/services/java/com/android/server/SystemServer.java中
-
bluetooth = new BluetoothManagerService(context);
-
ServiceManager.addService(BluetoothAdapter.BLUETOOTH_MANAGER_SERVICE, bluetooth);
就是
BluetoothManagerService这个东东。
2.2 enalbe过程的各个调用
./frameworks/base/core/java/android/bluetooth/BluetoothAdapter.java
-
public boolean enable() {
-
if (isEnabled() == true){
-
return true;
-
}
-
try {
-
return mManagerService.enable();
-
} catch (RemoteException e) {Log.e(TAG, "", e);}
-
return false;
-
}
其中mManagerService
.enable(),就是通过代理调用了BluetoothManagerService的enable
2.2.1 bt_adapter调用了bt_ms的enable
在frameworks/base/services/java/com/android/server/BluetoothManagerService.java中
-
public boolean enable() {
-
mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH ADMIN permission");
-
synchronized(mReceiver) {
-
mQuietEnableExternal = false;
-
mEnableExternal = true;
-
long callingIdentity = Binder.clearCallingIdentity();
-
persistBluetoothSetting(BLUETOOTH_ON_BLUETOOTH);
-
Binder.restoreCallingIdentity(callingIdentity);
-
sendEnableMsg(false);
-
}
-
return true;
-
}
BluetoothManagerService的enalbe过程就是调用了sendEnableMsg
,即发送一个MESSAGE_ENABLE
在msg的handler中
-
private void handleEnable(boolean quietMode) {
-
if (!mQuietEnable) {
-
if(!mBluetooth.enable())
-
}
-
else {
-
mBluetooth.enableNoAutoConnect();
-
}
-
}
BluetoothManagerService的enable就是调用了mBluetooth的enable.
2.3 这个mBluetooth是什么东东呢?
-
这个mBluetooth是 mBluetooth = IBluetooth.Stub.asInterface(service);
在./packages/apps/Bluetooth/src/com/android/bluetooth/btservice/AdapterService.java中
-
private static class AdapterServiceBinder extends IBluetooth.Stub
噢,原来这个BluetoothManagerService的enable又是通过代理调用到了Bluetooth.apk中的enalbe。
3. Bluetooth.apk中对于enable的操作
在./packages/apps/Bluetooth/src/com/android/bluetooth/btservice/AdapterService.java中
-
boolean enable() {
-
return enable (false);
-
}
-
public synchronized boolean enable(boolean quietMode) {
-
Message m = mAdapterStateMachine.obtainMessage(AdapterState.USER_TURN_ON);
-
mAdapterStateMachine.sendMessage(m);
-
return true;
-
}
就是发送一个msg: USER_TURN_ON
在./packages/apps/Bluetooth/src/com/android/bluetooth/btservice/AdapterState.java中
的processMessage在处理USER_TURN_ON
时要进行状态切换,会调用mAdapterService.processStart()
再次发送一个msg: STARTED,最终会调用 mAdapterService.enableNative();
3.1 Bluetooth.apk调用jni中的enableNative
在./packages/apps/Bluetooth/jni/com_android_bluetooth_btservice_AdapterService.cpp中
-
static jboolean enableNative(JNIEnv* env, jobject obj) {
-
int ret = sBluetoothInterface->enable();
-
result = (ret == BT_STATUS_SUCCESS) ? JNI_TRUE : JNI_FALSE;
-
return result;
-
}
3.1.1 这个sBluetoothInterface
又是怎么来的呢?
在./packages/apps/Bluetooth/jni/com_android_bluetooth_btservice_AdapterService.cpp中
-
static void classInitNative(JNIEnv* env, jclass clazz) {
-
const char *id = (strcmp(value, "1")? BT_STACK_MODULE_ID : BT_STACK_TEST_MODULE_ID);
-
hw_get_module(id, (hw_module_t const**)&module);
-
hw_device_t* abstraction;
-
module->methods->open(module, id, &abstraction);
-
bluetooth_module_t* btStack = (bluetooth_module_t *)abstraction;
-
sBluetoothInterface = btStack->get_bluetooth_interface();
-
}
这个BT_STACK_MODULE_ID 就是bluetooth.default.so,在InitNative函数中打开了bluetooth.default.so这个库。
然后sBluetoothInterface就代表了bluetooth.default.so的接口。
4. external中的bluetooth.default.so
在./external/bluetooth/bluedroid/btif/src/bluetooth.c中有HAL_MODULE_INFO_SYM 的定义,
Bluetooth.apk中的jni调用的enable也就是调用了bluetoothInterface中的enable
-
static int enable( void )
-
{
-
if (interface_ready() == FALSE)
-
return BT_STATUS_NOT_READY;
-
-
return btif_enable_bluetooth();
-
}
4.1
在./external/bluetooth/bluedroid/btif/src/btif_core.c中
-
bt_status_t btif_enable_bluetooth(void)
-
{
-
btif_core_state = BTIF_CORE_STATE_ENABLING;
-
bte_main_enable(btif_local_bd_addr.address);
-
return BT_STATUS_SUCCESS;
-
}
在./external/bluetooth/bluedroid/main/bte_main.c中
-
void bte_main_enable(uint8_t *local_addr)
-
{
-
BTE_Init();
-
if (bt_hc_if)
-
{
-
int result = bt_hc_if->init(&hc_callbacks, local_addr);
-
bt_hc_if->set_power(BT_HC_CHIP_PWR_ON);
-
bt_hc_if->preload(NULL);
-
}
-
}
其中 bt_hc_if的初始化是:
bt_hc_if = (bt_hc_interface_t *) bt_hc_get_interface();
同时这个get_interface是
./external/bluetooth/bluedroid/hci/src/bt_hci_bdroid.c
-
const bt_hc_interface_t *bt_hc_get_interface(void)
-
{
-
return &bluetoothHCLibInterface;
-
}
原来这个bt_hc_if 就是 bluetoothHCLibInterface,
4.2 所以bt_hc_if->set_power,就是调用bluetoothHCLibInterface中的set_power
-
static void set_power(bt_hc_chip_power_state_t state)
-
{
-
int pwr_state;
-
pwr_state = (state == BT_HC_CHIP_PWR_ON) ? BT_VND_PWR_ON : BT_VND_PWR_OFF;
-
if (bt_vnd_if)
-
bt_vnd_if->op(BT_VND_OP_POWER_CTRL, &pwr_state);
-
else
-
ALOGE("vendor lib is missing!");
-
}
这又是一个函数指针
4.3 又是一个函数指针
在./external/bluetooth/bluedroid/hci/src/bt_hw.c中
-
void init_vnd_if(unsigned char *local_bdaddr)
-
{
-
void *dlhandle;
-
dlhandle = dlopen("libbt-vendor.so", RTLD_NOW);
-
bt_vnd_if = (bt_vendor_interface_t *) dlsym(dlhandle, "BLUETOOTH_VENDOR_LIB_INTERFACE");
-
bt_vnd_if->init(&vnd_callbacks, local_bdaddr);
-
}
bt_vnd_if就是libbt-vendor.so中的接口,所以这个bt_vnd_if->op就是调用了libbt-vendor.so中的op
5. libbt-vendor.so
在./device/common/libbt/src/bt_vendor_brcm.c中
-
static int op(bt_vendor_opcode_t opcode, void *param)
-
{
-
switch(opcode)
-
{
-
case BT_VND_OP_POWER_CTRL: //0
-
{
-
int *state = (int *) param;
-
if (*state == BT_VND_PWR_OFF)
-
{
-
upio_set_bluetooth_power(UPIO_BT_POWER_OFF);
-
usleep(200000);
-
}
-
else if (*state == BT_VND_PWR_ON)
-
{
-
upio_set_bluetooth_power(UPIO_BT_POWER_ON);
-
usleep(500000);
-
}
-
}
-
break;
-
}
-
}
5.1 继续看
在./device/common/libbt/src/upio.c中
-
int upio_set_bluetooth_power(int on)
-
{
-
char buffer = '0';
-
switch(on)
-
{
-
case UPIO_BT_POWER_OFF:
-
buffer = '0';
-
break;
-
-
case UPIO_BT_POWER_ON:
-
buffer = '1';
-
break;
-
}
-
-
if (is_rfkill_disabled())
-
return 0;
-
-
if (rfkill_id == -1)
-
{
-
//下面这个init确认路径 /sys/class/rfkill/rfkill[n]/state 中的n
-
if (init_rfkill())
-
return ret;
-
}
-
//打开/sys/class/rfkill/rfkill0/state
-
fd = open(rfkill_state_path, O_WRONLY);
-
//将0或者1写到/sys/class/rfkill/rfkill0/state这个文件中
-
sz = write(fd, &buffer, 1);
-
return ret;
-
}
原来这最核心的就是向内核sys中写。
6.总结
这一系列的调用如下图所示:
阅读(4468) | 评论(0) | 转发(1) |