05 蓝牙服务 特征模块 - MiEcosystem/miot-plugin-sdk GitHub Wiki
蓝牙服务/特征操作类 蓝牙的开发,详见设备使用标准BLE 协议 本文件提供了蓝牙服务(Service)和蓝牙特征值(Characteristic)的读写监听方面的操作 蓝牙的开发简化流程为:发现设备 - 连接设备 - 发现服务 - 发现特征值 - 特征值读写 - 断开连接,本文件主要涉及到发现服务 - 发现特征值 - 特征值读写这么几步
Export: public
Doc_name: 蓝牙服务-特征模块
Doc_index: 5
Doc_directory: bluetooth
Example
import {Bluetooth} from 'miot/device/bluetooth'
...
ble = Bluetooth.createBluetoothLE(result.uuid || result.mac);//android 用 mac 创建设备,ios 用 uuid 创建设备
const charac = ble.getService('...').getCharacteristic('...')
charac.read().then(characteristic=>{characteristic.value ... }).catch(err=>{});
charac.write().then(characteristic=>{}).catch(err=>{})
...
-
miot/device/bluetooth
-
.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 :
-
.IBluetoothCharacteristic
Kind: static interface of miot/device/bluetooth
-
.IBluetoothCharacteristic
-
.isDiscovered :
boolean
-
.isValueLoaded :
boolean
-
.UUID :
string
- .value ⇒
-
.read() ⇒
[ 'Promise' ].<IBluetoothCharacteristic>
-
.write(value) ⇒
[ 'Promise' ].<IBluetoothCharacteristic>
-
.writeWithoutResponse(value) ⇒
[ 'Promise' ].<IBluetoothCharacteristic>
-
.setNotify(flag) ⇒
[ 'Promise' ].<IBluetoothCharacteristic>
-
.isDiscovered :
是否已经被发现,只有已经被发现的特征值才可以真正操作读写,如果蓝牙断开连接了,isDiscovered为false
Kind: instance property of IBluetoothCharacteristic
Read only: true
数值是否已经加载, 为 true 时,本类才能读到正确的 value。read/write/writeWithoutResponse等方法的成功调用,bluetoothCharacteristicValueChanged事件执行,都会将此属性置为true
Kind: instance property of IBluetoothCharacteristic
Read only: true
特征值的 UUID
Kind: instance property of IBluetoothCharacteristic
Read only: true
数值, 配合 isValueLoaded 使用
Kind: instance property of IBluetoothCharacteristic
Returns: hexstring
Read only: true
Example
...
if(charateristic.isValueLoaded){
const val = characteristic.value;
...
}
...
读取蓝牙数据
Kind: instance method of IBluetoothCharacteristic
Returns: [ 'Promise' ].<IBluetoothCharacteristic>
- resolve: 返回当前对象,value为读取到的value
reject:100:设备正在连接中 101:设备不存在 102:服务或者特征值未发现
写数据 对应 writeWithResponse
Kind: instance method of IBluetoothCharacteristic
Returns: [ 'Promise' ].<IBluetoothCharacteristic>
- resolve: 返回当前对象,value为成功写入的value
reject:100:设备正在连接中 102:服务或者特征值未发现
Param | Type | Description |
---|---|---|
value | hexstring |
hexstring 16进制字符串 |
直接写数据 对应 writeWithoutResponse
Kind: instance method of IBluetoothCharacteristic
Returns: [ 'Promise' ].<IBluetoothCharacteristic>
- resolve: 返回当前对象,value为成功写入的value
reject:{code: xxx, message: xxx} 100:设备正在连接中 102:服务或者特征值未发现
Param | Type | Description |
---|---|---|
value | hexstring |
16进制字符串 |
设置数值变化监听开关,如果成功监听了,可以接收到属性变化事件bluetoothCharacteristicValueChanged
Kind: instance method of IBluetoothCharacteristic
Returns: [ 'Promise' ].<IBluetoothCharacteristic>
- resolve:当前对象
reject:{code: xxx, message: xxx} 100:设备正在连接中 102:服务或者特征值未发现
Param | Type | Description |
---|---|---|
flag | boolean |
true 打开监听, false 则关闭监听 |
Example
...
import {BluetoothEvent} from 'miot/device/bluetooth'
character.setNotify(true).then(()=>{console.log("success")});
BluetoothEvent.bluetoothCharacteristicValueChanged.addListener((bluetooth, service, character, value) => {
if (character.UUID.indexOf("ffd5")>0){
console.log("bluetoothCharacteristicValueChanged", character.UUID, value);
}
})
...
Kind: static interface of miot/device/bluetooth
-
.IBluetoothService
-
.UUID :
string
-
.isDiscovered :
boolean
-
.getCharacteristic ⇒
IBluetoothCharacteristic
-
.startDiscoverCharacteristics(...characteristicUUIDs) ⇒
boolean
-
.UUID :
蓝牙服务 UUID
Kind: instance property of IBluetoothService
Read only: true
蓝牙服务是否已被发现,被发现的蓝牙服务才可以继续扫描特征值,蓝牙断开时,isDiscovered为false
Kind: instance property of IBluetoothService
Read only: true
获取蓝牙特征值,如果没有,会创建一个,然后保存到缓存中,注意新创建的并不能直接使用,需要被发现后才可真正使用
Kind: instance property of IBluetoothService
Param | Type |
---|---|
characteristicUUID | string |
发现蓝牙特征,此方法返回true or false,表示是否开始发现蓝牙特征值。发现的蓝牙特征值需要通过订阅BluetoothEvent的bluetoothCharacteristicDiscovered来使用
Kind: instance method of IBluetoothService
Param | Type | Description |
---|---|---|
...characteristicUUIDs | string |
特征的 UUID |