aidl通信客户端+服务端

	aidl实现两个app之前的进程通信。我们把两个通讯的app分为客户端和服务端,其中服务端app实现的功能是开机自启一个service服务,其中提供两个aidl接口,这个两个接口分别实现了一个开蓝牙操作和一个关蓝牙操作,具体的代码实现可以参考[开机自启服务+aidl](https://blog.csdn.net/u013441613/article/details/125177803?spm=1001.2014.3001.5501)
   将服务端生成的aidl接口的文件夹,也就是整个aidl文件夹拷贝出来发给客户端,我们的客户端demo实现的功能是绑定服务端,通过aidl通讯实现开启和关闭服务端的蓝牙操作,服务绑定的具体代码如下
	/**
	8.0一下系统绑定服务
	**/
     private void bindService3() {
        Intent intent = new Intent();
        intent.setAction("com.example.appservices.MyService");
        intent.setPackage("com.example.appservices");
        /*intent:用来指定启动哪一个service以及传递一些数据过去
          conn:实现客户端与服务端通信的一个关键类。
                要想实现它,就必须重写两个回调方法:onServiceConnected()以及onServiceDisconnected(),
                而我们可以通过这两个回调方法得到服务端里面的IBinder对象,从而达到通信的目的*/
      boolean result =   bindService(intent, mServiceConnection, BIND_AUTO_CREATE);
      Log.e(TAG,"result = "+result);

    }

    /**
     * Android 8。0系统以上绑定服务方法
     *
     */
    private void bindService2() {
        try {
            Intent intent = new Intent();
            intent.setComponent(new ComponentName(MYKEY_PACKAGE_NAME, CONNECT_CLASS_PATH));
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                startForegroundService(intent);

            } else {
                startService(intent);
            }

            boolean bindResult = bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
            Log.e(TAG,"bindResult = "+bindResult);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
   //可以通过ServiceConnection进行通信
    ServiceConnection mServiceConnection = new ServiceConnection() {
        //系统会调用该方法以传递服务的onBind()方法返回的IBinder
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.e(TAG,"IBinder = " );
            //数据类型转换
            myBinder = IMyAidlInterface.Stub.asInterface(service);
                 }

aidl通讯开关蓝牙的代码如下

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn:
                try {
                    Log.i(TAG, "开启蓝牙");
                    Log.e(TAG,"MyBinder" +myBinder);
                    if (myBinder != null){

                        myBinder.startBle(callBack);
                    }
                } catch (RemoteException e) {
                    e.printStackTrace();
                    Log.d(TAG, "e = " + e);
                }
                break;
            case R.id.btn_close:
                try {
                    Log.i(TAG, "关闭蓝牙");
                    myBinder.closeBle(callBack);
                } catch (RemoteException e) {
                    e.printStackTrace();
                    Log.d(TAG, "e = " + e);
                }
                break;
        }
    }

demo下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

甜美冰景

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值