Bluetooth - blockdate/ios-rn-sdk GitHub Wiki
蓝牙设备操作类
Export:
Example
import {Device} from 'miot'
const ble = Device.getBluetoothLE();
ble.connect().then(ble=>{
ble.startDiscoverServices("a-b-c-d-e", ...)
...
});
...
ble.getService("a-b-c-d-e").startDiscoverCharacteristics("1-2-3-4-5",...)
...
const charac = ble.getService('...').getCharacteristic('...')
charac.setNotify().then(characteristic=>{}).catch(err=>{});
charac.read().then(characteristic=>{characteristic.value ... }).catch(err=>{});
charac.write().then(characteristic=>{}).catch(err=>{})
...
ble.disconnect()-
miot/Bluetooth
-
module.exports ⏏
-
static
-
.IBluetoothCharacteristic
-
.isDiscovered :
boolean -
.isValueLoaded :
boolean -
.UUID :
string -
.value :
* -
.read() ⇒
Promise.<IBluetoothCharacteristic> -
.write(value) ⇒
Promise.<IBluetoothCharacteristic> -
.writeWithoutResponse(value) ⇒
Promise.<IBluetoothCharacteristic> -
.setNotify(flag) ⇒
Promise.<IBluetoothCharacteristic>
-
.isDiscovered :
-
.IBluetoothService
-
.UUID :
string -
.isDiscovered :
boolean -
.getCharacteristic ⇒
IBluetoothCharacteristic -
.startDiscoverCharacteristics(...characteristicUUIDs) ⇒
boolean
-
.UUID :
-
.IBluetoothLock
-
.toggle(cmd, timeout) ⇒
Promise.<IBluetoothLock> -
.isShareKeyValid() ⇒
Promise.<IBluetoothLock> -
.getOneTimePassword(interval, digits) ⇒
Promise.<Array.<int>> -
.encryptMessage(message) ⇒
Promise.<string> -
.decryptMessage(encrypted) ⇒
Promise.<string>
-
.toggle(cmd, timeout) ⇒
-
.IBluetooth
-
.isBLE :
boolean -
.mac :
string -
.UUID :
string -
.isConnected :
boolean -
.connect(type, option) ⇒
Promise.<IBluetooth> -
.readRSSI() ⇒
Promise.<*> - .disconnect(delay)
- .maximumWriteValueLength(type)
-
.getVersion() ⇒
Promise.<any>
-
.isBLE :
-
.IBluetoothLE
-
.securityLock ⇒
IBluetoothLock -
.getService ⇒
IBluetoothService -
.startDiscoverServices(...serviceUUIDs) ⇒
boolean -
.getVersion(decrypt) ⇒
boolean
-
.securityLock ⇒
- .IBluetoothClassic
-
.createBluetoothLE(macOrPeripheralUUID) ⇒
IBluetoothLE -
.createBluetoothClassic(macOrPeripheralUUID) ⇒
IBluetoothClassic -
.checkBluetoothIsEnabled() ⇒
Promise.<boolean> -
.startScan(durationInMillis, ...serviceUUIDs) ⇒
void -
.stopScan() ⇒
void -
.retrievePeripheralsForIOS(...UUIDs) ⇒
Promise.<Map.<uuid, Bluetooth>> -
.retrievePeripheralsWithServicesForIOS(...serviceUUIDs) ⇒
Promise.<Map.<uuid, Bluetooth>> - .enableBluetoothForAndroid(silence) ⇒
-
.isBleGatewayConnected(mac) ⇒
Promise.<boolean> - .bindDeviceforMIUI(mac)
- .unBindDeviceforMIUI(mac)
- .setAlertConfigsOnMIUI(alert, enable, mac)
-
.IBluetoothCharacteristic
-
inner
-
~BluetoothEvent :
object- "bluetoothConnectionStatusChanged" (bluetooh, isConnected)
- "bluetoothDeviceDiscovered" (bluetoohData)
- "bluetoothDeviceDiscoverFailed" (error)
- "bluetoothSeviceDiscovered" (bluetooh, ...service)
- "bluetoothSeviceDiscoverFailed" (bluetooh, error)
- "bluetoothCharacteristicDiscovered" (bluetooh, service, ...characters)
- "bluetoothCharacteristicDiscoverFailed" (bluetooh, service, error)
- "bluetoothCharacteristicValueChanged" (bluetooh, service, character, value)
- "bluetoothStatusChanged" (isEnabled)
-
~BluetoothEvent :
-
static
-
module.exports ⏏
Kind: static interface of module.exports
-
.IBluetoothCharacteristic
-
.isDiscovered :
boolean -
.isValueLoaded :
boolean -
.UUID :
string -
.value :
* -
.read() ⇒
Promise.<IBluetoothCharacteristic> -
.write(value) ⇒
Promise.<IBluetoothCharacteristic> -
.writeWithoutResponse(value) ⇒
Promise.<IBluetoothCharacteristic> -
.setNotify(flag) ⇒
Promise.<IBluetoothCharacteristic>
-
.isDiscovered :
是否已经被发现
Kind: instance property of IBluetoothCharacteristic
Read only: true
数值是否已经加载, 为 true 时,本类才能读到正确的 value
Kind: instance property of IBluetoothCharacteristic
Read only: true
特征值的 UUID
Kind: instance property of IBluetoothCharacteristic
Read only: true
数值, 配合 isValueLoaded 使用
Kind: instance property of IBluetoothCharacteristic
Read only: true
Example
...
if(charateristic.isValueLoaded){
const val = characteristic.value;
...
}
...读取蓝牙数据
Kind: instance method of IBluetoothCharacteristic
写数据 对应 writeWithResponse
Kind: instance method of IBluetoothCharacteristic
| Param | Type |
|---|---|
| value | * |
直接写数据 对应 writeWithoutResponse
Kind: instance method of IBluetoothCharacteristic
| Param | Type |
|---|---|
| value | * |
设置数值变化监听开关
Kind: instance method of IBluetoothCharacteristic
| Param | Type | Description |
|---|---|---|
| flag | boolean |
true 打开监听, false 则关闭监听 |
Kind: static interface of module.exports
-
.IBluetoothService
-
.UUID :
string -
.isDiscovered :
boolean -
.getCharacteristic ⇒
IBluetoothCharacteristic -
.startDiscoverCharacteristics(...characteristicUUIDs) ⇒
boolean
-
.UUID :
蓝牙服务 UUID
Kind: instance property of IBluetoothService
Read only: true
蓝牙服务是否已被发现
Kind: instance property of IBluetoothService
Read only: true
获取蓝牙特征
Kind: instance property of IBluetoothService
| Param | Type |
|---|---|
| characteristicUUID | string |
发现蓝牙特征
Kind: instance method of IBluetoothService
| Param | Type | Description |
|---|---|---|
| ...characteristicUUIDs | string |
特征的 UUID |
Kind: static interface of module.exports
-
.IBluetoothLock
-
.toggle(cmd, timeout) ⇒
Promise.<IBluetoothLock> -
.isShareKeyValid() ⇒
Promise.<IBluetoothLock> -
.getOneTimePassword(interval, digits) ⇒
Promise.<Array.<int>> -
.encryptMessage(message) ⇒
Promise.<string> -
.decryptMessage(encrypted) ⇒
Promise.<string>
-
.toggle(cmd, timeout) ⇒
支持小米加密芯片的蓝牙设备,开关蓝牙锁
Kind: instance method of IBluetoothLock
| 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'})
})
...支持小米加密芯片的蓝牙设备,在被分享的设备中,调用此方法,可判断分享的电子钥匙是否有效
Kind: instance method of IBluetoothLock
Example
import {Bluetooth} from 'miot'
...
Bluetooth.createBluetoothLE(...).securityLock().isShareKeyValid()
.then(lock => {console.log('ShareKey is valid')})
.catch(err => {console.log('ShareKey isn't valid'})
...支持小米加密芯片的蓝牙设备,获取一次性密码组 假设输入 interval 为 30,则会从当日 0 点开始计算,每 30 分钟为一个刷新间隔。生成的密码在当前刷新间隔及下一个刷新间隔内有效。 如当日 10:19 生成,则该组密码在 10:00 ~ 10:30(当前刷新间隔) 以及 10:30 ~ 11:00 (下一个刷新间隔) 有效。 密码组中每条密码使用一次即过期。 注意设备上获取当前时间(UTC,精度为秒)的准确性由设备保证,否则会有计算误差。
Kind: instance method of IBluetoothLock
| Param | Type | Description |
|---|---|---|
| interval | int |
时间间隔,单位为分钟,类型为 number,传入 10 到 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 IBluetoothLock
| 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 IBluetoothLock
| Param | Type | Description |
|---|---|---|
| encrypted | string |
密文 |
Example
import {Bluetooth} from 'miot'
...
Bluetooth.createBluetoothLE(...).securityLock().encryptMessage('decryptedMessage')
.then(msg => {console.log('decrypt message is ', msg)})
.catch(err => {console.log('decrypt message failed, ', err})
...Kind: static interface of module.exports
-
.IBluetooth
-
.isBLE :
boolean -
.mac :
string -
.UUID :
string -
.isConnected :
boolean -
.connect(type, option) ⇒
Promise.<IBluetooth> -
.readRSSI() ⇒
Promise.<*> - .disconnect(delay)
- .maximumWriteValueLength(type)
-
.getVersion() ⇒
Promise.<any>
-
.isBLE :
是否 BLE 蓝牙
Kind: instance property of IBluetooth
Read only: true
蓝牙设备的 mac 地址
Kind: instance property of IBluetooth
Read only: true
蓝牙设备的 UUID
Kind: instance property of IBluetooth
Read only: true
蓝牙是否已经连接
Kind: instance property of IBluetooth
Read only: true
打开蓝牙链接. option参数peripheralID为iOS 平台的可选参数,因为iOS平台无法获取普通 BLE 蓝牙设备的 Mac,所以当 type 为 3 时,iOS 上需填入 peripheralID peripheralID 可通过 startScan()搜索周边蓝牙设备获取(如设备OTA中,设备固件切换,无小米蓝牙协议相关服务时需建立连接),或通过retrievePeripheralsWithServicesForIOS()搜索已连接设备获取(如可穿戴长连接设备,无法发送 mibeacon) 建立连接后,SDK 会用 peripheralID 充当 Mac 地址
Kind: instance method of IBluetooth
| Param | Type | Default | Description |
|---|---|---|---|
| type | int |
蓝牙设备类型 -1 自动判断,0 普通小米蓝牙协议设备,1 安全芯片小米蓝牙设备(比如锁类产品),2 分享的安全芯片小米蓝牙设备,3 普通的BLE蓝牙设备(无 mibeacon,无小米 FE95 service) | |
| option | json |
0 |
附加参数, 格式 {timeout:12000, peripheralID:"..."}, timeout的单位为毫秒, peripheralID是iOS平台参数 |
Example
Device.getBluetoothLE()
.connect(3, {peripheralID:"1-a-b-3-c", timeout:12000})
.then(ble=>{
...
})
.catch(err=>{
...
});读取 RSSI
Kind: instance method of IBluetooth
关闭链接
Kind: instance method of IBluetooth
| Param | Type | Default | Description |
|---|---|---|---|
| delay | int |
0 |
延迟时长(毫秒) |
获取当前连接设备写操作每包最大长度 注:有开发者反馈该系统接口在 iOS 上并不完全准确,不可过于依赖,以实际测试为准 注:返回值单位为 bit,注意换算,8 bit 为 1 byte,两字符 hexString 长度为 1 byte,如 “FF”
Kind: instance method of IBluetooth
| Param | Type | Default | Description |
|---|---|---|---|
| type | int |
0 |
0 代表 writeWithResponse, 1 代表 writeWithoutResponse |
更新版本号,蓝牙的版本号 connect 之后才能
Kind: instance method of IBluetooth
Kind: static interface of module.exports
-
.IBluetoothLE
-
.securityLock ⇒
IBluetoothLock -
.getService ⇒
IBluetoothService -
.startDiscoverServices(...serviceUUIDs) ⇒
boolean -
.getVersion(decrypt) ⇒
boolean
-
.securityLock ⇒
蓝牙锁相关操作
Kind: instance property of IBluetoothLE
获取蓝牙服务
Kind: instance property of IBluetoothLE
| Param | Type |
|---|---|
| serviceUUID | string |
发现服务
Kind: instance method of IBluetoothLE
| Param | Type |
|---|---|
| ...serviceUUIDs | string |
获取蓝牙固件版本
Kind: instance method of IBluetoothLE
| Param | Type |
|---|---|
| decrypt | boolean |
Kind: static interface of module.exports
创建BLE蓝牙设备,
Kind: static method of module.exports
| Param | Type | Description |
|---|---|---|
| macOrPeripheralUUID | string |
- iOS传 peripheralUUID, android 传 mac |
Example
import Bluetooth from 'miot/Bluetooth'
const ble = Bluetooth.createBluetoothLE("a.b.c...")创建经典蓝牙设备
Kind: static method of module.exports
| Param | Type | Description |
|---|---|---|
| macOrPeripheralUUID | string |
- iOS传 peripheralUUID, android 传 mac |
Example
import Bluetooth from 'miot/Bluetooth'
const bludtoothClassic = Bluetooth.createBluetoothClassic("a.b.c...")判断蓝牙是否开放
Kind: static method of module.exports
开始扫描蓝牙设备
Kind: static method of module.exports
| Param | Type | Description |
|---|---|---|
| durationInMillis | int |
扫描时长 |
| ...serviceUUIDs | string |
指定扫描, 为空时扫描全部 |
Example
import Bluetooth from 'miot/Bluetooth'
Bluetooth.startScan(3000, 'FE95','FE96')停止扫描蓝牙设备
Kind: static method of module.exports
iOS 平台获取已连接 BLE Peripheral,适用于可穿戴长连接设备 对应 coreBLuetooth 中 retrievePeripheralsWithIdentifiers:(NSArray<NSUUID *> *)identifiers 方法
Kind: static method of module.exports
Returns: Promise.<Map.<uuid, Bluetooth>> - //@mark ios done
| Param | Type | Description |
|---|---|---|
| ...UUIDs | string |
Peripheral UUIDs |
Example
Bluetooth.retrievePeripheralsForIOS("PeripheralUUID1","PeripheralUUID2","PeripheralUUID3")module.exports.retrievePeripheralsWithServicesForIOS(...serviceUUIDs) ⇒ Promise.<Map.<uuid, Bluetooth>>
iOS 平台通过 serviceUUID 获取已连接 BLE Peripheral,适用于可穿戴长连接设备 对应 coreBLuetooth 中 retrieveConnectedPeripheralsWithServices:(NSArray<CBUUID *> *)serviceUUIDs 方法
Kind: static method of module.exports
Returns: Promise.<Map.<uuid, Bluetooth>> - //@mark ios done
| Param | Type | Description |
|---|---|---|
| ...serviceUUIDs | string |
Peripheral serviceUUIDs |
Example
Bluetooth.retrievePeripheralsWithServicesForIOS("serviceUUID1","serviceUUID2","serviceUUID3")打开蓝牙
Kind: static method of module.exports
Returns: void
| Param | Type |
|---|---|
| silence | boolean |
判断当前设备是否通过蓝牙网关扫描到了
Kind: static method of module.exports
| Param | Type |
|---|---|
| mac | string |
只在MIUI上支持,维持长连接 如果连接失败,则会隔一段时间尝试重连,如果继续失败,则重连间隔会翻倍,直到上限。
Kind: static method of module.exports
| Param | Type |
|---|---|
| mac | string |
只在MIUI上支持,解除长连接
Kind: static method of module.exports
| Param | Type |
|---|---|
| mac | string |
只在MIUI上支持,维持长连接 如果连接失败,则会隔一段时间尝试重连,如果继续失败,则重连间隔会翻倍,直到上限。
Kind: static method of module.exports
| Param | Type |
|---|---|
| alert | * |
| enable | * |
| mac | string |
蓝牙事件名集合
Kind: inner namespace of module.exports
Example
import {BluetoothEvent} from 'miot'
const subscription = BluetoothEvent.bluetoothServiceDiscovered.addListener(
(bluetooth, ...services)=>{
...
}
)
...
subscription.remove()
...-
~BluetoothEvent :
object- "bluetoothConnectionStatusChanged" (bluetooh, isConnected)
- "bluetoothDeviceDiscovered" (bluetoohData)
- "bluetoothDeviceDiscoverFailed" (error)
- "bluetoothSeviceDiscovered" (bluetooh, ...service)
- "bluetoothSeviceDiscoverFailed" (bluetooh, error)
- "bluetoothCharacteristicDiscovered" (bluetooh, service, ...characters)
- "bluetoothCharacteristicDiscoverFailed" (bluetooh, service, error)
- "bluetoothCharacteristicValueChanged" (bluetooh, service, character, value)
- "bluetoothStatusChanged" (isEnabled)
蓝牙断开连接
Kind: event emitted by BluetoothEvent
| Param | Type | Description |
|---|---|---|
| bluetooh | IBluetooth |
发生连接打开关闭事件的蓝牙设备 |
| isConnected | boolean |
当前连接状态 |
蓝牙设备扫描发现事件
Kind: event emitted by BluetoothEvent
| Param | Type | Description |
|---|---|---|
| bluetoohData | json |
-扫描发现的蓝牙设备数据 |
蓝牙设备扫描发现失败事件
Kind: event emitted by BluetoothEvent
| Param | Type | Description |
|---|---|---|
| error | * |
错误信息 |
蓝牙服务发现事件
Kind: event emitted by BluetoothEvent
| Param | Type | Description |
|---|---|---|
| bluetooh | IBluetooth |
蓝牙设备 |
| ...service | IBluetoothService |
发现的蓝牙服务 |
蓝牙服务发现失败事件
Kind: event emitted by BluetoothEvent
| Param | Type | Description |
|---|---|---|
| bluetooh | IBluetooth |
蓝牙设备 |
| error | * |
错误信息 |
蓝牙特征发现事件
Kind: event emitted by BluetoothEvent
| Param | Type | Description |
|---|---|---|
| bluetooh | IBluetooth |
蓝牙设备 |
| service | IBluetoothService |
蓝牙特征 |
| ...characters | IBluetoothCharacteristic |
发现的蓝牙特征 |
蓝牙特征发现失败事件
Kind: event emitted by BluetoothEvent
| Param | Type | Description |
|---|---|---|
| bluetooh | IBluetooth |
蓝牙设备 |
| service | IBluetoothService |
蓝牙特征 |
| error | * |
错误信息 |
蓝牙特征值变更事件 notify, read
Kind: event emitted by BluetoothEvent
| Param | Type | Description |
|---|---|---|
| bluetooh | IBluetooth |
蓝牙设备 |
| service | IBluetoothService |
蓝牙服务 |
| character | IBluetoothCharacteristic |
蓝牙特征 |
| value | * |
数值 |
蓝牙开关状态变更事件
Kind: event emitted by BluetoothEvent
| Param | Type | Description |
|---|---|---|
| isEnabled | boolean |
当前状态 |