您现在的位置是:首页 > 文章详情

小程序中设备相关的api总结

日期:2018-09-05点击:558

系统信息

wx.getSystemInfo(OBJECT)

获取系统信息

OBJECT参数说明:

参数名 类型 必填 说明
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success回调参数说明:

参数 说明 最低版本
brand 手机品牌 1.5.0
model 手机型号
pixelRatio 设备像素比
screenWidth 屏幕宽度 1.1.0
screenHeight 屏幕高度 1.1.0
windowWidth 可使用窗口宽度
windowHeight 可使用窗口高度
statusBarHeight 状态栏的高度 1.9.0
language 微信设置的语言
version 微信版本号
system 操作系统版本
platform 客户端平台
fontSizeSetting 用户字体大小设置。以“我-设置-通用-字体大小”中的设置为准,单位:px 1.5.0
SDKVersion 客户端基础库版本 1.1.0
wx.getSystemInfo({ success: function(res) { console.log(res.model) console.log(res.pixelRatio) console.log(res.windowWidth) console.log(res.windowHeight) console.log(res.language) console.log(res.version) console.log(res.platform) } })

wx.getSystemInfoSync()

获取系统信息同步接口

同步返回参数说明:

参数 说明 最低版本
brand 手机品牌 1.5.0
model 手机型号
pixelRatio 设备像素比
screenWidth 屏幕宽度 1.1.0
screenHeight 屏幕高度 1.1.0
windowWidth 可使用窗口宽度
windowHeight 可使用窗口高度
statusBarHeight 状态栏的高度 1.9.0
language 微信设置的语言
version 微信版本号
system 操作系统版本
platform 客户端平台
fontSizeSetting 用户字体大小设置。以“我-设置-通用-字体大小”中的设置为准,单位:px 1.5.0
SDKVersion 客户端基础库版本 1.1.0
try { var res = wx.getSystemInfoSync() console.log(res.model) console.log(res.pixelRatio) console.log(res.windowWidth) console.log(res.windowHeight) console.log(res.language) console.log(res.version) console.log(res.platform) } catch (e) { // Do something when catch error }

wx.canIUse(String)

判断小程序的API,回调,参数,组件等是否在当前版本可用。注意:此接口从基础库 1.1.1 版本开始支持

String参数说明: 使用${API}.${method}.${param}.${options}或者${component}.${attribute}.${option}方式来调用,例如:

${API} 代表 API 名字

${method} 代表调用方式,有效值为return, success, object, callback

${param} 代表参数或者返回值

${options} 代表参数的可选值

${component} 代表组件名字

${attribute} 代表组件属性

${option} 代表组件属性的可选值

wx.canIUse('openBluetoothAdapter') wx.canIUse('getSystemInfoSync.return.screenWidth') wx.canIUse('getSystemInfo.success.screenWidth') wx.canIUse('showToast.object.image') wx.canIUse('onCompassChange.callback.direction') wx.canIUse('request.object.method.GET') wx.canIUse('live-player') wx.canIUse('text.selectable') wx.canIUse('button.open-type.contact')

内存

wx.onMemoryWarning(callback):监听内存不足的告警事件,Android下有告警等级划分,只有LOW和CRITICAL会回调开发者;iOS无等级划分。基础库 2.0.2 开始支持,低版本需做兼容处理

CALLBACK返回参数:

参数名 类型 说明
level Number 仅Android有该字段,对应系统内存告警等级宏定义

Android下告警等级对应系统宏:

TRIM_MEMORY_RUNNING_MODERATE = 5 TRIM_MEMORY_RUNNING_LOW = 10 TRIM_MEMORY_RUNNING_CRITICAL = 15

示例代码:

wx.onMemoryWarning(function () { console.log('onMemoryWarningReceive') })

网络状态

wx.getNetworkType(OBJECT)

获取网络类型

参数名 类型 必填 说明
success Function 接口调用成功,返回网络类型 networkType
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数名 说明
networkType 网络类型
wx.getNetworkType({ success: function(res) { // 返回网络类型, 有效值: // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络) var networkType = res.networkType } })

wx.onNetworkStatusChange(CALLBACK)

监听网络状态变化。基础库 1.1.0 开始支持,低版本需做兼容处理

callback返回参数

参数名 类型 说明
isConnected Boolean 当前是否有网络连接
networkType String 网络类型

networkType 有效值:

说明
wifi wifi 网络
2g 2g 网络
3g 3g 网络
4g 4g 网络
none 无网络
unknown Android下不常见的网络类型
wx.onNetworkStatusChange(function(res) { console.log(res.isConnected) console.log(res.networkType) })

加速度计

wx.onAccelerometerChange(CALLBACK)

监听加速度数据,频率:5次/秒,接口调用后会自动开始监听,可使用 wx.stopAccelerometer 停止监听

callback返回参数

参数名 类型 说明
x Number X 轴
y Number Y 轴
z Number Z 轴
wx.onAccelerometerChange(function(res) { console.log(res.x) console.log(res.y) console.log(res.z) })

wx.startAccelerometer(OBJECT)

开始监听加速度数据。基础库 1.1.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数名 类型 必填 说明 最低版本
interval String 监听加速度数据回调函数的执行频率 2.1.0
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

interval 的合法值

根据机型性能、当前 CPU 与内存的占用情况,interval 的设置与实际 wx.onAccelerometerChange() 回调函数的执行频率会有一些出入

说明
game 适用于更新游戏的回调频率,在 20ms/次 左右
ui 适用于更新 UI 的回调频率,在 60ms/次 左右
normal 普通的回调频率,在 200ms/次 左右
wx.startAccelerometer({ interval: 'game' })

wx.stopAccelerometer(OBJECT)

停止监听加速度数据。基础库 1.1.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数名 类型 必填 说明 最低版本
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)
wx.stopAccelerometer()

罗盘

wx.onCompassChange(CALLBACK)

监听罗盘数据,频率:5次/秒,接口调用后会自动开始监听,可使用wx.stopCompass停止监听

callback返回参数

参数名 类型 说明
direction Number 面对的方向度数
wx.onCompassChange(function (res) { console.log(res.direction) })

wx.startCompass(OBJECT)

开始监听罗盘数据。基础库 1.1.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数名 类型 必填 说明 最低版本
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)
wx.startCompass()

wx.stopCompass(OBJECT)

停止监听罗盘数据。基础库 1.1.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数名 类型 必填 说明 最低版本
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)
wx.stopCompass()

拨打电话

wx.makePhoneCall(OBJECT)

OBJECT参数说明:

参数名 类型 必填 说明
phoneNumber String 需要拨打的电话号码
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)
wx.makePhoneCall({ phoneNumber: '1340000' //仅为示例,并非真实的电话号码 })

扫码

wx.scanCode(OBJECT):调起客户端扫码界面,扫码成功后返回对应的结果

OBJECT参数说明:

参数名 类型 必填 说明
onlyFromCamera Boolean 是否只能从相机扫码,不允许从相册选择图片
scanType Array 扫码类型,参数类型是数组,二维码是'qrCode',一维码是'barCode',DataMatrix是‘datamatrix’,pdf417是‘pdf417’
success Function 接口调用成功的回调函数,返回内容详见返回参数说明
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数 说明
result 所扫码的内容
scanType 所扫码的类型
charSet 所扫码的字符集
path 当所扫的码为当前小程序的合法二维码时,会返回此字段,内容为二维码携带的 path
rawData 原始数据,base64编码
// 允许从相机和相册扫码 wx.scanCode({ success: (res) => { console.log(res) } }) // 只允许从相机扫码 wx.scanCode({ onlyFromCamera: true, success: (res) => { console.log(res) } })

剪贴板

wx.setClipboardData(OBJECT)

设置系统剪贴板的内容。基础库 1.1.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数 类型 必填 说明
data String 需要设置的内容
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)
wx.setClipboardData({ data: 'data', success: function(res) { wx.getClipboardData({ success: function(res) { console.log(res.data) // data } }) } })

wx.getClipboardData(OBJECT)

获取系统剪贴板内容。基础库 1.1.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数 类型 必填 说明
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数 类型 说明
data String 剪贴板的内容
wx.getClipboardData({ success: function(res){ console.log(res.data) } })

蓝牙适配器接口

目前不支持在开发者工具上进行调试,需要使用真机才能正常调用小程序蓝牙接口。iOS 微信客户端 6.5.6 版本开始支持,Android 6.5.7 版本开始支持

wx.openBluetoothAdapter(OBJECT)

初始化小程序蓝牙模块,生效周期为调用wx.openBluetoothAdapter至调用wx.closeBluetoothAdapter或小程序被销毁为止。 在小程序蓝牙适配器模块生效期间,开发者可以正常调用下面的小程序API,并会收到蓝牙模块相关的on回调

OBJECT参数说明:

参数 类型 必填 说明
success Function 成功则返回成功初始化信息
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)
wx.openBluetoothAdapter({ success: function (res) { console.log(res) } })

Bug & Tip

tip: 在没有调用wx.openBluetoothAdapter的情况下调用小程序其它蓝牙模块相关API,API会返回错误,错误码为10000

bug: 在用户蓝牙开关未开启或者手机不支持蓝牙功能的情况下,调用wx.openBluetoothAdapter会返回错误,错误码为10001,表示手机蓝牙功能不可用;此时小程序蓝牙模块已经初始化完成,可通过wx.onBluetoothAdapterStateChange监听手机蓝牙状态的改变,也可以调用蓝牙模块的所有API。

wx.closeBluetoothAdapter(OBJECT)

关闭蓝牙模块,使其进入未初始化状态。调用该方法将断开所有已建立的链接并释放系统资源。建议在使用小程序蓝牙流程后调用,与wx.openBluetoothAdapter成对调用。基础库 1.1.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数 类型 必填 说明
success Function 成功则返回成功初始化信息
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)
wx.closeBluetoothAdapter({ success: function (res) { console.log(res) } })

wx.getBluetoothAdapterState(OBJECT)

获取本机蓝牙适配器状态。基础库 1.1.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数 类型 必填 说明
success Function 成功则返回本机蓝牙适配器状态
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数 类型 说明
discovering Boolean 是否正在搜索设备
available Boolean 蓝牙适配器是否可用
errMsg String 成功:ok,错误:详细信息
wx.getBluetoothAdapterState({ success: function (res) { console.log(res) } })

wx.onBluetoothAdapterStateChange(CALLBACK)

监听蓝牙适配器状态变化事件。基础库 1.1.0 开始支持,低版本需做兼容处理

CALLBACK参数说明:

参数 类型 说明
available Boolean 蓝牙适配器是否可用
discovering Boolean 蓝牙适配器是否处于搜索状态
wx.onBluetoothAdapterStateChange(function(res) { console.log(`adapterState changed, now is`, res) })

wx.startBluetoothDevicesDiscovery(OBJECT)

开始搜寻附近的蓝牙外围设备。注意,该操作比较耗费系统资源,请在搜索并连接到设备后调用 stop 方法停止搜索。基础库 1.1.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数 类型 必填 说明
services Array 蓝牙设备主 service 的 uuid 列表
allowDuplicatesKey Boolean 是否允许重复上报同一设备, 如果允许重复上报,则onDeviceFound 方法会多次上报同一设备,但是 RSSI 值会有不同
interval Number 上报设备的间隔,默认为0,意思是找到新设备立即上报,否则根据传入的间隔上报
success Function 成功则返回本机蓝牙适配器状态
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

services参数说明:某些蓝牙设备会广播自己的主 service 的 uuid。如果这里传入该数组,那么根据该 uuid 列表,只搜索发出广播包有这个主服务的蓝牙设备,建议主要通过该参数过滤掉周边不需要处理的其他蓝牙设备

success返回参数说明:

参数 类型 说明
errMsg String 成功:ok,错误:详细信息
// 以微信硬件平台的蓝牙智能灯为例,主服务的 UUID 是 FEE7。传入这个参数,只搜索主服务 UUID 为 FEE7 的设备 wx.startBluetoothDevicesDiscovery({ services: ['FEE7'], success: function (res) { console.log(res) } })

wx.stopBluetoothDevicesDiscovery(OBJECT)

停止搜寻附近的蓝牙外围设备。若已经找到需要的蓝牙设备并不需要继续搜索时,建议调用该接口停止蓝牙搜索。基础库 1.1.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数 类型 必填 说明
success Function 成功则返回本机蓝牙适配器状态
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数 类型 说明
errMsg String 成功:ok,错误:详细信息
wx.stopBluetoothDevicesDiscovery({ success: function (res) { console.log(res) } })

wx.getBluetoothDevices(OBJECT)

获取在小程序蓝牙模块生效期间所有已发现的蓝牙设备,包括已经和本机处于连接状态的设备。基础库 1.1.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数 类型 必填 说明
success Function 成功则返回本机蓝牙适配器状态
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数 类型 说明
devices Array uuid 对应的的已连接设备列表
errMsg String 成功:ok,错误:详细信息

device 对象

蓝牙设备信息

参数 类型 说明
name String 蓝牙设备名称,某些设备可能没有
deviceId String 用于区分设备的 id
RSSI Number 当前蓝牙设备的信号强度
advertisData ArrayBuffer 当前蓝牙设备的广播数据段中的ManufacturerData数据段 (注意:vConsole 无法打印出 ArrayBuffer 类型数据)
advertisServiceUUIDs Array 当前蓝牙设备的广播数据段中的ServiceUUIDs数据段
localName String 当前蓝牙设备的广播数据段中的LocalName数据段
serviceData ArrayBuffer 当前蓝牙设备的广播数据段中的ServiceData数据段
// ArrayBuffer转16进度字符串示例 function ab2hex(buffer) { var hexArr = Array.prototype.map.call( new Uint8Array(buffer), function(bit) { return ('00' + bit.toString(16)).slice(-2) } ) return hexArr.join(''); } wx.getBluetoothDevices({ success: function (res) { console.log(res) if (res.devices[0]) { console.log(ab2hex(res.devices[0].advertisData)) } } })

Tip

1.Mac系统可能无法获取advertisData及RSSI,请使用真机调试

2.开发者工具和 Android 上获取到的deviceId为设备 MAC 地址,iOS 上则为设备 uuid。因此deviceId不能硬编码到代码中

3.注意该接口获取到的设备列表为小程序蓝牙模块生效期间所有搜索到的蓝牙设备,若在蓝牙模块使用流程结束后未及时调用 wx.closeBluetoothAdapter 释放资源,会存在调用该接口会返回之前的蓝牙使用流程中搜索到的蓝牙设备,可能设备已经不在用户身边,无法连接。

4.蓝牙设备在被搜索到时,系统返回的 name 字段一般为广播包中的LocalName字段中的设备名称,而如果与蓝牙设备建立连接,系统返回的 name 字段会改为从蓝牙设备上获取到的GattName。若需要动态改变设备名称并展示,建议使用localName字段。

wx.onBluetoothDeviceFound(CALLBACK)

监听寻找到新设备的事件。基础库 1.1.0 开始支持,低版本需做兼容处理

CALLBACK参数说明:

参数 类型 说明
devices Array 新搜索到的设备列表

device 对象:蓝牙设备信息

参数 类型 说明
name String 蓝牙设备名称,某些设备可能没有
deviceId String 用于区分设备的 id
RSSI Number 当前蓝牙设备的信号强度
advertisData ArrayBuffer 当前蓝牙设备的广播数据段中的ManufacturerData数据段 (注意:vConsole 无法打印出 ArrayBuffer 类型数据)
advertisServiceUUIDs Array 当前蓝牙设备的广播数据段中的ServiceUUIDs数据段
localName String 当前蓝牙设备的广播数据段中的LocalName数据段
serviceData ArrayBuffer 当前蓝牙设备的广播数据段中的ServiceData数据段
// ArrayBuffer转16进度字符串示例 function ab2hex(buffer) { var hexArr = Array.prototype.map.call( new Uint8Array(buffer), function(bit) { return ('00' + bit.toString(16)).slice(-2) } ) return hexArr.join(''); } wx.onBluetoothDeviceFound(function(devices) { console.log('new device list has founded') console.dir(devices) console.log(ab2hex(devices[0].advertisData)) })

Tip

  1. Mac系统可能无法获取advertisData及RSSI,请使用真机调试

2.开发者工具和 Android 上获取到的deviceId为设备 MAC 地址,iOS 上则为设备 uuid。因此deviceId不能硬编码到代码中

3.若在onBluetoothDeviceFound回调了某个设备,则此设备会添加到 wx.getBluetoothDevices 接口获取到的数组中

wx.getConnectedBluetoothDevices(OBJECT)

根据 uuid 获取处于已连接状态的设备。基础库 1.1.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数 类型 必填 说明
services Array 蓝牙设备主 service 的 uuid 列表
success Function 成功则返回本机蓝牙适配器状态
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数 类型 说明
devices Array 搜索到的设备列表
errMsg String 成功:ok,错误:详细信息

device对象:蓝牙设备信息

参数 类型 说明
name String 蓝牙设备名称,某些设备可能没有
deviceId String 用于区分设备的 id
wx.getConnectedBluetoothDevices({ success: function (res) { console.log(res) } })

tip: 开发者工具和 Android 上获取到的deviceId为设备 MAC 地址,iOS 上则为设备 uuid。因此deviceId不能硬编码到代码中

低功耗蓝牙接口

wx.createBLEConnection(OBJECT)

连接低功耗蓝牙设备。基础库 1.1.0 开始支持,低版本需做兼容处理

若小程序在之前已有搜索过某个蓝牙设备,并成功建立链接,可直接传入之前搜索获取的deviceId直接尝试连接该设备,无需进行搜索操作

OBJECT参数说明:

参数 类型 必填 说明
deviceId String 蓝牙设备 id,参考 getDevices 接口
timeout Number 超时时间,单位ms,不填表示不会超时
success Function 成功则返回本机蓝牙适配器状态
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数:

参数 类型 说明
errMsg String 成功:ok,错误:详细信息
wx.createBLEConnection({ // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId: deviceId, success: function (res) { console.log(res) } })

tip:

1.安卓手机上如果多次调用create创建连接,有可能导致系统持有同一设备多个连接的实例,导致调用close的时候并不能真正的断开与设备的连接。因此请保证尽量成对的调用create和close接口

2.蓝牙链接随时可能断开,建议监听 wx.onBLEConnectionStateChange 回调事件,当蓝牙设备断开时按需执行重连操作

3.若对未连接的设备或已断开连接的设备调用数据读写操作的接口,会返回10006错误,详见错误码,建议进行重连操作

wx.closeBLEConnection(OBJECT)

断开与低功耗蓝牙设备的连接。基础库 1.1.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数 类型 必填 说明
deviceId String 蓝牙设备 id,参考 getDevices 接口
success Function 成功则返回本机蓝牙适配器状态
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数:

参数 类型 说明
errMsg String 成功:ok,错误:详细信息
wx.closeBLEConnection({ deviceId:deviceId, success: function (res) { console.log(res) } })

wx.onBLEConnectionStateChange(CALLBACK)

监听低功耗蓝牙连接状态的改变事件,包括开发者主动连接或断开连接,设备丢失,连接异常断开等等。基础库 1.1.1 开始支持,低版本需做兼容处理

CALLBACK参数说明:

参数 类型 说明
deviceId String 蓝牙设备 id,参考 device 对象
connected Boolean 连接目前的状态
wx.onBLEConnectionStateChange(function(res) { // 该方法回调中可以用于处理连接意外断开等异常情况 console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`) })

wx.getBLEDeviceServices(OBJECT)

获取蓝牙设备所有 service(服务)。基础库 1.1.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数 类型 必填 说明
deviceId String 蓝牙设备 id,参考 getDevices 接口
success Function 成功则返回本机蓝牙适配器状态
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数:

参数 类型 说明
services Array 设备服务列表
errMsg String 成功:ok,错误:详细信息

service对象:蓝牙设备service(服务)信息

参数 类型 说明
uuid String 蓝牙设备服务的 uuid
isPrimary Boolean 该服务是否为主服务
wx.getBLEDeviceServices({ // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId: deviceId, success: function (res) { console.log('device services:', res.services) } })

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

wx.getBLEDeviceCharacteristics(OBJECT)

获取蓝牙设备某个服务中的所有 characteristic(特征值)。基础库 1.1.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数 类型 必填 说明
deviceId String 蓝牙设备 id,参考 device 对象
serviceId String 蓝牙服务 uuid
success Function 成功则返回本机蓝牙适配器状态
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数:

参数 类型 说明
characteristics Array 设备特征值列表
errMsg String 成功:ok,错误:详细信息

characteristic对象:蓝牙设备characteristic(特征值)信息

参数 类型 说明
uuid String 蓝牙设备特征值的 uuid
properties Object 该特征值支持的操作类型

properties对象

参数 类型 说明
read Boolean 该特征值是否支持 read 操作
write Boolean 该特征值是否支持 write 操作
notify Boolean 该特征值是否支持 notify 操作
indicate Boolean 该特征值是否支持 indicate 操作
wx.getBLEDeviceCharacteristics({ // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId: deviceId, // 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取 serviceId: serviceId, success: function (res) { console.log('device getBLEDeviceCharacteristics:', res.characteristics) } })

tip:

1.传入的serviceId需要在getBLEDeviceServices获取到

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

wx.readBLECharacteristicValue(OBJECT)

读取低功耗蓝牙设备的特征值的二进制数据值。注意:必须设备的特征值支持read才可以成功调用,具体参照 characteristic 的 properties 属性。基础库 1.1.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数 类型 必填 说明
deviceId String 蓝牙设备 id,参考 device 对象
serviceId String 蓝牙特征值对应服务的 uuid
characteristicId String 蓝牙特征值的 uuid
success Function 成功则返回本机蓝牙适配器状态
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数:

参数 类型 说明
errCode Number 错误码
errMsg String 成功:ok,错误:详细信息
// 必须在这里的回调才能获取 wx.onBLECharacteristicValueChange(function(characteristic) { console.log('characteristic value comed:', characteristic) }) wx.readBLECharacteristicValue({ // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 [**new**] deviceId: deviceId, // 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取 serviceId: serviceId, // 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取 characteristicId: characteristicId, success: function (res) { console.log('readBLECharacteristicValue:', res.errCode) } })

tip:

1.并行调用多次读写接口存在读写失败的可能性。

2.read接口读取到的信息需要在onBLECharacteristicValueChange方法注册的回调中获取

wx.writeBLECharacteristicValue(OBJECT)

向低功耗蓝牙设备特征值中写入二进制数据。注意:必须设备的特征值支持write才可以成功调用,具体参照 characteristic 的 properties 属性。基础库 1.1.0 开始支持,低版本需做兼容处理

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

OBJECT参数说明:

参数 类型 必填 说明
deviceId String 蓝牙设备 id,参考 device 对象
serviceId String 蓝牙特征值对应服务的 uuid
characteristicId String 蓝牙特征值的 uuid
value ArrayBuffer 蓝牙设备特征值对应的二进制值
success Function 成功则返回本机蓝牙适配器状态
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数:

参数 类型 说明
errMsg String 成功:ok,错误:详细信息
// 向蓝牙设备发送一个0x00的16进制数据 let buffer = new ArrayBuffer(1) let dataView = new DataView(buffer) dataView.setUint8(0, 0) wx.writeBLECharacteristicValue({ // 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取 deviceId: deviceId, // 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取 serviceId: serviceId, // 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取 characteristicId: characteristicId, // 这里的value是ArrayBuffer类型 value: buffer, success: function (res) { console.log('writeBLECharacteristicValue success', res.errMsg) } })

Bug & Tip

1.并行调用多次读写接口存在读写失败的可能性。

2.小程序不会对写入数据包大小做限制,但系统与蓝牙设备会确定蓝牙4.0单次传输的数据大小,超过最大字节数后会发生写入错误,建议每次写入不超过20字节。

3.安卓平台上,在调用notify成功后立即调用write接口,在部分机型上会发生 10008 系统错误

4.若单次写入数据过长,iOS平台上存在系统不会有任何回调的情况(包括错误回调)

wx.notifyBLECharacteristicValueChange(OBJECT)

启用低功耗蓝牙设备特征值变化时的 notify 功能,订阅特征值。注意:必须设备的特征值支持notify或者indicate才可以成功调用,具体参照 characteristic 的 properties 属性。另外,必须先启用notify才能监听到设备 characteristicValueChange 事件。基础库 1.1.1 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数 类型 必填 说明
deviceId String 蓝牙设备 id,参考 device 对象
serviceId String 蓝牙特征值对应服务的 uuid
characteristicId String 蓝牙特征值的 uuid
state Boolean true: 启用 notify; false: 停用 notify
success Function 成功则返回本机蓝牙适配器状态
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数:

参数 类型 说明
errMsg String 成功:ok,错误:详细信息
wx.notifyBLECharacteristicValueChange({ state: true, // 启用 notify 功能 // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接 deviceId: deviceId, // 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取 serviceId: serviceId, // 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取 characteristicId: characteristicId, success: function (res) { console.log('notifyBLECharacteristicValueChange success', res.errMsg) } })

tip:

1.订阅操作成功后需要设备主动更新特征值的value,才会触发 wx.onBLECharacteristicValueChange 回调。

2.安卓平台上,在调用notify成功后立即调用write接口,在部分机型上会发生 10008 系统错误

wx.onBLECharacteristicValueChange(CALLBACK)

监听低功耗蓝牙设备的特征值变化。必须先启用notify接口才能接收到设备推送的notification。基础库 1.1.0 开始支持,低版本需做兼容处理

CALLBACK参数说明:

参数 类型 说明
deviceId String 蓝牙设备 id,参考 device 对象
serviceId String 特征值所属服务 uuid
characteristicId String 特征值 uuid
value ArrayBuffer 特征值最新的值 (注意:vConsole 无法打印出 ArrayBuffer 类型数据)
// ArrayBuffer转16进度字符串示例 function ab2hex(buffer) { var hexArr = Array.prototype.map.call( new Uint8Array(buffer), function(bit) { return ('00' + bit.toString(16)).slice(-2) } ) return hexArr.join(''); } wx.onBLECharacteristicValueChange(function(res) { console.log(`characteristic ${res.characteristicId} has changed, now is ${res.value}`) console.log(ab2hext(res.value)) })

蓝牙错误码(errCode)列表

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

iBeacon

wx.startBeaconDiscovery(OBJECT)

开始搜索附近的iBeacon设备。基础库 1.2.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数 类型 必填 说明
uuids StringArray iBeacon设备广播的 uuids
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数 类型 说明
errMsg String 调用结果
wx.startBeaconDiscovery({ success(res) { } })

wx.stopBeaconDiscovery(OBJECT)

停止搜索附近的iBeacon设备。基础库 1.2.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数 类型 必填 说明
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数 类型 说明
errMsg String 调用结果

wx.getBeacons(OBJECT)

获取所有已搜索到的iBeacon设备。基础库 1.2.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数 类型 必填 说明
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数 类型 说明
beacons ObjectArray iBeacon 设备列表
errMsg String 调用结果

iBeacon 结构:

参数 类型 说明
uuid String iBeacon 设备广播的 uuid
major String iBeacon 设备的主 id
minor String iBeacon 设备的次 id
proximity Number 表示设备距离的枚举值
accuracy Number iBeacon 设备的距离
rssi Number 表示设备的信号强度

wx.onBeaconUpdate(CALLBACK)

监听 iBeacon 设备的更新事件。基础库 1.2.0 开始支持,低版本需做兼容处理

CALLBACK返回参数说明:

参数 类型 说明
beacons array object 当前搜寻到的所有 iBeacon 设备列表

iBeacon 结构:

参数 类型 说明
uuid String iBeacon 设备广播的 uuid
major String iBeacon 设备的主 id
minor String iBeacon 设备的次 id
proximity Number 表示设备距离的枚举值
accuracy Number iBeacon 设备的距离
rssi Number 表示设备的信号强度

wx.onBeaconServiceChange(CALLBACK)

监听 iBeacon 服务的状态变化。基础库 1.2.0 开始支持,低版本需做兼容处理

CALLBACK返回参数说明:

参数 类型 说明
available Boolean 服务目前是否可用
discovering Boolean 目前是否处于搜索状态

错误码列表

错误码 说明 备注
0 ok 正常
11000 unsupport 系统或设备不支持
11001 bluetooth service unavailable 蓝牙服务不可用
11002 location service unavailable 位置服务不可用
11003 already start 已经开始搜索

屏幕亮度

wx.setScreenBrightness(OBJECT)

设置屏幕亮度。基础库 1.2.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数名 类型 必填 说明
value Number 屏幕亮度值,范围 0~1,0 最暗,1 最亮
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

wx.getScreenBrightness(OBJECT)

获取屏幕亮度。基础库 1.2.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数名 类型 必填 说明
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数名 类型 说明
value Number 屏幕亮度值,范围 0~1,0 最暗,1 最亮

wx.setKeepScreenOn(OBJECT)

设置是否保持常亮状态。仅在当前小程序生效,离开小程序后设置失效。基础库 1.4.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数名 类型 必填 说明
keepScreenOn Boolean 是否保持屏幕常亮
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数名 类型 说明
errMsg String 调用结果
// 保持屏幕常亮 wx.setKeepScreenOn({ keepScreenOn: true })

用户截屏事件

wx.onUserCaptureScreen(CALLBACK)

监听用户主动截屏事件,用户使用系统截屏按键截屏时触发此事件。基础库 1.4.0 开始支持,低版本需做兼容处理

CALLBACK返回参数:无

wx.onUserCaptureScreen(function(res) { console.log('用户截屏了') })

震动

wx.vibrateLong(OBJECT)

使手机发生较长时间的振动(400ms)。基础库 1.2.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数名 类型 必填 说明
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

wx.vibrateShort(OBJECT)

使手机发生较短时间的振动(15ms)。基础库 1.2.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数名 类型 必填 说明
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

Tip

1.vibrateShort 接口仅在 iPhone7/iPhone7Plus 及 Android 机型生效

2.getScreenBrightness 接口若安卓系统设置中开启了自动调节亮度功能,则屏幕亮度会根据光线自动调整,该接口仅能获取自动调节亮度之前的值,而非实时的亮度值

手机联系人

wx.addPhoneContact(OBJECT):调用后,用户可以选择将该表单以“新增联系人”或“添加到已有联系人”的方式,写入手机系统通讯录,完成手机通讯录联系人和联系方式的增加。基础库 1.2.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数名 类型 必填 说明
photoFilePath String 头像本地文件路径
nickName String 昵称
lastName String 姓氏
middleName String 中间名
firstName String 名字
remark String 备注
mobilePhoneNumber String 手机号
weChatNumber String 微信号
addressCountry String 联系地址国家
addressState String 联系地址省份
addressCity String 联系地址城市
addressStreet String 联系地址街道
addressPostalCode String 联系地址邮政编码
organization String 公司
title String 职位
workFaxNumber String 工作传真
workPhoneNumber String 工作电话
hostNumber String 公司电话
email String 电子邮件
url String 网站
workAddressCountry String 工作地址国家
workAddressState String 工作地址省份
workAddressCity String 工作地址城市
workAddressStreet String 工作地址街道
workAddressPostalCode String 工作地址邮政编码
homeFaxNumber String 住宅传真
homePhoneNumber String 住宅电话
homeAddressCountry String 住宅地址国家
homeAddressState String 住宅地址省份
homeAddressCity String 住宅地址城市
homeAddressStreet String 住宅地址街道
homeAddressPostalCode String 住宅地址邮政编码
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

回调结果:

回调类型 errMsg 说明
success ok 添加成功
fail fail cancel 用户取消操作
fail fail ${detail} 调用失败,detail 加上详细信息

NFC(近距离无线通讯技术)

暂仅支持 HCE(基于主机的卡模拟)模式,即将安卓手机模拟成实体智能卡。

适用机型:支持 NFC 功能,且系统版本为Android5.0及以上的手机

适用卡范围:符合ISO 14443-4标准的CPU卡

wx.getHCEState(OBJECT)

判断当前设备是否支持 HCE 能力。基础库 1.7.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数名 类型 必填 说明
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数名 类型 说明
errMsg String 错误信息
errCode Number 错误码
wx.getHCEState({ success: function(res) { console.log(res.errCode) } })

wx.startHCE(OBJECT)

初始化 NFC 模块。基础库 1.7.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数名 类型 必填 说明
aid_list Array 需要注册到系统的AID列表,每个 AID 为 String 类型
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数名 类型 说明
errMsg String 错误信息
errCode Number 错误码
wx.startHCE({ aid_list: ['F222222222'] success: function(res) { console.log(res.errMsg) } })

wx.stopHCE(OBJECT)

关闭 NFC 模块。仅在安卓系统下有效。基础库 1.7.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数名 类型 必填 说明
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数名 类型 说明
errMsg String 错误信息
errCode Number 错误码
wx.stopHCE({ success: function(res) { console.log(res.errMsg) } })

wx.onHCEMessage(CALLBACK)

监听 NFC 设备的消息回调,并在回调中处理。返回参数中 messageType 表示消息类型,目前有如下值:

1.消息为HCE Apdu Command类型,小程序需对此指令进行处理,并调用 sendHCEMessage 接口返回处理指令;

2.消息为设备离场事件

CALLBACK 返回参数说明:

参数名 类型 说明
messageType Number 消息类型
data ArrayBuffer 客户端接收到 NFC 设备的指令,此参数当且仅当 messageType=1 时有效
reason Number 此参数当且仅当 messageType=2 时有效

wx.sendHCEMessage(OBJECT)

发送 NFC 消息。仅在安卓系统下有效。基础库 1.7.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数名 类型 必填 说明
data ArrayBuffer 二进制数据
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数名 类型 说明
errMsg String 错误信息
errCode Number 错误码
const buffer = new ArrayBuffer(1) const dataView = new DataView(buffer) dataView.setUint8(0, 0) wx.startHCE({ success: function(res) { wx.onHCEMessage(function(res) { if (res.messageType === 1) { wx.sendHCEMessage({data: buffer}) } }) } })

errCode列表

每个接口调用的时候,都会返回 errCode 字段

错误码 说明
0 ok
13000 当前设备不支持 NFC
13001 当前设备支持 NFC,但系统NFC开关未开启
13002 当前设备支持 NFC,但不支持HCE
13003 AID 列表参数格式错误
13004 未设置微信为默认NFC支付应用
13005 返回的指令不合法
13006 注册 AID 失败

Wi-Fi

在小程序中支持搜索周边的 Wi-Fi,同时可以针对指定 Wi-Fi,传入密码发起连接。该系列接口为系统原生能力

连接指定 Wi-Fi 接口调用时序:

Android: startWifi —> connectWifi —> onWifiConnected

iOS(仅iOS 11及以上版本支持):startWifi —> connectWifi —> onWifiConnected

连周边 Wi-Fi 接口调用时序:

Android startWifi —> getWifiList —> onGetWifiList —> connectWifi —> onWifiConnected

iOS(iOS 11.0及11.1版本因系统原因暂不支持):startWifi —> getWifiList —> onGetWifiList —> setWifiList —> onWifiConnected

注意:

Wi-Fi 相关接口暂不可用 wx.canIUse 接口判断。

Android 6.0 以上版本,在没有打开定位开关的时候会导致设备不能正常获取周边的 Wi-Fi 信息。

wx.startWifi(OBJECT)

初始化 Wi-Fi 模块。基础库 1.6.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数 类型 必填 说明
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)
wx.startWifi({ success: function(res) { console.log(res.errMsg) } })

wx.stopWifi(OBJECT)

关闭 Wi-Fi 模块。基础库 1.6.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数 类型 必填 说明
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)
wx.stopWifi({ success: function(res) { console.log(res.errMsg) } })

wx.connectWifi(OBJECT)

连接 Wi-Fi。若已知 Wi-Fi 信息,可以直接利用该接口连接。仅 Android 与 iOS 11 以上版本支持。基础库 1.6.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数 类型 必填 说明
SSID String Wi-Fi 设备ssid
BSSID String Wi-Fi 设备bssid
password String Wi-Fi 设备密码
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)
wx.connectWifi({ SSID: '', BSSID: '', success: function(res) { console.log(res.errMsg) } })

wx.getWifiList(OBJECT)

请求获取 Wi-Fi 列表,在 onGetWifiList 注册的回调中返回 wifiList 数据。iOS 将跳转到系统的 Wi-Fi 界面,Android 不会跳转。 iOS 11.0 及 iOS 11.1 两个版本因系统问题,该方法失效。但在 iOS 11.2 中已修复。基础库 1.6.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数 类型 必填 说明
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

wx.onGetWifiList(CALLBACK)

监听在获取到 Wi-Fi 列表数据时的事件,在回调中将返回 wifiList。基础库 1.6.0 开始支持,低版本需做兼容处理

CALLBACK 返回参数说明:

参数 类型 说明
wifiList Array Wi-Fi 列表数据

Wi-Fi 列表项说明:

参数 类型 说明
SSID String Wi-Fi 的SSID
BSSID String Wi-Fi 的BSSID
secure Boolean Wi-Fi 是否安全
signalStrength Number Wi-Fi 信号强度

wx.setWifiList(OBJECT)

iOS特有接口 在 onGetWifiList 回调后,利用接口设置 wifiList 中 AP 的相关信息。基础库 1.6.0 开始支持,低版本需做兼容处理

注意:

1.该接口只能在 onGetWifiList 回调之后才能调用。

2.此时客户端会挂起,等待小程序设置 Wi-Fi 信息,请务必尽快调用该接口,若无数据请传入一个空数组。

3.有可能随着周边 Wi-Fi 列表的刷新,单个流程内收到多次带有存在重复的 Wi-Fi 列表的回调。

OBJECT参数说明:

参数 类型 必填 说明
wifiList Array 提供预设的 Wi-Fi 信息列表
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

Wi-Fi 列表项说明:

参数 类型 说明
SSID String Wi-Fi 的SSID
BSSID String Wi-Fi 的BSSID
password String Wi-Fi 设备密码
wx.onGetWifiList(function(res) { if (res.wifiList.length) { wx.setWifiList({ wifiList: [{ SSID: res.wifiList[0].SSID, BSSID: res.wifiList[0].BSSID, password: '123456' }] }) } else { wx.setWifiList({ wifiList: [] }) } }) wx.getWifiList()

wx.onWifiConnected(CALLBACK)

监听连接上 Wi-Fi 的事件。基础库 1.6.0 开始支持,低版本需做兼容处理

CALLBACK 返回参数说明:

参数 类型 说明
wifi Object Wi-Fi 信息

wifi对象说明:

参数 类型 说明
SSID String Wi-Fi 的SSID
BSSID String Wi-Fi 的BSSID
secure Boolean Wi-Fi 是否安全
signalStrength Number Wi-Fi 信号强度

wx.getConnectedWifi(OBJECT)

获取已连接中的 Wi-Fi 信息。基础库 1.6.0 开始支持,低版本需做兼容处理

OBJECT参数说明:

参数 类型 必填 说明
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

success返回参数说明:

参数 类型 说明
wifi Object Wi-Fi 信息

wifi对象说明:

参数 类型 说明
SSID String Wi-Fi 的SSID
BSSID String Wi-Fi 的BSSID
secure Boolean Wi-Fi 是否安全
signalStrength Number Wi-Fi 信号强度

errCode列表

每个接口调用的时候,都会返回 errCode 字段

错误码 说明 备注
0 ok 正常
12000 not init 未先调用startWifi接口
12001 system not support 当前系统不支持相关能力
12002 password error Wi-Fi 密码错误
12003 connection timeout 连接超时
12004 duplicate request 重复连接 Wi-Fi
12005 wifi not turned on Android特有,未打开 Wi-Fi 开关
12006 gps not turned on Android特有,未打开 GPS 定位开关
12007 user denied 用户拒绝授权链接 Wi-Fi
12008 invalid SSID 无效SSID
12009 ssystem config err 系统运营商配置拒绝连接 Wi-Fi
12010 system internal error 系统其他错误,需要在errmsg打印具体的错误原因
12011 weapp in background 应用在后台无法配置 Wi-Fi
原文链接:https://yq.aliyun.com/articles/636312
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章