本文介绍: 提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录接上一篇HAL C++创建服务,这一篇使用AIDL对其进行通信一、获取jar包,并在项目加载这个jar包二、Client方法的实现2.读入数据总结接上一篇HAL C++创建服务,这一篇使用AIDL对其进行通信在上一篇中,创建了一个服务,我们需要使用这个服务,这一篇,就讲解如何使用这个服务。一、获取jar包,并在项目加载这个jar包从/out/soong/.intermediates/vendor/mediatek/p.
AIDL的创建与使用
server+client
一、server,aidl的服务端
<application
android:name=".ApiApplication"
android:allowBackup="true"
android:directBootAware="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:persistent="false"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true">
<service
android:name=".APIService"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.xxxxapi.intent.ACTION_API_SERVICE" />
</intent-filter>
</service>
</application>
<action android:name="com.xxxxapi.intent.ACTION_API_SERVICE" />这个很重要,在客户端绑定服务端时,需要用到这个intent filter
package com.emdoor.xxxxapi;
import com.emdoor.xxxxapi.APIListener;
interface xxxxAPI {
int getDisplayInputPortState(int port); //服务端的API方法
//sendUartData,接引另一篇文章<<Android C++服务创建和HIDL的生成>>,
//client通过aidl连接server,server再通过这个方法发送data到hal层的TestOfHidl服务
//TestOfHidl服务server实现的onDataReceived方法,通过APIListener中的onUartDataReceived(in byte[] data)返回数据给app
//具体可见稍后的代码
void sendUartData(in byte[] data);
void setAPIListener(in APIListener listener); //app监听
}
再创建监听文件
// APIListener.aidl
package com.emdoor.xxxxapi;
interface APIListener {
void onUartDataReceived(in byte[] data);
}
创建服务
public class APIService extends Service {
private static final String TAG = "APIService";
private xxxxAPI mxxxxAPI;
@Override
public void onCreate() {
super.onCreate();
Log.e(TAG, "APIService onCreate !!!");
IBinder binder = getService("xxxxapi");
if (binder != null) {
mxxxxAPI = xxxxAPI.Stub.asInterface(binder);
} else {
Log.e(TAG, "API service not available");
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.e(TAG, "APIService onBind !!!");
return ApiImpl.getInstance();
}
private IBinder getService(String serviceName) {
try {
Class<?> serviceManager = Class.forName("android.os.ServiceManager");
Method getService = serviceManager.getMethod("getService", String.class);
return (IBinder) getService.invoke(serviceManager.newInstance(), serviceName);
} catch (ClassNotFoundException | NoSuchMethodException |
IllegalArgumentException | IllegalAccessException |
InvocationTargetException | InstantiationException e) {
e.printStackTrace();
}
return null;
}
}
创建实现类,有关ITestOfHidl的作用,可以查看“Android C++服务创建和HIDL的生成”这一章内容
public class ApiImpl extends xxxxAPI.Stub {
private static final String TAG = "ApiImpl";
private Context mContext;
private static ApiImpl instance;
private APIListener listener;
private ITestOfHidl mTestOfHidl;
public static ApiImpl getInstance() {
if (instance == null) {
instance = new ApiImpl();
}
return instance;
}
private ApiImpl() {
try {
//获取HIDL的服务
mTestOfHidl = ITestOfHidl.getService();
if (mTestOfHidl != null) {
//实现ITestOfHidl中的回调,即获取ITestOfHidl这个服务中返回的数据
mTestOfHidl.setDataCallback(new IDataCallback.Stub() {
@Override
public void onDataReceived(ArrayList<Byte> data) throws RemoteException {
Log.d(TAG, "enter onDataReceived");
if ( data != null) {
//开始“解析”和获取数据
byte[] result = new byte[data.size()];
for (int i = 0; i < data.size(); i++) {
result[i] = data.get(i);
}
//listener是跟aidl相关的监听,注意是aidl,不是hidl
//hidl的监听在上一章“Android C++服务创建和HIDL的生成”
if(listener != null){
//通过aidl的监听发送来自hidl服务返回的数据
//数据的流动:hidl服务端->aidl服务端(hidl客户端)->aidl客户端
listener.onUartDataReceived(result);
}else {
Log.d(TAG, "listener == null");
}
}else {
Log.d(TAG, "data == null");
}
}
});
}
} catch (RemoteException | NoSuchElementException e) {
e.printStackTrace();
}
}
public void setContext(Context context) {
this.mContext = context.getApplicationContext();
}
@Override
public int getDisplayInputPortState(int port) throws RemoteException {
switch (port) {
case 1:
return 1;
case 2:
return 2;
case 3:
return 3;
default:
return -1;
}
}
//aidl的实现,这个接口给aidl客户端使用,客户端只需调用sendUartData(byte[] data)接口即可,
//将指令byte[] data从AIDL客户端->AIDL服务端(HDIL客户端)->HIDL服务端
//HIDL服务端接收到AIDL客户端发送的指令,经过HIDL服务内部的一些处理,
//通过ApiImpl()内的HIDL回调onDataReceived,返回一些数据给AIDL的客户端
@Override
public void sendUartData(byte[] data) throws RemoteException {
if (data == null || data.length == 0) {
Log.e(TAG, "data can not be null or empty");
return;
}
if (mTestOfHidl != null) {
ArrayList<Byte> list = new ArrayList<Byte>();
for (byte datum : data) {
list.add(datum);
}
mTestOfHidl.sendData(list);
Log.e(TAG, "data already send!");
} else {
Log.e(TAG, "server is not ready");
}
}
@Override
public void setAPIListener(APIListener listener) throws RemoteException {
if(listener == null){
Log.e(TAG,"input listener NULL");
}
this.listener = listener;
}
}
二. client,AIDL的客户端
1.先将aidl文件复制到客户端app的项目中
@Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
//连接服务
Intent intent = new Intent("com.xxxxapi.intent.ACTION_API_SERVICE");
intent.setPackage("com.emdoor.xxxxapi");
bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
}
//连接
private final ServiceConnection mServiceConnection = new ServiceConnection() {
//如果连接上,就执行这里面的方法
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mxxxxAPI= xxxxAPI.Stub.asInterface(service);
if (mInnocnAPI != null) {
try {
//设置监听
mxxxxAPI.setAPIListener(apiListener);
//调用xxxxAPI中的sendUartData方法,将指令a发送到hidl的服务中
byte[] a = {0x66, (byte) 0x88, 0x13, 0x02, 0x00, 0x00, 0x03};
mxxxxAPI.sendUartData(a);
} catch (RemoteException e) {
e.printStackTrace();
}
} else {
Log.e(TAG, "xxxx API service not available");
}
}
@Override
public void onServiceDisconnected(ComponentName name) {
mInnocnAPI = null;
}
};
//数据回调,当a指令发送过去后,返回的数据,在这里接收
private final APIListener.Stub apiListener = new APIListener.Stub() {
@Override
public void onUartDataReceived(byte[] data) throws RemoteException {
Log.d(TAG,"data = " + data);
Vector v1 = new Vector(data.length);
for (int i = 0; i < data.length; i++) {
int k = data[i]&0xff;
//Log.d(TAG, "result = " + k);
v1.add(i,Integer.toHexString(k));
Log.d(TAG, "v1 = " + v1.get(i));
}
}
@Override
public void onDisplayInputStateChanged(int port, int state) throws RemoteException {
Log.d(TAG, "port = " + port + "," + "state = " + state);
}
@Override
public void onUsbPortStateChanged(int port, int state) throws RemoteException {
}
};
3.如有错误,请指正,谢谢
原文地址:https://blog.csdn.net/jamecer/article/details/123797916
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_39432.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。