03 蓝牙锁模块 - MiEcosystem/miot-plugin-sdk GitHub Wiki
蓝牙锁操作类 蓝牙锁的开发,详见:https://iot.mi.com/new/doc/extension-development/topics/bluetooth-lock 本文件主要提供了蓝牙锁的开关锁,蓝牙锁密钥的分享,获取一次性开锁密钥,锁相关数据加解密等功能
Export: public
Doc_name: 蓝牙锁模块
Doc_index: 3
Doc_directory: bluetooth
Example
import {Bluetooth} from 'miot/device/bluetooth'
...
Bluetooth.createBluetoothLE(...).connect(...).then(device => {
device.securityLock().toggle(0,5000)
.then(lock => {console.log('toggle success')})
.catch(err => {console.log('toggle failed'})
})
Bluetooth.createBluetoothLE(...).securityLock().encryptMessage('message')
.then(msg => {console.log('encrypted message is ', msg)})
.catch(err => {console.log('encrypted message failed, ', err})
Bluetooth.createBluetoothLE(...).securityLock().encryptMessage('decryptedMessage')
.then(msg => {console.log('decrypt message is ', msg)})
.catch(err => {console.log('decrypt message failed, ', err})
...
-
miot/device/bluetooth
-
module.exports ⏏
-
.toggle(cmd, timeout) ⇒
[ 'Promise' ].<IBluetoothLock>
-
.isShareKeyValid() ⇒
Promise
-
.getOneTimePassword(interval, digits) ⇒
[ 'Promise' ].<Array.<int>>
-
.encryptMessage(message) ⇒
[ 'Promise' ].<string>
-
.decryptMessage(encrypted) ⇒
[ 'Promise' ].<string>
-
.encryptMessageWithToken(data) ⇒
[ 'Promise' ].<json>
-
.decryptMessageWithToken(data) ⇒
[ 'Promise' ].<json>
-
.toggle(cmd, timeout) ⇒
-
module.exports ⏏
Kind: Exported interface
支持小米加密芯片的蓝牙设备,开关蓝牙锁
Kind: instance method of module.exports
Returns: [ 'Promise' ].<IBluetoothLock>
- resolve:hex string
reject:{code: xxx, message: xxx} 1:设备正在切换中 2:加密失败 3:找不到服务 4:超时
Param | Type | Description |
---|---|---|
cmd | int |
操作命令可传入 0 ,1 ,2三个 int 值,分别代表 开锁,上锁,反锁 |
timeout | int |
毫秒 蓝牙未响应的超时时间 |
Example
import {Bluetooth} from 'miot'
...
Bluetooth.createBluetoothLE(...).connect(...).then(device => {
device.securityLock.toggle(0,5000)
.then(lock => {console.log('toggle success')})
.catch(err => {console.log('toggle failed'})
})
...
支持小米加密芯片的蓝牙设备,在被分享的设备中,调用此方法,可判断分享的电子钥匙是否有效。设备owner调用此方法会走reject
Kind: instance method of module.exports
Returns: Promise
- resolve:null
reject:null
Example
import {Bluetooth} from 'miot'
...
Bluetooth.createBluetoothLE(...).securityLock.isShareKeyValid()
.then(lock => {console.log('ShareKey is valid')})
.catch(err => {console.log('ShareKey isn't valid'})
...
支持小米加密芯片的蓝牙设备,获取一次性密码组。 设备owner调用此方法才有效 假设输入 interval 为 30,则会从当日 0 点开始计算,每 30 分钟为一个刷新间隔。生成的密码在当前刷新间隔及下一个刷新间隔内有效。 如当日 10:19 生成,则该组密码在 10:00 ~ 10:30(当前刷新间隔) 以及 10:30 ~ 11:00 (下一个刷新间隔) 有效。 密码组中每条密码使用一次即过期。 注意设备上获取当前时间(UTC,精度为秒)的准确性由设备保证,否则会有计算误差。
Kind: instance method of module.exports
Returns: [ 'Promise' ].<Array.<int>>
- resolve:int[8],意思是生成8个一次性密码,每个密码的长度等于digits。比如 [123456,234567,....]
reject:{code: xxx, message:xxx} 1:设备owner才可调用 2:参数不正确 3:生成的密码长度不对 4:网络错误
Param | Type | Description |
---|---|---|
interval | int |
时间间隔,单位为分钟,类型为 number,传入 10 到 60 的整数(建议用整数10,20,30,40,50,60) |
digits | int |
密码位数,类型为 number,传入 6 到 8 的整数 |
Example
import {Bluetooth} from 'miot'
...
Bluetooth.createBluetoothLE(...).securityLock.getOneTimePassword(30,6)
.then(pwd => {console.log('one time password is ', pwd)})
.catch(err => {console.log('get one time password failed, ', err})
...
支持小米加密芯片的蓝牙设备,使用此方法将明文加密为密文后,可发送给设备。然后小米加密芯片会解密,设备端可以直接拿到解密后的数据。
Kind: instance method of module.exports
Returns: [ 'Promise' ].<string>
- resolve: 加密后的string
reject:{code: xxx, message: xxx} 1:必须是16进制字符串 2:设备未绑定 3:加密出错
Param | Type | Description |
---|---|---|
message | string |
明文 |
Example
import {Bluetooth} from 'miot'
...
Bluetooth.createBluetoothLE(...).securityLock.encryptMessage('message')
.then(msg => {console.log('encrypted message is ', msg)})
.catch(err => {console.log('encrypted message failed, ', err})
...
支持小米加密芯片的蓝牙设备,使用此方法将密文解密为明文
Kind: instance method of module.exports
Returns: [ 'Promise' ].<string>
- resolve:解密后的string
reject:{code: xxx, message: xxx} 1:必须是16进制字符串 2:设备未绑定 3:解密出错
Param | Type | Description |
---|---|---|
encrypted | string |
密文 |
Example
import {Bluetooth} from 'miot'
...
Bluetooth.createBluetoothLE(...).securityLock.decryptMessage('decryptedMessage')
.then(msg => {console.log('decrypt message is ', msg)})
.catch(err => {console.log('decrypt message failed, ', err})
...
使用设备的token加密指定数据
Kind: instance method of module.exports
Returns: [ 'Promise' ].<json>
- resolve:{"result": :"encripted string"} result字段即为加密后的string
reject:{code: xxx, message: xxx} 1:必须16进制字符串 2:获取device token 失败 3:加密失败
Since: 10004
Param | Type | Description |
---|---|---|
data | string |
Hex Data String |
使用设备的token解密指定数据
Kind: instance method of module.exports
Returns: [ 'Promise' ].<json>
- resolve:{"result": :"encripted string"} result字段即为解密后的string
reject:{code: xxx, message: xxx} 1:必须16进制字符串 2:获取device token 失败 3:解密失败
Since: 10004
Param | Type | Description |
---|---|---|
data | strng |
Hex Data String |