跳到主要内容

设备

系统信息

getClientInfo

调用 qt.getClientInfo 获取设备信息

qt.getClientInfo();

返回说明

参数类型说明
typestring设备类型:android->安卓客户端,iOS->iPhone客户端,pc->PC客户端,'not QT client'->非轻推客户端
versionstring客户端版本

getJsApiVersion

调用 qt.getJsApiVersion 获取JSApi版本号

qt.getJsApiVersion();

返回说明

参数类型说明
jsApiVersionstringjsapi版本号

getLanguageSetting

调用 qt.getLanguageSetting 获取设备语言

qt.getLanguageSetting();

返回说明

参数类型说明
languagestringzh中文、en英文

网络

getNetworkType

调用 qt.getNetworkType 获取网络信息

qt.getNetworkType({
succes: function (res) {
var networkType = res.networkType; //设备网络类型,WIFI、2G、3G、4G;
},
});

返回说明

参数类型说明
networkTypestring设备网络类型,WIFI、2G、3G、4G

ajax

调用qt.ajax由轻推客户端进行代发网络请求,用于解决跨域问题。仅在手机端支持,页面如果同时在PC和手机使用,请自行判断

qt.ajax({
url: "http://xxx", // 必须是完整的url地址
header: {}, // 需要附加的请求头,选填
type: "get" || "post", // 请求方式
data: {}, // 请求参数
success: function (res) {
var statusCode = res.statusCode; // 状态码, 请求的状态码例如200,404,500等
var result = res.result; // 返回结果,会自动被序列化为对象
},
});

参数说明

参数类型默认值必填说明
urlstring需要附加的请求头
headerjson蓝牙特征值对应服务的 uuid
typestring请求方式,get、post
datajson请求参数

返回说明

参数类型说明
statusCodestring状态码, 请求的状态码例如200,404,500等
resultjson返回结果

Wi-Fi

getWifiList

调用 qt.getWifiList 获取wifi列表

qt.getWifiList();

返回说明

参数类型说明
wifiListarraywifi 列表
 ssidstringwifi 的 ssid
 bssidstringwifi 的 bssid

蓝牙

蓝牙适配器模块生效周期为调用 qt.openBluetoothAdapter 至调用 qt.closeBluetoothAdapter 或页面被销毁为止。​

​在蓝牙适配器模块生效期间,开发者才能够正常调用蓝牙相关的小程序 API,并收到蓝牙模块相关的事件回调。​

​由于系统限制,Android 上获取到的 deviceId 为设备 MAC 地址,iOS 上则为设备 uuid。因此 deviceId 不能硬编码到代码中。

错误码列表

错误码说明备注
0ok正常
10000not init未初始化蓝牙适配器
10001not available当前蓝牙适配器不可用
10002no device没有找到指定设备
10003connection fail连接失败
10004no service没有找到指定服务
10005no characteristic没有找到指定特征值
10006no connection当前连接已断开
10007property not support当前特征值不支持此操作
10008system error其余所有系统上报的异常
10009system not supportAndroid 系统特有,系统版本低于 4.3 不支持BLE
10010unauthorized用户没有授权

openBluetoothAdapter

调用 qt.openBluetoothAdapter 初始化蓝牙模块

qt.openBluetoothAdapter();

closeBluetoothAdapter

调用 qt.closeBluetoothAdapter 关闭蓝牙模块

qt.closeBluetoothAdapter();

startBluetoothDevicesDiscovery

调用 qt.startBluetoothDevicesDiscovery 开始搜寻附近的蓝牙外围设备。注意,该操作比较耗费系统资源,请在搜索并连接到设备后调用 stop 方法停止搜索。

// 以硬件平台的蓝牙智能灯为例,主服务的 UUID 是 FEE7。传入这个参数,只搜索主服务 UUID 为 FEE7 的设备
qt.startBluetoothDevicesDiscovery({
services: ["FEE7"],
});

stopBluetoothDevicesDiscovery

调用 qt.stopBluetoothDevicesDiscovery 停止搜寻附近的蓝牙外围设备。若已经找到需要的蓝牙设备并不需要继续搜索时,建议调用该接口停止蓝牙搜索。

qt.stopBluetoothDevicesDiscovery();

getBluetoothDevices

调用qt.getBluetoothDevices获取在蓝牙模块生效期间所有已发现的蓝牙设备,包括已经和本机处于连接状态的设备。

qt.getBluetoothDevices();

返回说明

参数类型说明
devicesarrayuuid 对应的的已连接设备列表
 namestring蓝牙设备名称,某些设备可能没有
 localNamestring当前蓝牙设备的广播数据段中的LocalName数据段
 deviceIdstring用于区分设备的 id
 statenumber连接状态, 0未连接、1连接中、2已连接、3 断开中
  • tip: Mac系统可能无法获取advertisData及RSSI,请使用真机调试
  • tip: 开发者工具和 Android 上获取到的deviceId为设备 MAC 地址,iOS 上则为设备 uuid。因此deviceId不能硬编码到代码中
  • tip: 注意该接口获取到的设备列表为蓝牙模块生效期间所有搜索到的蓝牙设备,若在蓝牙模块使用流程结束后未及时调用 wx.closeBluetoothAdapter 释放资源,会存在调用该接口会返回之前的蓝牙使用流程中搜索到的蓝牙设备,可能设备已经不在用户身边,无法连接。
  • tip: 蓝牙设备在被搜索到时,系统返回的 name 字段一般为广播包中的LocalName字段中的设备名称,而如果与蓝牙设备建立连接,系统返回的 name 字段会改为从蓝牙设备上获取到的GattName。若需要动态改变设备名称并展示,建议使用localName字段。

