mi_band小米穿戴第三方 APP 能力开放接口文档_v1.4
Aristore整理自小米穿戴第三方 APP 能力开放接口文档
手环小程序开放接口文档
1.4 版本更新日志
兼容 Android R
1.3 版本更新日志
兼容未来小米穿戴和小米健康合并项目,api 无变化,直接替换 sdk 即可
1.2 版本更新日志:
兼容 jdk1.7 版本,api 没有变化
1.1 版本更新日志:
-
修改状态订阅和状态查询相关接口返回值,详情请查看文档 3.2 和 3.3 部分
-
增加消息通知 api,详情请看文档第 5 部分
1. 查询已连接的可穿戴设备(不需要权限)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| NodeApi api = Wearable.getNodeApi(context);
api.getConnectedNodes() .addOnSuccessListener(new OnSuccessListener<List<Node>>() { @Override public void onSuccess(List<Node> nodes) { } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { } });
|
2. 权限查询与申请
2.1 查询授权状态
第一次调用授权会默认授予 Permission.DEVICE_MANAGER 和 Permission.Notify 权限
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| AuthApi authApi = Wearable.getAuthApi(context);
authApi.checkPermission("nodeId", Permission.DEVICE_MANAGER) .addOnSuccessListener(new OnSuccessListener<Boolean>() { @Override public void onSuccess(Boolean result) { } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { } });
Permission[] permissions = new Permission[]{ Permission.DEVICE_MANAGER, Permission.NOTIFY };
authApi.checkPermissions("nodeId", permissions) .addOnSuccessListener(new OnSuccessListener<Boolean[]>() { @Override public void onSuccess(Boolean[] results) { } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { } });
|
2.2 申请权限
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| AuthApi authApi = Wearable.getAuthApi(context);
authApi.requestPermission("nodeId", Permission.DEVICE_MANAGER, Permission.NOTIFY) .addOnSuccessListener(new OnSuccessListener<Permission[]>() { @Override public void onSuccess(Permission[] permissions) { } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { } });
|
3. 设备管理与状态订阅
3.1 支持查询与订阅的设备状态
设备管理与状态订阅 |
查询结果 |
订阅事件触发条件 |
订阅结果 |
连接状态 |
1. 连接 2. 未连接 |
1. 手机与设备连接成功 2. 手机与设备断开连接 |
1. 连接成功 2. 连接断开 3. 连接失败 4. 设备被删除 |
电量状态 |
电量值(0~100,比如 98 ) |
N/A |
N/A |
充电状态 |
1. 正在充电 2. 非充电状态 |
1. 给设备充电 2. 充满电 3. 停止充电
|
1. 开始充电 2. 充电完成 3. 停止充电 |
佩戴状态 |
1. 佩戴中 2. 未佩戴 |
1. 手表戴在手腕上 2. 摘下手表 |
1. 佩戴 2. 未佩戴 |
睡眠状态 |
1. 睡眠中 2. 清醒 |
1. 佩戴手表入睡 2. 从入睡状态清醒过来 |
1. 入睡 2. 出睡 |
3.2 状态查询(需要申请 Permission.DEVICE_MANAGER 权限)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| NodeApi api = Wearable.getNodeApi(context);
api.query("nodeId", DataItem.ITEM_CONNECTION) .addOnSuccessListener(new OnSuccessListener<DataQueryResult>() { @Override public void onSuccess(DataQueryResult result) { boolean connectionStatus = result.isConnected(); } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { } });
api.isWearAppInstalled("nodeId") .addOnSuccessListener(new OnSuccessListener<Boolean>() { @Override public void onSuccess(Boolean result) { } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { } });
api.launchWearApp("nodeId", "uri") .addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void aVoid) { } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { } });
|
3.3 状态订阅(需要申请 Permission.DEVICE_MANAGER 权限)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| NodeApi api = Wearable.getNodeApi(context);
OnDataChangedListener onDataChangedListener = new OnDataChangedListener() { @Override public void onDataChanged(@NonNull String nodeId, @NonNull DataItem dataItem, @NonNull DataSubscribeResult data) { if (dataItem.getType() == DataItem.ITEM_CONNECTION.getType()) { int connectionStatus = data.getConnectedStatus(); if (connectionStatus == DataSubscribeResult.RESULT_CONNECTION_CONNECTED) { } } } };
api.subscribe("nodeId", DataItem.ITEM_CONNECTION, onDataChangedListener) .addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void aVoid) { } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { } });
api.unsubscribe("nodeId", DataItem.ITEM_CONNECTION) .addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void aVoid) { } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { } });
|
4. 应用间消息通信(需要申请 Permission.DEVICE_MANAGER 权限)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| byte[] messageBytes = new byte[1024];
MessageApi messageApi = Wearable.getMessageApi(context);
messageApi.sendMessage("nodeId", messageBytes) .addOnSuccessListener(new OnSuccessListener<Integer>() { @Override public void onSuccess(Integer result) { } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { } });
OnMessageReceivedListener onMessageReceivedListener = new OnMessageReceivedListener() { @Override public void onMessageReceived(@NotNull String nodeId, @NotNull byte[] message) { } };
messageApi.addListener("nodeId", onMessageReceivedListener) .addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void aVoid) { } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { } });
messageApi.removeListener("nodeId", onMessageReceivedListener) .addOnSuccessListener(new OnSuccessListener<Void>() { @Override public void onSuccess(Void aVoid) { } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { } });
|
5. 消息通知(需要 Permission.NOTIFY 权限)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| NotifyApi notifyApi = Wearable.getNotifyApi(context);
notifyApi.sendNotify("nodeId", "title", "message") .addOnSuccessListener(new OnSuccessListener<Status>() { @Override public void onSuccess(Status status) { if (status.isSuccess()) { } } }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { } });
|
6. 管理服务连接状态(不需要权限)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| ServiceApi serviceApi = Wearable.getServiceApi(context);
OnServiceConnectionListener onServiceConnectionListener = new OnServiceConnectionListener() { @Override public void onServiceConnected() {
} @Override public void onServiceDisconnected() {
} };
serviceApi.registerServiceConnectionListener(onServiceConnectionListener);
serviceApi.unregisterServiceConnectionListener(onServiceConnectionListener);
|