通过AIDL的方式 与 串口通讯的APP通讯。
- maven依赖方式
dependencies {
implementation 'aidog.littlegreens:controllib:1.0.3'
}
- 需要的话可提供aar/jar 的方式
- 绑定远程服务、解绑远程服务
- 提供了和远程服务通信接口
- 提供了接口回调,接收远程服务回调的通知。
ControlManager controlManager = ControlManager.getInstance(context);
- 绑定
if (!controlManager.isBound()) {
controlManager.binderServer();
}
- 解绑
if (!controlManager.isBound()) {
controlManager.unBindServer();
}
- 是否已经绑定
controlManager.isBound();
- 开门
boolean isServerBinder = controlManager.openDoor();
if (!isServerBinder) {
Toast.makeText(this, "远程服务没有绑定,请绑定后,再发送指令", Toast.LENGTH_SHORT).show();
}
- 查询门状态
controlManager.queryDoorStatus();
- 查询温度
controlManager.queryTemperature();
- 查询机型和固件版本
controlManager.queryDeviceVersion();
controlManager.setOnControlServerListener(new ControlServerListener() {
@Override
public void onServiceConnected() {
Toast.makeText(MainActivity.this, "远程服务绑定成功",
Toast.LENGTH_SHORT).show();
}
@Override
public void onServiceDisconnected() {
Toast.makeText(MainActivity.this, "远程服务断开连接",
Toast.LENGTH_SHORT).show();
}
@Override
public void valueChanged(DeviceBean deviceBean) {
Log.d(TAG, "valueChanged: " + deviceBean.toString());
switch (deviceBean.getFunctionType()) {
case FunctionType.OpenDoor:
int openDoorResponse = deviceBean.getOpenDoorResponse();
if (openDoorResponse == 0) {
mCallbackText.setText("开门失败");
} else {
mCallbackText.setText("开门成功");
}
break;
case FunctionType.QueryDoor:
int queryDoorStatusResponse = deviceBean.getQueryDoorStatusResponse();
if (queryDoorStatusResponse == 0) {
mCallbackText.setText("开门失败");
} else {
mCallbackText.setText("开门成功");
}
break;
case FunctionType.QueryTemperature:
float queryTemperatureResponse = deviceBean.getQueryTemperatureResponse();
if (queryTemperatureResponse == -1) {
mCallbackText.setText("温度传感器异常");
} else {
mCallbackText.setText("温度:" + queryTemperatureResponse + "摄氏度");
}
break;
case FunctionType.PersonNear:
mCallbackText.setText("人体靠近");
break;
case FunctionType.QueryDeviceVersion:
int deviceTypeResponse = deviceBean.getDeviceTypeResponse();
int deviceVersion = deviceBean.getDeviceVersion();
mCallbackText.setText("机型:" + deviceTypeResponse + " 固件版本:" + deviceVersion);
break;
}
}
});