美文网首页
设备应用相关API

设备应用相关API

作者: wangyu2488 | 来源:发表于2019-12-29 15:36 被阅读0次

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

相关文章

  • 设备应用相关API

    2019年12月26日 一.获得系统信息 1.wx.getSystemInfo(object) 异步 2.wx.g...

  • cordova3.X 运用grunt快速生成plugin自定义插

    Cordova提供了一组设备相关的API,通过这组API,移动应用能够以JavaScript访问原生的设备功能,如...

  • cordova的学习及使用

    什么是cordovaCordova提供了一组设备相关的API,通过这组API,移动应用能够以JavaScript访...

  • Cordova简介

    Cordova Cordova提供了一组设备相关的API,通过这组API,移动应用能够以JavaScript访问原...

  • 让iOS运用Cordova变得如此简单

    一:Cordova理论知识点 Cordova提供了一组设备相关的API,通过这组API,移动应用能够以JavaSc...

  • 0901_Cordova封装APP

    Cordova概述 Cordova的目的是通过提供了一组设备相关的API, 使得移动应用能够以JavaScript...

  • 设备信息相关的开发

    设备信息相关的开发(非私有API, 底层API) 1.设备的型号 2.设备的CPU型号\使用情况 3.设备的内存容...

  • Gradle插件

    一、插件相关API PluginAware主要定义了插件相关API。 应用插件 apply plugin: 'ja...

  • 2019软件测试:移动应用性能测试:CheckList 工具(A

    移动应用测试策略 - 设备性能 - 服务器/ API性能 - 网络性能 设备性能 应用启动 你的应用程序需要多长时...

  • 应用配置相关API

    为了方便我将相关内容简记如下图: 更多相关原图和原始文件已上传GitHub,一来方便批量下载相关脑图,二来可以方便...

网友评论

      本文标题:设备应用相关API

      本文链接:https://www.haomeiwen.com/subject/nyiyoctx.html