低功耗蓝牙

iOS 上对特征值的 read、write、notify操作,由于系统需要获取特征值实例,传入的 serviceId 与 characteristicId 必须由 qt.getBLEDeviceServices 与 qt.getBLEDeviceCharacteristics 中获取到后才能使用。建议双平台统一在建立连接后先执行 qt.getBLEDeviceServices 与 qt.getBLEDeviceCharacteristics 后再进行与蓝牙设备的数据交互

参数说明

参数类型默认值必填说明
deviceIdstring蓝牙设备 id,参考 getDevices 接口
serviceIdstring蓝牙特征值对应服务的 uuid
characteristicIdstring蓝牙特征值的 uuid

createBLEConnection

调用 qt.createBLEConnection 连接低功耗蓝牙设备

qt.createBLEConnection({
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId: deviceId,
});

closeBLEConnection

调用qt.closeBLEConnection断开低功耗蓝牙设备

qt.closeBLEConnection({
deviceId: deviceId,
});

getBLEDeviceServices

调用 qt.getBLEDeviceServices 获取蓝牙设备所有 service(服务)

qt.getBLEDeviceServices({
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId: deviceId,
success: function (res) {
console.log("device services:", res.services);
},
});

返回说明

参数类型说明
devicesarray蓝牙设备service(服务)信息
 serviceIdstring蓝牙设备服务的 uuid
 isPrimaryboolean该服务是否为主服务
 characteristicsarray设备特征值列表
  characteristicIdstring蓝牙设备特征值的 uuid
  readboolean该特征值是否支持 read 操作
  writeboolean该特征值是否支持 write 操作
  notifyboolean该特征值是否支持 notify 操作
  indicateboolean该特征值是否支持 indicate 操作

Bug & Tip

  1. tip:iOS平台上后续对特征值的read、write、notify,由于系统需要获取特征值实例,传入的 serviceId 与 characteristicId 必须由 getBLEDeviceServices 中获取到后才能使用。建议双平台统一在建立链接后先执行 getBLEDeviceServices 与蓝牙设备的数据交互

readBLECharacteristicValue

调用 qt.readBLECharacteristicValue 读取低功耗蓝牙设备的特征值的二进制数据值。注意:必须设备的特征值支持read才可以成功调用,具体参照 characteristic 的 properties 属性

qt.readBLECharacteristicValue({
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 [**new**]
deviceId: deviceId,
// 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
serviceId: serviceId,
// 这里的 characteristicId 需要在上面的 getBLEDeviceServices 接口中获取
characteristicId: characteristicId,
success: function (res) {
console.log("readBLECharacteristicValue:", res.errCode);
},
});

返回说明

参数类型说明
deviceIdstring蓝牙设备的 id
serviceIdstring蓝牙设备服务的 uuid
characteristicIdstring蓝牙设备特征值的 uuid
valuearrayBuffer特征最新的值

writeBLECharacteristicValue

调用 qt.writeBLECharacteristicValue 向低功耗蓝牙设备特征值中写入二进制数据。注意:必须设备的特征值支持write才可以成功调用,具体参照 characteristic 的 properties 属性

tips: 并行调用多次读写接口存在读写失败的可能性

qt.writeBLECharacteristicValue({
// 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
deviceId: deviceId,
// 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
serviceId: serviceId,
// 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取
characteristicId: characteristicId,
// 这里的value是指令原文或蓝牙设备特征值对应的二进制值
value: string | ArrayBuffer,
});

Bug & Tip

  1. tip: 并行调用多次读写接口存在读写失败的可能性。
  2. tip: 接口不会对写入数据包大小做限制,但系统与蓝牙设备会确定蓝牙4.0单次传输的数据大小,超过最大字节数后会发生写入错误,建议每次写入不超过20字节。
  3. tip: 安卓平台上,在调用notify成功后立即调用write接口,在部分机型上会发生 10008 系统错误
  4. bug: 若单次写入数据过长,iOS平台上存在系统不会有任何回调的情况(包括错误回调)。

常亮

keepScreen

调用 qt.keepScreen 设置屏幕是否常亮

qt.keepScreen({
on: true, // 常亮
});

参数说明

参数类型默认值必填说明
onboolean是否常亮

屏幕亮度

只支持页面级的亮度调节,退出页面或者跳转其他新页面屏幕亮度自动恢复默认亮度。

setBrightness

调用qt.setBrightnes设置安卓屏幕亮度

qt.setBrightness({
brightness: number, // 亮度值,范围0-1
});

参数说明

参数类型默认值必填说明
brightnessnumber屏幕亮度值。取值范围:0 ~ 1。其中,0 最暗、1 最亮。

getBrightness

调用qt.getBrightness获取安卓屏幕亮度

qt.getBrightness();

返回说明

参数类型必填说明
brightnessnumber屏幕亮度值。取值范围:0 ~ 1。其中,0 最暗、1 最亮。

红外

hasIrEmitter

调用 qt.hasIrEmitter 判断安卓设备是否支持红外功能

qt.hasIrEmitter();

返回说明

参数类型必填说明
resultboolean是否支持

getCarrierFrequencies

调用 qt.getCarrierFrequencies 获取支持的红外信号频率

qt.getCarrierFrequencies(): {
"carrierFrequencies": [{
"minFrequency": number
"maxFrequency": number
}]
}

infraredTransmit

调用 qt.infraredTransmit 发送数据

qt.infraredTransmit({
"carrierFrequency": number
"userCodeH": string // 十六进制
"userCodeL": string // 十六进制
"keyCode": string // 十六进制
})