Bluetooth - blockdate/ios-rn-sdk GitHub Wiki

miot/Bluetooth

蓝牙设备操作类

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()

module.exports ⏏

Kind: Exported member
Export:

module.exports.IBluetoothCharacteristic

Kind: static interface of module.exports

iBluetoothCharacteristic.isDiscovered : boolean

是否已经被发现

Kind: instance property of IBluetoothCharacteristic
Read only: true

iBluetoothCharacteristic.isValueLoaded : boolean

数值是否已经加载, 为 true 时,本类才能读到正确的 value

Kind: instance property of IBluetoothCharacteristic
Read only: true

iBluetoothCharacteristic.UUID : string

特征值的 UUID

Kind: instance property of IBluetoothCharacteristic
Read only: true

iBluetoothCharacteristic.value : *

数值, 配合 isValueLoaded 使用

Kind: instance property of IBluetoothCharacteristic
Read only: true
Example

...
  if(charateristic.isValueLoaded){
      const val = characteristic.value;
      ...
  }
  ...

iBluetoothCharacteristic.read() ⇒ Promise.<IBluetoothCharacteristic>

读取蓝牙数据

Kind: instance method of IBluetoothCharacteristic

iBluetoothCharacteristic.write(value) ⇒ Promise.<IBluetoothCharacteristic>

写数据 对应 writeWithResponse

Kind: instance method of IBluetoothCharacteristic

Param Type
value *

iBluetoothCharacteristic.writeWithoutResponse(value) ⇒ Promise.<IBluetoothCharacteristic>

直接写数据 对应 writeWithoutResponse

Kind: instance method of IBluetoothCharacteristic

Param Type
value *

iBluetoothCharacteristic.setNotify(flag) ⇒ Promise.<IBluetoothCharacteristic>

设置数值变化监听开关

Kind: instance method of IBluetoothCharacteristic

Param Type Description
flag boolean true 打开监听, false 则关闭监听

module.exports.IBluetoothService

Kind: static interface of module.exports

iBluetoothService.UUID : string

蓝牙服务 UUID

Kind: instance property of IBluetoothService
Read only: true

iBluetoothService.isDiscovered : boolean

蓝牙服务是否已被发现

Kind: instance property of IBluetoothService
Read only: true

iBluetoothService.getCharacteristic ⇒ IBluetoothCharacteristic

获取蓝牙特征

Kind: instance property of IBluetoothService

Param Type
characteristicUUID string

iBluetoothService.startDiscoverCharacteristics(...characteristicUUIDs) ⇒ boolean

发现蓝牙特征

Kind: instance method of IBluetoothService

Param Type Description
...characteristicUUIDs string 特征的 UUID

module.exports.IBluetoothLock

Kind: static interface of module.exports

iBluetoothLock.toggle(cmd, timeout) ⇒ Promise.<IBluetoothLock>

支持小米加密芯片的蓝牙设备,开关蓝牙锁

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'})
})
...

iBluetoothLock.isShareKeyValid() ⇒ Promise.<IBluetoothLock>

支持小米加密芯片的蓝牙设备,在被分享的设备中,调用此方法,可判断分享的电子钥匙是否有效

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'})
...

iBluetoothLock.getOneTimePassword(interval, digits) ⇒ Promise.<Array.<int>>

支持小米加密芯片的蓝牙设备,获取一次性密码组 假设输入 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})
...

iBluetoothLock.encryptMessage(message) ⇒ Promise.<string>

支持小米加密芯片的蓝牙设备,使用此方法将明文加密为密文后,可发送给设备

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})
...

iBluetoothLock.decryptMessage(encrypted) ⇒ Promise.<string>

支持小米加密芯片的蓝牙设备,使用此方法将明文加密为密文后,可发送给设备

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})
...

module.exports.IBluetooth

Kind: static interface of module.exports

iBluetooth.isBLE : boolean

是否 BLE 蓝牙

Kind: instance property of IBluetooth
Read only: true

iBluetooth.mac : string

蓝牙设备的 mac 地址

Kind: instance property of IBluetooth
Read only: true

iBluetooth.UUID : string

蓝牙设备的 UUID

Kind: instance property of IBluetooth
Read only: true

iBluetooth.isConnected : boolean

蓝牙是否已经连接

Kind: instance property of IBluetooth
Read only: true

iBluetooth.connect(type, option) ⇒ Promise.<IBluetooth>

打开蓝牙链接. 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=>{
         ...
      });

iBluetooth.readRSSI() ⇒ Promise.<*>

读取 RSSI

Kind: instance method of IBluetooth

iBluetooth.disconnect(delay)

关闭链接

Kind: instance method of IBluetooth

Param Type Default Description
delay int 0 延迟时长(毫秒)

iBluetooth.maximumWriteValueLength(type)

获取当前连接设备写操作每包最大长度 注:有开发者反馈该系统接口在 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

iBluetooth.getVersion() ⇒ Promise.<any>

更新版本号,蓝牙的版本号 connect 之后才能

Kind: instance method of IBluetooth

module.exports.IBluetoothLE

Kind: static interface of module.exports

iBluetoothLE.securityLock ⇒ IBluetoothLock

蓝牙锁相关操作

Kind: instance property of IBluetoothLE

iBluetoothLE.getService ⇒ IBluetoothService

获取蓝牙服务

Kind: instance property of IBluetoothLE

