BluetoothHeadSetClient的framework修改

1.概述:
BluetoothHeadSetClient是Android系统中对于HFP协议HF端的定义,HFP协议AG端则是BluetoothHeadSet相关的定义。
通常情况下,手机对应的是AG端;而类似于车机、蓝牙耳机等设备,对应的是HF端。Android原生的SDK是针对手机开发的,所以会发现在原生API中找不到BluetoothHeadSetClient相关的定义,这个时候我们就要去源码一探究竟了。

2.修改源码
可以看到源码中对BluetoothHeadSetClient是做了隐藏的:
(android/frameworks/base/core/java/android/bluetooth/BluetoothHeadsetClient.java)

/**
 * Public API to control Hands Free Profile (HFP role only).
 * <p>
 * This class defines methods that shall be used by application to manage profile
 * connection, calls states and calls actions.
 * <p>
 *
 * @hide
 */
public final class BluetoothHeadsetClient implements BluetoothProfile {

那么我们如果是要做蓝牙电话的开发,肯定是需要把这个类释放出来。于是在源码中去掉BluetoothHeadsetClient类上@hide的标识符,同时类里面几个标识了 @UnsupportedAppUsage的方法,也需要去掉这个 @UnsupportedAppUsage标识,不然编译会出错。

在BluetoothHeadsetClient类中,还引用了BluetoothHeadsetClientCall这个类,他也是@hide标识的,也需要去掉。

3.编译源码
准备工作完毕之后,可以在android根目录下执行 make framework ,如下:
在这里插入图片描述
这会重新编译整个framwork,生成的新的framework的jar包位于android/out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/class.jar
我这里把class.jar名字改成framework.jar,便于标识。

4.引入framework.jar到项目中
接着把framework.jar放进module的libs文件下:
在这里插入图片描述

在module下的build.gradle文件中引入jar包,但需要用compileOnly,这样就不会把整个framework.jar打包到apk中,而只是在编译的时候用到其中类的。当真正运行的时候,还是会使用到android系统中system/framwork/framework.jar里面的内容。
在这里插入图片描述
5.调整SDK引用的顺序
把jar包引入之后,还需要调整我们引用SDK的顺序,调整为优先使用framework.jar里面的内容。
需要注意的是,我的Android Studio 版本是4.2.1,app.iml的位置有了变化,使用网上老的方法会不起作用,具体的配置如下:
首先在module的build.gradle文件最后加入如下的配置:

preBuild {
    doLast {
        def imlFile = file("..\\.idea\\modules\\app\\" + project.parent.name + ".app.iml")
        println 'Change ' + project.name + '.iml order'
        try {
            def parsedXml = (new XmlParser()).parse(imlFile)
            def jdkNode = parsedXml.component[1].orderEntry.find { it.'@type' == 'jdk' }
            parsedXml.component[1].remove(jdkNode)
            def sdkString = "Android API " + android.compileSdkVersion.substring("android-".length()) + " Platform"
            println 'what' + sdkString
            new Node(parsedXml.component[1], 'orderEntry', ['type': 'jdk', 'jdkName': sdkString, 'jdkType': 'Android SDK'])
            groovy.xml.XmlUtil.serialize(parsedXml, new FileOutputStream(imlFile))
        } catch (FileNotFoundException e) {
            // nop, iml not found
            println "no iml found"
        }
    }
}

然后,在整个Project的build.gradle文件加入如下配置:

allprojects {
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
    }
    gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {
            Set<File> fileSet = options.bootstrapClasspath.getFiles()
            List<File> newFileList =  new ArrayList<>();
            //相对位置,根据存放的位置修改路径
            newFileList.add(new File("./app/libs/framework.jar"))
            newFileList.addAll(fileSet)
            options.bootstrapClasspath = files(
                    newFileList.toArray()
            )
        }
    }
}

这样重新sync之后,我们就可以使用到BluetoothHeadSetClient这个类了:

 public void init(Context context){
        this.context = context;
        BluetoothAdapter.getDefaultAdapter().getProfileProxy(context,serviceListener, BluetoothProfile.HEADSET_CLIENT);
    }

 BluetoothProfile.ServiceListener serviceListener = new BluetoothProfile.ServiceListener() {
        @Override
        public void onServiceConnected(int profile, BluetoothProfile proxy) {

            bluetoothHeadset = (BluetoothHeadset) proxy;

        }

        @Override
        public void onServiceDisconnected(int profile) {

        }
    };
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值