123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- // const plugin = requirePlugin("myPlugin").AiFresh;
-
- const {AiFresh: plugin} = requirePlugin("sdkPlugin");
-
- function inArray(arr, key, val) {
- for (let i = 0; i < arr.length; i++) {
- if (arr[i][key] === val) {
- return i;
- }
- }
- return -1;
- }
-
- // 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(',');
- }
-
- Page({
- data: {
- devices: [
- // {
- // deviceId: "02:03:04:05:06:07",
- // name: "elink",
- // localName: "elink",
- // mac: "02:03:04:05:06:07",
- // RSSI: -69,
- // advertisServiceUUIDs:[
- // "FFE0","FFC0"
- // ],
- // analyzeDataText:"0102003241123413"
- // }
- ],
- mac: "",
- connected: false,
- text: "",
- unitList_all: ['g', 'ml', 'lb:oz', 'oz', 'kg', '斤', '牛奶 ml', '水 ml', '牛奶 floz', '水 floz', 'lb'],
- unitList: ['g', 'ml', 'lb:oz', 'oz'],
- weight: 0,
- unit: 'g',
- unitIndex: 0,
- min: 0,
- second: 0,
- },
- onLoad: function () {
-
- },
- clearDevices() {
- this.setData({
- devices: []
- })
- this.closeBluetoothAdapter()
- wx.nextTick(()=>{
- this.openBluetoothAdapter()
- })
- },
- tare() {
- console.log("去皮")
- plugin.cmdTare()
- },
- getUnits() {
- console.log("获取设备支持的单位")
- plugin.cmdGetAllUnits()
- },
- countUp() {
- console.log("正计时开始")
- plugin.cmdTimeCountUp()
- },
- countUpPause() {
- console.log("正计时暂停")
- plugin.cmdTimeCountUpPause(this.data.min, this.data.second)
- },
- countDown() {
- console.log("倒计时开始")
- if (this.data.min || this.data.second) {
- plugin.cmdTimeCountDown(this.data.min, this.data.second)
- } else {
- plugin.cmdTimeCountDown(5, 30)
- }
- },
- countDownPause() {
- console.log("倒计时暂停")
- plugin.cmdTimeCountDownPause(this.data.min, this.data.second)
- },
- resetTime() {
- console.log("重置时间")
- plugin.cmdTimeReset()
- this.setData({
- min: 0,
- second: 0,
- })
- },
- selectUnit(e) {
- let unit = e.currentTarget.dataset.unit
- let index = e.currentTarget.dataset.index
- let idx = this.data.unitList_all.indexOf(unit)
- plugin.cmdSwitchUnit(idx)
- },
- openBluetoothAdapter() {
- wx.openBluetoothAdapter({
- success: (res) => {
- console.log('openBluetoothAdapter success', res)
- this.startBluetoothDevicesDiscovery()
- },
- fail: (res) => {
- if (res.errCode === 10001) {
- wx.showToast({
- title: '请打开蓝牙',
- icon:"none"
- })
- wx.onBluetoothAdapterStateChange(function (res) {
- console.log('onBluetoothAdapterStateChange', res)
- if (res.available) {
- this.startBluetoothDevicesDiscovery()
- }
- })
- }
- }
- })
- },
- getBluetoothAdapterState() {
- wx.getBluetoothAdapterState({
- success: (res) => {
- console.log('getBluetoothAdapterState', res)
- if (res.discovering) {
- this.onBluetoothDeviceFound()
- } else if (res.available) {
- this.startBluetoothDevicesDiscovery()
- }
- }
- })
- },
- startBluetoothDevicesDiscovery() {
- if (this._discoveryStarted) {
- return
- }
- this._discoveryStarted = true
- wx.startBluetoothDevicesDiscovery({
- allowDuplicatesKey: true,
- services: [
- "FFB0",
- ],
- success: (res) => {
- console.log('startBluetoothDevicesDiscovery success', res)
- this.onBluetoothDeviceFound()
- },
- })
- },
- stopBluetoothDevicesDiscovery() {
- wx.stopBluetoothDevicesDiscovery()
- },
- onBluetoothDeviceFound() {
- wx.onBluetoothDeviceFound((res) => {
- res.devices.forEach(device => {
- const foundDevices = this.data.devices
- const idx = inArray(foundDevices, 'deviceId', device.deviceId)
- const data = {}
- console.log(device)
- // console.log(ab2hex(device.advertisData))
- if (idx === -1) {
- data[`devices[${foundDevices.length}]`] = device
- } else {
- data[`devices[${idx}]`] = device
- }
- this.setData(data)
- })
- })
- },
- createBLEConnection(e) {
- const ds = e.currentTarget.dataset
- const deviceId = ds.deviceId
- const name = ds.name
- const index = ds.index
- this._device = this.data.devices[index]
- console.log(this._device)
- wx.createBLEConnection({
- deviceId,
- success: (res) => {
- let buff = this._device.advertisData.slice(-6)
- let arr = new Uint8Array(buff).reverse()
- let mac = ab2hex(arr).replace(/,/g,":")
- this.setData({
- connected: true,
- name,
- deviceId,
- mac: mac,
- })
- console.log("createBLEConnection:success")
- wx.stopBluetoothDevicesDiscovery()
- this.getBLEDeviceServices(deviceId)
- }
- })
- this.stopBluetoothDevicesDiscovery()
- },
- closeBLEConnection() {
- wx.closeBLEConnection({
- deviceId: this._deviceId
- })
- this.setData({
- connected: false,
- min: 0,
- second: 0,
- weight: 0,
- unitList: ['g', 'ml', 'lb:oz', 'oz'],
- })
- },
- getBLEDeviceServices(deviceId) {
- wx.getBLEDeviceServices({
- deviceId,
- success: (res) => {
- console.log('getBLEDeviceServices:', res)
- for (let i = 0; i < res.services.length; i++) {
- if (res.services[i].isPrimary && res.services[i].uuid.indexOf('0000FFB0') != -1) {
- this.getBLEDeviceCharacteristics(deviceId, res.services[i].uuid)
- return
- }
- }
- }
- })
- },
- getBLEDeviceCharacteristics(deviceId, serviceId) {
- this._deviceId = deviceId
- this._serviceId = serviceId
- this._device.serviceId = serviceId
-
- wx.getBLEDeviceCharacteristics({
- deviceId,
- serviceId,
- success: (res) => {
- console.log('getBLEDeviceCharacteristics success', res.characteristics)
- for (let i = 0; i < res.characteristics.length; i++) {
- let item = res.characteristics[i]
- if (item.uuid.indexOf('0000FFB1') != -1) {
- this.uuid1 = item.uuid //下发数据
- } else if (item.uuid.indexOf('0000FFB2') != -1) {
- this.uuid2 = item.uuid //监听数据
- }
- }
- // 打开监听
- wx.notifyBLECharacteristicValueChange({
- deviceId,
- serviceId,
- characteristicId: this.uuid2,
- state: true,
- })
-
- plugin.initPlugin(res.characteristics, this._device)
- plugin.cmdGetAllUnits()
- wx.onBLECharacteristicValueChange((characteristic) => {
- let bleData = plugin.parseBleData(characteristic.value)
- if (bleData.status == 1) {
- this.setData({
- text: bleData.data.text,
- weight: bleData.data.weight,
- unit: bleData.data.unit,
- unitIndex: bleData.data.unitIndex
- })
- } else if (bleData.status == 2) {
- this.setData({
- min: bleData.data.min,
- second: bleData.data.second,
- })
- } else if (bleData.status == 3) {//设备支持的单位
- // 使用返回的单位索引,从本地的所有单位列表中取出支持的单位名
- let unitList = bleData.data.unitIndexs.map(i=>{
- return this.data.unitList_all[i]
- })
- this.setData({
- // unitList: bleData.data.unitList,
- unitList,
- })
- } else {
- console.log(bleData)
- }
- })
- },
- fail(res) {
- console.error('getBLEDeviceCharacteristics', res)
- }
- })
- },
- writeBLECharacteristicValue(buffer, uuid, deviceId, serviceId) {
- // 向蓝牙设备发送一个二进制流数据
- wx.writeBLECharacteristicValue({
- deviceId,
- serviceId,
- characteristicId: uuid,
- value: buffer,
- success(res) {
- console.log('writeBLECharacteristicValue success', res)
- console.log(plugin.ab2hex(buffer))
- }
- })
- },
- closeBluetoothAdapter() {
- wx.closeBluetoothAdapter()
- this._discoveryStarted = false
- },
- });
|