Bluetooth 蓝牙
API 调用
QN.bluetooth.open(options)
打开蓝牙设备
API 调用入参
参数名 |
类型 |
是否可选 |
默认值 |
含义 |
options |
Object |
可选 |
|
选项 |
API 响应结果
参数名 |
类型 |
是否一定返回 |
含义 |
result |
Object |
|
响应对象 |
result.code |
String |
|
错误码,成功为 QAP_SUCCESS ;失败为其他 |
result.msg |
String |
|
错误信息 |
调用示例
QN.bluetooth.open()
.then(result => {
console.log(result);
})
.catch(error => {
console.log(error);
});
QN.bluetooth.close(options)
关闭蓝牙设备
API 调用入参
参数名 |
类型 |
是否可选 |
默认值 |
含义 |
options |
Object |
可选 |
|
选项 |
API 响应结果
参数名 |
类型 |
是否一定返回 |
含义 |
result |
Object |
|
响应对象 |
result.code |
String |
|
错误码,成功为 QAP_SUCCESS ;失败为其他 |
result.msg |
String |
|
错误信息 |
调用示例
QN.bluetooth.close()
.then(result => {
console.log(result);
})
.catch(error => {
console.log(error);
});
QN.bluetooth.getState(options)
获取蓝牙状态
API 调用入参
参数名 |
类型 |
是否可选 |
默认值 |
含义 |
options |
Object |
可选 |
|
选项 |
API 响应结果
参数名 |
类型 |
是否一定返回 |
含义 |
result |
Object |
|
响应对象 |
result.code |
String |
|
错误码,成功为 QAP_SUCCESS ;失败为其他 |
result.msg |
String |
|
错误信息 |
result.data |
Object |
|
响应数据 |
result.data.available |
Boolean |
|
蓝牙是否可用 |
result.data.discovering |
Boolean |
|
蓝牙是否在搜寻设备 |
调用示例
QN.bluetooth.getState()
.then(result => {
console.log(result);
})
.catch(error => {
console.log(error);
});
QN.bluetooth.startDiscovery(options)
开始搜寻周围的蓝牙设备
API 调用入参
参数名 |
类型 |
是否可选 |
默认值 |
含义 |
options |
Object |
可选 |
|
选项 |
API 响应结果
参数名 |
类型 |
是否一定返回 |
含义 |
result |
Object |
|
响应对象 |
result.code |
String |
|
错误码,成功为 QAP_SUCCESS ;失败为其他 |
result.msg |
String |
|
错误信息 |
调用示例
QN.bluetooth.startDiscovery()
.then(result => {
console.log(result);
})
.catch(error => {
console.log(error);
});
QN.bluetooth.stopDiscovery(options)
停止搜寻周围的蓝牙设备
API 调用入参
参数名 |
类型 |
是否可选 |
默认值 |
含义 |
options |
Object |
可选 |
|
选项 |
API 响应结果
参数名 |
类型 |
是否一定返回 |
含义 |
result |
Object |
|
响应对象 |
result.code |
String |
|
错误码,成功为 QAP_SUCCESS ;失败为其他 |
result.msg |
String |
|
错误信息 |
调用示例
QN.bluetooth.stopDiscovery()
.then(result => {
console.log(result);
})
.catch(error => {
console.log(error);
});
QN.bluetooth.getDevices(options)
获取已搜寻到的蓝牙设备。该接口只能获取到当前已搜寻到的蓝牙设备,每次调用,可能返回的结果会不一样。
API 调用入参
参数名 |
类型 |
是否可选 |
默认值 |
含义 |
options |
Object |
可选 |
|
选项 |
API 响应结果
参数名 |
类型 |
是否一定返回 |
含义 |
result |
Object |
|
响应对象 |
result.code |
String |
|
错误码,成功为 QAP_SUCCESS ;失败为其他 |
result.msg |
String |
|
错误信息 |
result.data |
Object |
|
响应数据 |
result.data.devices |
Array |
|
已搜寻到的蓝牙设备对象数据 |
result.data.devices[].uuid |
String |
|
已搜寻到的蓝牙设备对象的唯一id |
result.data.devices[].name |
String |
否 |
已搜寻到的蓝牙设备对象的名称,不一定所有的蓝牙设备都会有名称 |
调用示例
QN.bluetooth.getDevices()
.then(result => {
console.log(result);
})
.catch(error => {
console.log(error);
});
QN.bluetooth.connect(options)
连接蓝牙设备
API 调用入参
参数名 |
类型 |
是否可选 |
默认值 |
含义 |
options |
Object |
|
|
选项 |
options.query |
Object |
|
|
调用参数 |
options.query.uuid |
String |
|
|
要连接的蓝牙设备的唯一id |
API 响应结果
参数名 |
类型 |
是否一定返回 |
含义 |
result |
Object |
|
响应对象 |
result.code |
String |
|
错误码,成功为 QAP_SUCCESS ;失败为其他 |
result.msg |
String |
|
错误信息 |
调用示例
QN.bluetooth.connect({
query: {
uuid: '4AB701BD-5651-433B-B23E-3E4FE41C7B3F'
}
})
.then(result => {
console.log(result);
})
.catch(error => {
console.log(error);
});
QN.bluetooth.disconnect(options)
断开与蓝牙设备的连接
API 调用入参
参数名 |
类型 |
是否可选 |
默认值 |
含义 |
options |
Object |
可选 |
|
选项 |
API 响应结果
参数名 |
类型 |
是否一定返回 |
含义 |
result |
Object |
|
响应对象 |
result.code |
String |
|
错误码,成功为 QAP_SUCCESS ;失败为其他 |
result.msg |
String |
|
错误信息 |
调用示例
QN.bluetooth.disconnect()
.then(result => {
console.log(result);
})
.catch(error => {
console.log(error);
});
QN.bluetooth.getServices(options)
获取当前连接的蓝牙设备的 service (服务),该接口只能返回当前已经获取到的服务,每次调用,可能返回的结果会不一样。
API 调用入参
参数名 |
类型 |
是否可选 |
默认值 |
含义 |
options |
Object |
可选 |
|
选项 |
API 响应结果
参数名 |
类型 |
是否一定返回 |
含义 |
result |
Object |
|
响应对象 |
result.code |
String |
|
错误码,成功为 QAP_SUCCESS ;失败为其他 |
result.msg |
String |
|
错误信息 |
result.data |
Object |
|
响应数据 |
result.data.services |
Array |
|
蓝牙设备的服务对象数组 |
result.data.services[].uuid |
String |
|
服务的唯一id |
result.data.services[].isPrimary |
Boolean |
|
是否为主服务 |
调用示例
QN.bluetooth.getServices()
.then(result => {
console.log(result);
})
.catch(error => {
console.log(error);
});
QN.bluetooth.getCharacteristics(options)
获取当前连接的蓝牙设备的 characteristic (特征值),该接口只能返回当前已经获取到的特征值,每次调用,可能返回的结果会不一样。
API 调用入参
参数名 |
类型 |
是否可选 |
默认值 |
含义 |
options |
Object |
可选 |
|
选项 |
API 响应结果
参数名 |
类型 |
是否一定返回 |
含义 |
result |
Object |
|
响应对象 |
result.code |
String |
|
错误码,成功为 QAP_SUCCESS ;失败为其他 |
result.msg |
String |
|
错误信息 |
result.data |
Object |
|
响应数据 |
result.data.characteristics |
Array |
|
蓝牙设备的服务对象数组 |
result.data.characteristics[].uuid |
String |
|
特征值的唯一id |
result.data.characteristics[].readable |
Boolean |
|
特征值是否可读 |
result.data.characteristics[].writeable |
Boolean |
|
特征值是否可写 |
result.data.characteristics[].notifiable |
Boolean |
|
特征值是否可以进行通知 |
调用示例
QN.bluetooth.getCharacteristics()
.then(result => {
console.log(result);
})
.catch(error => {
console.log(error);
});
QN.bluetooth.readCharacteristic(options)
读取蓝牙设备指定特征的数据
API 调用入参
参数名 |
类型 |
是否可选 |
默认值 |
含义 |
options |
Object |
|
|
选项 |
options.query |
Object |
|
|
调用参数 |
options.query.characteristic |
Object |
|
|
特征值对象 |
options.query.characteristic.uuid |
String |
|
|
特征值的唯一id |
API 响应结果
参数名 |
类型 |
是否一定返回 |
含义 |
result |
Object |
|
响应对象 |
result.code |
String |
|
错误码,成功为 QAP_SUCCESS ;失败为其他 |
result.msg |
String |
|
错误信息 |
result.data |
Object |
|
响应数据 |
result.data.value |
String |
|
读取到的特征值数据 |
调用示例
QN.bluetooth.readCharacteristic({
query: {
characteristic: {
uuid: '2A29'
}
}
})
.then(result => {
console.log(result);
})
.catch(error => {
console.log(error);
});
QN.bluetooth.writeCharacteristic(options)
向蓝牙设备写入指定特征的数据
API 调用入参
参数名 |
类型 |
是否可选 |
默认值 |
含义 |
options |
Object |
|
|
选项 |
options.query |
Object |
|
|
调用参数 |
options.query.characteristic |
Object |
|
|
特征值对象 |
options.query.characteristic.uuid |
String |
|
|
特征值的唯一id |
options.query.value |
String |
|
|
向特征值写入的数据 |
API 响应结果
参数名 |
类型 |
是否一定返回 |
含义 |
result |
Object |
|
响应对象 |
result.code |
String |
|
错误码,成功为 QAP_SUCCESS ;失败为其他 |
result.msg |
String |
|
错误信息 |
调用示例
QN.bluetooth.writeCharacteristic({
query: {
characteristic: {
uuid: 'AF0BADB1-5B99-43CD-917A-A77BC549E3CC'
}
value: 'Hello bluetooth'
}
})
.then(result => {
console.log(result);
})
.catch(error => {
console.log(error);
});
QN.bluetooth.setCharacteristicNotify(options)
向蓝牙设备写入指定特征的数据
API 调用入参
参数名 |
类型 |
是否可选 |
默认值 |
含义 |
options |
Object |
|
|
选项 |
options.query |
Object |
|
|
调用参数 |
options.query.characteristic |
Object |
|
|
特征值对象 |
options.query.characteristic.uuid |
String |
|
|
特征值的唯一id |
options.query.enable |
Boolean |
|
|
是否开启 |
API 响应结果
参数名 |
类型 |
是否一定返回 |
含义 |
result |
Object |
|
响应对象 |
result.code |
String |
|
错误码,成功为 QAP_SUCCESS ;失败为其他 |
result.msg |
String |
|
错误信息 |
调用示例
QN.bluetooth.setCharacteristicNotify({
query: {
characteristic: {
uuid: 'AF0BADB1-5B99-43CD-917A-A77BC549E3CC'
},
enable: true,
}
})
.then(result => {
console.log(result);
})
.catch(error => {
console.log(error);
});
事件
事件名称 |
含义 |
备注 |
Global.BluetoothStateChange |
蓝牙设备状态发生变更 |
|
Global.BluetoothConnectionChange |
蓝牙设备连接状态发生变更 |
|
App.BluetoothDevicesFound |
搜寻蓝牙设备时,发现新的蓝牙设备 |
|
App.BluetoothCharacteristicFound |
连接蓝牙设备后,发现新的特征值 |
|
App.BluetoothCharacteristicChange |
蓝牙特征值对应的数据发生变化 |
需先调用 QN.bluetooth.setCharacteristicNotify 开启通知功能后,才会收到通知 |
FAQ
关于此文档暂时还没有FAQ