Module: miot/Bluetooth

蓝牙设备操作类

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

Interfaces

IBluetooth
IBluetoothCharacteristic
IBluetoothClassic
IBluetoothLE
IBluetoothLock
IBluetoothService

Namespaces

BluetoothEvent

Members

(static) UUID128

标准化蓝牙UUID为128位大写

Example
import {Bluetooth} from 'miot';
const myServiceUUID = Bluetooth.UUID128("0015");
const myCharacterUUID = Bluetooth.UUID128("f7255c06-e981-46f1-be3d-86c5cd1bb590");

Methods

(static) bindDeviceforMIUI(mac)

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

Parameters:
Name Type Description
mac string

(static) checkBluetoothIsEnabled() → {Promise.<boolean>}

判断蓝牙是否开放

Returns:
Type
Promise.<boolean>

(static) createBluetoothClassic(macOrPeripheralID) → {IBluetoothClassic}

创建经典蓝牙设备

Parameters:
Name Type Description
macOrPeripheralID string
  • iOS传 peripheralUUID, android 传 mac
Returns:
Type
IBluetoothClassic
Example
import Bluetooth from 'miot/Bluetooth'
  const bludtoothClassic = Bluetooth.createBluetoothClassic("a.b.c...")

(static) createBluetoothLE(macOrPeripheralID) → {IBluetoothLE}

创建BLE蓝牙设备,

Parameters:
Name Type Description
macOrPeripheralID string
  • iOS传 peripheralUUID, android 传 mac
Returns:
Type
IBluetoothLE
Example
import Bluetooth from 'miot/Bluetooth'
  const ble = Bluetooth.createBluetoothLE("a.b.c...")

(static) enableBluetoothForAndroid(silence)

打开蓝牙

Parameters:
Name Type Description
silence boolean
Returns:

void

(static) isBleGatewayConnected(mac) → {Promise.<boolean>}

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

Parameters:
Name Type Description
mac string
Returns:
Type
Promise.<boolean>

(static) isSameUUID(uuid1, uuid2)

用以判断两个 UUID 是否相等

Parameters:
Name Type Description
uuid1 string
uuid2 string

(static) retrievePeripheralsForIOS(…UUIDs) → {Promise.<Map.<uuid, Bluetooth>>}

iOS 平台获取已连接 BLE Peripheral,适用于可穿戴长连接设备 对应 coreBLuetooth 中 retrievePeripheralsWithIdentifiers:(NSArray *)identifiers 方法

Parameters:
Name Type Attributes Description
UUIDs string <repeatable>

Peripheral UUIDs

Returns:

//@mark ios done

Type
Promise.<Map.<uuid, Bluetooth>>
Example
Bluetooth.retrievePeripheralsForIOS("PeripheralUUID1","PeripheralUUID2","PeripheralUUID3")

(static) retrievePeripheralsWithServicesForIOS(…serviceUUIDs) → {Promise.<Map.<uuid, Bluetooth>>}

iOS 平台通过 serviceUUID 获取已连接 BLE Peripheral,适用于可穿戴长连接设备 对应 coreBLuetooth 中 retrieveConnectedPeripheralsWithServices:(NSArray *)serviceUUIDs 方法

Parameters:
Name Type Attributes Description
serviceUUIDs string <repeatable>

Peripheral serviceUUIDs

Returns:

//@mark ios done

Type
Promise.<Map.<uuid, Bluetooth>>
Example
Bluetooth.retrievePeripheralsWithServicesForIOS("serviceUUID1","serviceUUID2","serviceUUID3")

(static) setAlertConfigsOnMIUI(alert, enable, mac)

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

Parameters:
Name Type Description
alert *
enable *
mac string

(static) startScan(durationInMillis, …serviceUUIDs) → {void}

开始扫描蓝牙设备

Parameters:
Name Type Attributes Description
durationInMillis int

扫描时长

serviceUUIDs string <repeatable>

指定扫描, 为空时扫描全部

Returns:
Type
void
Example
import Bluetooth from 'miot/Bluetooth'
     Bluetooth.startScan(3000, 'FE95','FE96')

(static) stopScan() → {void}

停止扫描蓝牙设备

Returns:
Type
void

(static) unBindDeviceforMIUI(mac)

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

Parameters:
Name Type Description
mac string