123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
-
- var plugin = requirePlugin("myPlugin")
- //var plugin = require("../../plugins/index.js");
- const app = getApp();
-
- function inArray(arr, key, val) {
- for (let i = 0; i < arr.length; i++) {
- if (arr[i][key] === val) {
- return i;
- }
- }
- return -1;
- }
-
-
-
- Page({
- data: {
- list: [],
- devices: [], //
- dev1: "已发现",
- dev2: "台设备",
- data: {},
- dainji: "绑定设备",
- shebei: "",
- headTip: '下拉刷新',
- indx: "0",
- // 显示隐藏
- showView: true,
-
- deviceInfo: ({
- macAddress: "",
- name: "",
- advertisData: "",
- })
-
-
- },
- onLoad: function() {
-
- var that = this;
- // that.setData({
- // list: [],
- // devices: [],
- // });
-
- // 获取设备列表
- // that.openBluetoothAdapter();
- // console.log("openBluetoothAdapter");
- },
-
- // 点击切换显示隐藏
-
-
- onShow: function() {
- var that = this;
-
-
- that.setData({
- list: [],
- devices: [],
- });
-
- // that.refreshInterface();
-
- that.openBluetoothAdapter();
-
- setTimeout(() => {
- var deviceInfo = wx.getStorageSync('deviceInfo')
- console.log(deviceInfo);
- if (deviceInfo == "1" || deviceInfo == null) {
- that.setData({
- dainji: "绑定设备",
- });
- } else {
- // if (deviceInfo.macAddress.length == 9) {
- // that.setData({
- // dainji: "绑定设备",
- // });
- // }else{
- // that.setData({
- // dainji: "绑定设备",
-
- // dev1: "已发现:",
- // dev2: "台设备,已连接",
- // shebei:wx.getStorageSync('deviceInfo').macAddress
- // });
- // }
- }
- }, 1000)
-
- },
-
- onHide: function() {
- var that = this;
- that.stopBluetoothDevicesDiscovery();
-
- },
-
-
-
-
-
-
- onPullDownRefresh: function() {
- console.log('下拉刷新')
- var that = this;
- that.setData({
- list: [],
- devices: [],
- });
-
- that.openBluetoothAdapter();
- wx.showNavigationBarLoading() //在标题栏中显示加载
-
- setTimeout(() => {
- wx.hideNavigationBarLoading() //完成停止加载
- wx.stopPullDownRefresh() //停止下拉刷新
- }, 3000)
-
- },
-
-
-
- // 官方广播称开始
-
- openBluetoothAdapter: function() {
- wx.openBluetoothAdapter({
- success: (res) => {
- console.log('openBluetoothAdapter success', res)
- this.startBluetoothDevicesDiscovery()
- this.setData({ // 我们不一样
- shebei: ",请稍后",
- dev1: "已发现",
- dev2: "台设备",
- })
- },
- fail: (res) => {
-
- if (res.errCode === 10001) {
- wx.onBluetoothAdapterStateChange(function(res) {
- console.log('onBluetoothAdapterStateChange', res)
- if (res.available) {
- this.startBluetoothDevicesDiscovery()
- }
- })
- }
- this.setData({ // 我们不一样
- shebei: "请开启手机蓝牙",
- dev1: "",
- dev2: "",
- })
- }
- })
- },
- getBluetoothAdapterState() {
- wx.getBluetoothAdapterState({
- success: (res) => {
- // console.log('getBluetoothAdapterState', res)
- if (res.discovering) {
- this.onBluetoothDeviceFound()
- } else if (res.available) {
- this.startBluetoothDevicesDiscovery()
- }
- }
- })
- },
- startBluetoothDevicesDiscovery() {
-
- wx.startBluetoothDevicesDiscovery({
- // services: [AICARE_SERVICE_UUID, aicare_service_uuid_android],
- allowDuplicatesKey: true,
- success: (res) => {
- console.log('startBluetoothDevicesDiscovery success', res)
- this.onBluetoothDeviceFound()
- },
- })
- },
-
- onBluetoothDeviceFound() {
-
- console.log("reresresresresress");
- wx.onBluetoothDeviceFound((res) => {
- console.log(res);
- res.devices.forEach(device => {
- if (device.advertisServiceUUIDs == null || device.advertisServiceUUIDs.length == 0) {
- var data15 = plugin.getBbroadcastData(device.advertisData);
- if (data15 != null) {
- console.log("广播秤");
- device.advertisData = data15.address;
- device.name = "el-deviceinfo";
- const foundDevices = this.data.devices
- const idx = inArray(foundDevices, 'deviceId', device.deviceId)
- const data = {}
- if (idx === -1) {
- data[`devices[${foundDevices.length}]`] = device
- } else {
- data[`devices[${idx}]`] = device
- }
- console.log(device);
- this.setData(data)
- }
- } else {
- console.log("不是我们的秤");
- }
- })
- })
-
-
- },
-
- stopBluetoothDevicesDiscovery() {
- wx.stopBluetoothDevicesDiscovery(
- console.log("蓝牙搜索已关闭")
- )
- },
- closeBluetoothAdapter() {
- wx.closeBluetoothAdapter()
- console.log("蓝牙模块已关闭")
- },
-
-
-
-
- //点击绑定设备处理
- bindViewTap: function(e) {
- var that = this;
- console.log('点击事件--绑定对应的秤');
-
- wx.closeBLEConnection({
- deviceId: wx.getStorageSync('deviceInfo').macAddress,
- success(res) {
- console.log(res)
- }
- })
-
- that.stopBluetoothDevicesDiscovery(); // 关闭蓝牙搜索
- console.log(e.currentTarget.dataset.title);
- console.log(e.currentTarget.dataset.name);
- console.log(e.currentTarget.dataset.macaddress);
-
- if (e.currentTarget.dataset.name != "el-deviceinfo") {
-
- console.log('点击连接--连接秤');
-
- wx.showLoading({
- title: '正在连接....',
- })
- // 初适化蓝牙模块
- wx.openBluetoothAdapter({
- success: function(res) {},
- fail: function() {
- wx.showToast({
- title: '启动蓝牙失败,请开启蓝牙',
- icon: 'noine',
- duration: 1500,
- })
- }
- })
-
- wx.createBLEConnection({
- deviceId: e.currentTarget.dataset.title,
- success: function(res) {
- console.log('连接成功');
- console.log(res);
- wx.hideLoading()
- wx.showToast({
- title: '绑定成功',
- icon: 'success',
- duration: 1000,
- })
-
- that.setData({ // 我们不一样
- deviceInfo: ({
- macAddress: e.currentTarget.dataset.title,
- name: e.currentTarget.dataset.name,
- advertisData: e.currentTarget.dataset.advertisData,
- macdidname: e.currentTarget.dataset.macdidname
- })
- })
-
- wx.setStorageSync("deviceInfo", that.data.deviceInfo);
- console.log("新绑定设备信息:")
- console.log(wx.getStorageSync('deviceInfo'))
-
- wx.switchTab({
- url: '../index/index'
- })
-
- },
- fail: function(res) {
- console.log('连接秤连接失败');
- console.log(res);
- wx.hideLoading()
- console.log(res);
- wx.showToast({
- title: '连接失败,再试试',
- icon: 'none',
- duration: 2000,
- })
- }
- })
- } else {
- // 广播秤BM15 连接
- // 初适化蓝牙模块
- wx.showLoading({
- title: '正在绑定广播....',
- })
-
- wx.closeBluetoothAdapter({
- success: function(res) {
- console.log(res)
- wx.hideLoading()
- wx.showToast({
- title: '广播秤绑定成功',
- icon: 'success',
- duration: 1000,
- })
-
- that.setData({ // 我们不一样
- deviceInfo: ({
- macAddress: e.currentTarget.dataset.title,
- name: e.currentTarget.dataset.name,
- advertisData: e.currentTarget.dataset.advertisData,
- macdidname: e.currentTarget.dataset.macdidname,
- })
- })
-
- wx.setStorageSync("deviceInfo", that.data.deviceInfo);
- wx.setStorageSync("devMacAddress", e.currentTarget.dataset.macaddress);
- console.log("新绑定设备信息:" + e.currentTarget.dataset.macaddress);
-
- // wx.switchTab({
- // url: '../index/index'
- // })
- wx.navigateTo({
- url: '../index/index'
- })
- }
- })
-
- }
-
-
-
-
-
-
-
- // 跳转回主界面
- // wx.switchTab({
- // url: '../measure/measure'
- // })
-
- },
-
-
-
-
- })
|