Param Type
serviceUUID string

iBluetoothLE.startDiscoverServices(...serviceUUIDs) ⇒ boolean

发现服务

Kind: instance method of IBluetoothLE

Param Type
...serviceUUIDs string

iBluetoothLE.getVersion(decrypt) ⇒ boolean

获取蓝牙固件版本

Kind: instance method of IBluetoothLE

Param Type
decrypt boolean

module.exports.IBluetoothClassic

Kind: static interface of module.exports

module.exports.createBluetoothLE(macOrPeripheralUUID) ⇒ IBluetoothLE

创建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...")

module.exports.createBluetoothClassic(macOrPeripheralUUID) ⇒ IBluetoothClassic

创建经典蓝牙设备

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...")

module.exports.checkBluetoothIsEnabled() ⇒ Promise.<boolean>

判断蓝牙是否开放

Kind: static method of module.exports

module.exports.startScan(durationInMillis, ...serviceUUIDs) ⇒ void

开始扫描蓝牙设备

Kind: static method of module.exports

Param Type Description
durationInMillis int 扫描时长
...serviceUUIDs string 指定扫描, 为空时扫描全部

Example

import Bluetooth from 'miot/Bluetooth'
     Bluetooth.startScan(3000, 'FE95','FE96')

module.exports.stopScan() ⇒ void

停止扫描蓝牙设备

Kind: static method of module.exports

module.exports.retrievePeripheralsForIOS(...UUIDs) ⇒ Promise.<Map.<uuid, Bluetooth>>

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")

module.exports.enableBluetoothForAndroid(silence) ⇒

打开蓝牙

Kind: static method of module.exports
Returns: void

Param Type
silence boolean

module.exports.isBleGatewayConnected(mac) ⇒ Promise.<boolean>

判断当前设备是否通过蓝牙网关扫描到了

Kind: static method of module.exports

Param Type
mac string

module.exports.bindDeviceforMIUI(mac)

只在MIUI上支持,维持长连接 如果连接失败,则会隔一段时间尝试重连,如果继续失败,则重连间隔会翻倍,直到上限。

Kind: static method of module.exports

Param Type
mac string

module.exports.unBindDeviceforMIUI(mac)

只在MIUI上支持,解除长连接

Kind: static method of module.exports

Param Type
mac string

module.exports.setAlertConfigsOnMIUI(alert, enable, mac)

只在MIUI上支持,维持长连接 如果连接失败,则会隔一段时间尝试重连,如果继续失败,则重连间隔会翻倍,直到上限。

Kind: static method of module.exports

Param Type
alert *
enable *
mac string

module.exports~BluetoothEvent : object

蓝牙事件名集合

Kind: inner namespace of module.exports
Example

import {BluetoothEvent} from 'miot'
   const subscription = BluetoothEvent.bluetoothServiceDiscovered.addListener(
      (bluetooth, ...services)=>{
         ...
      }
    )
   ...
   subscription.remove()
   ...

"bluetoothConnectionStatusChanged" (bluetooh, isConnected)

蓝牙断开连接

Kind: event emitted by BluetoothEvent

Param Type Description
bluetooh IBluetooth 发生连接打开关闭事件的蓝牙设备
isConnected boolean 当前连接状态

"bluetoothDeviceDiscovered" (bluetoohData)

蓝牙设备扫描发现事件

Kind: event emitted by BluetoothEvent

Param Type Description
bluetoohData json -扫描发现的蓝牙设备数据

"bluetoothDeviceDiscoverFailed" (error)

蓝牙设备扫描发现失败事件

Kind: event emitted by BluetoothEvent

Param Type Description
error * 错误信息

"bluetoothSeviceDiscovered" (bluetooh, ...service)

蓝牙服务发现事件

Kind: event emitted by BluetoothEvent

Param Type Description
bluetooh IBluetooth 蓝牙设备
...service IBluetoothService 发现的蓝牙服务

"bluetoothSeviceDiscoverFailed" (bluetooh, error)

蓝牙服务发现失败事件

Kind: event emitted by BluetoothEvent

Param Type Description
bluetooh IBluetooth 蓝牙设备
error * 错误信息

"bluetoothCharacteristicDiscovered" (bluetooh, service, ...characters)

蓝牙特征发现事件

Kind: event emitted by BluetoothEvent

Param Type Description
bluetooh IBluetooth 蓝牙设备
service IBluetoothService 蓝牙特征
...characters IBluetoothCharacteristic 发现的蓝牙特征

"bluetoothCharacteristicDiscoverFailed" (bluetooh, service, error)

蓝牙特征发现失败事件

Kind: event emitted by BluetoothEvent

Param Type Description
bluetooh IBluetooth 蓝牙设备
service IBluetoothService 蓝牙特征
error * 错误信息

"bluetoothCharacteristicValueChanged" (bluetooh, service, character, value)

蓝牙特征值变更事件 notify, read

Kind: event emitted by BluetoothEvent

Param Type Description
bluetooh IBluetooth 蓝牙设备
service IBluetoothService 蓝牙服务
character IBluetoothCharacteristic 蓝牙特征
value * 数值

"bluetoothStatusChanged" (isEnabled)

蓝牙开关状态变更事件

Kind: event emitted by BluetoothEvent

Param Type Description
isEnabled boolean 当前状态
⚠️ **GitHub.com Fallback** ⚠️