Android关于service call 直接调用方法分析

1 背景

希望通过命令的方式控制蓝牙打开与关闭,通过网上搜索我们都能搜到

//打开蓝牙
adb shell service call bluetooth_manager 6
//关闭蓝牙
adb shell service call bluetooth_manager 8
  • 其中运用到的就是service call,其机制是通过 Android 的 Binder 机制来实现的。
  • Android 系统中的各种服务和应用都可以通过 Binder 机制提供自己的接口,其他的应用或者系统组件 可以通过 Binder 来调用这些接口,实现跨进程通信和调用远程服务的功能。
  • 当使用 “service call” 命令时,实际上是通过 Binder 机制找到对应的系统服务,并调用这个服务的方法。命令的参数会被传递给对应的方法,从而实现特定的功能。

2 分析

2.1 查阅代码可知,打开蓝牙使用

./frameworks/base/services/core/java/com/android/server/BluetoothManagerService.java

在这里插入图片描述
因此铁定是server call 调用了enable因此打开了蓝牙功能

2.2 service list 查找 bluetooth 服务对应的 aidl

firefly_3399:/ # service list | grep "bluetooth"
11      bluetooth_manager: [android.bluetooth.IBluetoothManager]

2.3 代码中找到IBluetoothManager.aidl

fireflyserver:~/android/rk3399/industry-7.1$ find ./frameworks/base/  -name "IBluetoothManager.aidl"
./frameworks/base/core/java/android/bluetooth/IBluetoothManager.aidl

2.4 查阅IBluetoothManager.aidl

interface IBluetoothManager
 {
 ....
    IBluetooth registerAdapter(in IBluetoothManagerCallback callback);
     void unregisterAdapter(in IBluetoothManagerCallback callback);
     void registerStateChangeCallback(in IBluetoothStateChangeCallback callback);
     void unregisterStateChangeCallback(in IBluetoothStateChangeCallback callback);
     boolean isEnabled(); 
     boolean enable(String packageName); // 6
	 boolean enableNoAutoConnect(String packageName);
     boolean disable(String packageName, boolean persist); // 8
     int getState();
    IBluetoothGatt getBluetoothGatt();
....

很明显这个编号就是6是enable,8是disable,符合预期

3 延伸service用法

Usage: service [-h|-?]
       service list
       service check SERVICE
       service call SERVICE CODE [i32 N | i64 N | f N | d N | s16 STR ] ...
Options:
// i32 N: 表示将一个32位整数N写入发送包中。您可以使用这个选项来向系统服务的方法传递一个32位整数类型的参数。
   i32: Write the 32-bit integer N into the send parcel.
//i64 N: 表示将一个64位整数N写入发送包中。类似地,这个选项用于向系统服务的方法传递一个64位整数类型的参数。
   i64: Write the 64-bit integer N into the send parcel.
//f N: 表示将一个32位单精度浮点数N写入发送包中。您可以使用这个选项来向系统服务的方法传递一个单精度浮点数类型的参数。
   f:   Write the 32-bit single-precision number N into the send parcel.
//d N: 表示将一个64位双精度浮点数N写入发送包中。类似地,这个选项用于向系统服务的方法传递一个双精度浮点数类型的参数。
   d:   Write the 64-bit double-precision number N into the send parcel.
//s16 STR: 表示将一个UTF-16字符串STR写入发送包中。您可以使用这个选项来向系统服务的方法传递一个UTF-16格式的字符串参数。
   s16: Write the UTF-16 string STR into the send parcel.

service call 可以增加输入参数,但是无法获取返回值,例如这样运行,packageName就是firefly

service call bluetooth_manager 6 s16 firefly
05-27 10:56:10.949   479   884 D BluetoothManagerService: enable(firefly):  mBluetooth =android.bluetooth.IBluetooth$Stub$
Proxy@3862774 mBinding = false mState = ON
  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值