跳转到内容

手机侧

Request request = new Request.Builder()
.action(Constant.Action.ACTION_DEVICE_BUSINESS_DATA)
.pkgName("com.vivo.health")
.data(data)
.build();
Response response = RpcClient.getInstance().callSync(request);
RpcLogger.i("test test_send response:" + response.getData());
String data = "业务自定义数据";
Request request = new Request.Builder()
.action(Constant.Action.ACTION_DEVICE_BUSINESS_DATA)
.pkgName("com.vivo.health")
.data(data)
.build();
RpcClient.getInstance().callAsync(request, new Callback() {
@Override
public void onResponse(Response response) {
MockLogger.i("test test_send_async response:" + response.getData());
}
});
DeviceRpcManager.getInstance().startDataReceiver(new IDataReceiver() {
@Override
public void onReceiveData(Request request) {
switch (request.getAction()){
case Constant.Action.ACTION_DEVICE_BUSINESS_DATA:
String data = request.getData(); //接收端数据处理
String result = "填写的response";
response = Util.responseData(request, result);
DeviceRpcManager.getInstance().onResponse(response);
break;
default:
break;
}
@Override
public void onReceiveNotification(Notification notification) {
//处理接收的notification事件
}
});
  • 请求的数据:
String data = "业务自定义数据";
Notification notification = new Notification.Builder()
.action(Constant.Action.ACTION_DEVICE_BUSINESS_DATA)
.pkgName("com.vivo.health")
.data(data)
.build();
RpcClient.getInstance().notify(notification);
  • 返回的数据:通知类数据没有返回结果。
code描述
0成功
1目标 app 没有处理对应 ACTION
2数据解析错误
3设备命令失败
4设备未连接
5请求设备数据超时
6设备不存在
7请求目标 app 超时
8目标 app 不支持 sdk 功能或目标 app 未安装
9权限拒绝
-1未知错误
  • ACTION_DEVICE_INFO 当前连接的设备信息
  • ACTION_DEVICE_BUSINESS_DATA 发送设备业务数据
  • ACTION_DEVICE_DYNAMIC 设备动态信息更新
获取运动健康功能版本号,版本号大于等于 2 说明具备当前能力
Section titled “获取运动健康功能版本号,版本号大于等于 2 说明具备当前能力”
int version = DeviceRpcManager.getInstance().getHealthDeviceVersion();
Request request = new Request.Builder()
.action(Constant.Action.ACTION_DEVICE_INFO)
.pkgName("com.vivo.health")
.data(data)
.build();
Response response = RpcClient.getInstance().callSync(request);
{
"device":
{
"deviceName":"Watch Name XXX2",
"battery":90, //范围0-100
"connected":true,
"freeStorage":200000, //单位byte
"mac":"11:11:11:11:11:11",
"productId":2,
"totalStorage":800000000 //单位byte
"batteryState":1 //充电状态
}
}
JSONObject jsonObject = new JSONObject();
jsonObject.put("msg", "业务自定义数据");
Request request = new Request.Builder()
.action(Constant.Action.ACTION_DEVICE_BUSINESS_DATA)
.pkgName("com.vivo.health")
.data(jsonObject.toJSONString())
.build();
Response response = RpcClient.getInstance().callSync(request);
data{"code":0}
DeviceRpcManager.getInstance().registerDataReceiver(new IDataReceiver() {
@Override
public void onReceiveRequest(Request request) {
}
@Override
public void onReceiveNotification(Notification notification) {
try {
RpcLogger.i("server onReceiveNotification:" + notification);
switch (notification.getAction()) {
case Constant.Action.ACTION_DEVICE_DYNAMIC:
String data = notification.getData();
break;
default:
break;
}
}catch (Exception e){
e.printStackTrace();
}
}
});
获取到的 data 数据:在 onReceiveNotification 回调中,得到的 data 是如下格式:
Section titled “获取到的 data 数据:在 onReceiveNotification 回调中,得到的 data 是如下格式:”
{
"device":
{
"deviceName":"Watch Name XXX2",
"battery":90, //范围0-100
"connected":true,
"freeStorage":200000, //单位byte
"mac":"11:11:11:11:11:11",
"productId":2,
"totalStorage":800000000 //单位byte
"batteryState":1 //充电状态
}
}