2019年12月26日
一.获得系统信息
1.wx.getSystemInfo(object) 异步
image.png
image.png
2.wx.getSystemInfoSync 同步获取系统信息
try {
const 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.getNetworkType(object)
image.png
image.png
三.加速度计 (使用场景 计步)
1.wx.onAccelerometerChange(callback)监听加速度数据 5次/秒
image.png
image.png
2.wx.startAccelerometer(object) 开始监听
image.png
3.wx.stopAccelerometer(object) 停止监听
四.罗盘 (指南针)
wx.onCompassChange(callback) 监听罗盘数据, 5次/秒
image.png
五.拨打电话
wx.makePhoneCall(object)
image.png
六.扫码
wx.scanCode(object) 调出客户端扫码界面
image.png
七.剪切板
1.设置剪贴板 wx.setClipboardData(object)
2.获取剪贴板 wx.getClipboardData(object)
image.png
八.蓝牙
image.png
1.初始化和关闭 小程序被销毁也会关闭蓝牙生效周期
wx.openBluetoothAdapter(object)
wx.closeBluetoothAdapter(object)
2.监听蓝牙状态和获取蓝牙状态
2.1wx.onBluetoothAdapterStateChange(callback)
2.2wx.getBluetoothAdapterState(callback)
image.png
3.蓝牙设备
3.1搜索附件蓝牙外围设备
wx.startBluetoothDevicesDiscovery(object) 比较耗资源,连接设备后,在调用wx.stopBluetoothDevicesDiscovery(object)
image.png
// 以微信硬件平台的蓝牙智能灯为例,主服务的 UUID 是 FEE7。传入这个参数,只搜索主服务 UUID 为 FEE7 的设备
wx.startBluetoothDevicesDiscovery({
services: ['FEE7'],
success (res) {
console.log(res)
}
})
wx.stopBluetoothDevicesDiscovery({
success (res) {
console.log(res)
}
})
3.2获取蓝牙设备
3.2.1获取生效期间所有已发现的设备(包括已经和本机连接的设备)
wx.getBluetoothDevices(object)
// 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))
}
}
})
3.2.2根据uuid获取处于已连接状态的设备
wx.getConnectedBluetoothDevices(oject)
wx.getConnectedBluetoothDevices({
success (res) {
console.log(res)
}
})
3.2.3监听寻找新设备
wx.onBluetoothDeviceFound(callback)
// 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(res) {
var devices = res.devices;
console.log('new device list has founded')
console.dir(devices)
console.log(ab2hex(devices[0].advertisData))
})
4.低功耗的蓝牙设备
4.1连接低功耗的蓝牙设备
wx.createBLEConnection(object)
wx.createBLEConnection({
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId,
success (res) {
console.log(res)
}
})
4.2关闭与低功耗蓝牙设备的连接
wx.closeBLEConnection(object)
wx.closeBLEConnection({
deviceId,
success (res) {
console.log(res)
}
})
4.3启用低功耗蓝牙设备特征值变化
wx.notifyBLECharacteristicValueChange(object)
wx.notifyBLECharacteristicValueChange({
state: true, // 启用 notify 功能
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId,
// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
serviceId,
// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
characteristicId,
success (res) {
console.log('notifyBLECharacteristicValueChange success', res.errMsg)
}
})
4.4.监听低功耗蓝牙连接状态的改变事件
wx.onBLEConnectionStateChange()
wx.onBLEConnectionStateChange(function(res) {
// 该方法回调中可以用于处理连接意外断开等异常情况
console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`)
})
4.5.监听低功耗蓝牙设备的特征值变化
wx.onBLECharacteristicValueChange()
// ArrayBuffer转16进制字符串示例
function ab2hex(buffer) {
let 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(ab2hex(res.value))
})
4.6低功率数据读写
写
// 向蓝牙设备发送一个0x00的16进制数据
let buffer = new ArrayBuffer(1)
let dataView = new DataView(buffer)
dataView.setUint8(0, 0)
wx.writeBLECharacteristicValue({
// 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
deviceId,
// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
serviceId,
// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
characteristicId,
// 这里的value是ArrayBuffer类型
value: buffer,
success (res) {
console.log('writeBLECharacteristicValue success', res.errMsg)
}
})
读
// 必须在这里的回调才能获取
wx.onBLECharacteristicValueChange(function(characteristic) {
console.log('characteristic value comed:', characteristic)
})
wx.readBLECharacteristicValue({
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId,
// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
serviceId,
// 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
characteristicId,
success (res) {
console.log('readBLECharacteristicValue:', res.errCode)
}
})
5.获取蓝牙设备服务和某个服务的特征值
5.1获取蓝牙设备服务
wx.getBLEDeviceServices(object)
wx.getBLEDeviceServices({
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId,
success (res) {
console.log('device services:', res.services)
}
})
5.2某个服务的特征值
wx.getBLEDeviceCharacteristics(object)
wx.getBLEDeviceCharacteristics({
// 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
deviceId,
// 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
serviceId,
success (res) {
console.log('device getBLEDeviceCharacteristics:', res.characteristics)
}
})
九.屏幕亮度
wx.setScreenBrightness(object)
image.png
十.用户截屏事件
wx.onUserCaptureScreen(callback)
wx.onUserCaptureScreen(function (res) {
console.log('用户截屏了')
})
十一.振动
wx.vibrateLong(objcect) 400ms
wx.vibrateShort(object) 15ms
onLoad: function (options) {
// 页面初始化 options为页面跳转所带来的参数
wx.vibrateLong({
success: res => {
console.log(res)
console.log('颤抖')
},
fail: err => {
console.log('我就问你为什么不抖动了')
}
})
},
十二.手机联系人
wx.addPhoneContact(object)
image.png













网友评论