init: function (n) {
this.list = [];
this.serviceId = "6E400001-B5A3-F393-E0A9-E50E24DCCA9E";
this.serviceId_2 = "00001803-0000-1000-8000-00805F9B34FB";
this.serviceId_3 = "00001814-0000-1000-8000-00805F9B34FB";
this.serviceId_4 = "00001802-0000-1000-8000-00805F9B34FB";
this.serviceId_5 = "00001804-0000-1000-8000-00805F9B34FB";
this.serviceId_6 = "00001535-1212-EFDE-1523-785FEABCD123";
this.characterId_write = "6E400042-B5A3-F393-E0A9-E50E24DCCA9E";
this.characterId_read = "6E400012-B5A3-F393-E0A9-E50E24DCCA9E";
this.connectDeviceIndex = 0;
this.isGettingConnected = false;
this.isDiscovering = false;
this.isConnecting = false;
this.connectedDevice = {};
console.log('init state', this.connectedDevice.state);
if (!this.connectedDevice.state || n == 200) {
this.connectedDevice.state = false;
this.connectedDevice.deviceId = '';
this.adapterHasInit = false
}
this.startConnect();
}
startConnect: function () {
var that = this;
if (that.connectedDevice.state) return;
that.connectedDevice.deviceId = "";
that.connectedDevice.state = false;
// 如果适配器已经初始化不在调用初始化(重复初始化会报错)
if (this.adapterHasInit == undefined || this.adapterHasInit) return;
wx.showLoading({
title: '初始化蓝牙',
duration: 2000
});
// 开启蓝牙适配器状态监听
this.listenAdapterStateChange();
// 初始化蓝牙适配器状态(必须步骤,否则无法进行后续的任何操作)
wx.openBluetoothAdapter({
success: function (res) {
console.log("初始化蓝牙适配器成功");
that.getBluetoothAdapterState();
that.adapterHasInit = true;
},
fail: function (err) {
console.log(err);
wx.showLoading({
title: '请开蓝牙',
icon: 'loading',
duration: 2000
})
}
});
}
getBluetoothAdapterState: function () {
var that = this;
wx.getBluetoothAdapterState({
success: function (res) {
console.log(res);
var available = res.available;
that.isDiscovering = res.discovering;
if (!available) {
wx.showLoading({
title: '请开蓝牙',
icon: 'loading',
duration: 2000
})
} else {
if (!that.connectedDevice['state']) {
that.judegIfDiscovering(res.discovering);
}
}
},
fail: function (err) {
console.log(err);
}
})
}
judegIfDiscovering: function (discovering) {
var that = this;
if (this.isConnectinng) return;
wx.getConnectedBluetoothDevices({
services: [that.serviceId],
success: function (res) {
console.log("获取处于连接状态的设备", res);
var devices = res['devices'];
if (devices[0]) {
if (that.isAndroidPlatform) {
wx.showToast({
title: '蓝牙连接成功',
icon: 'success',
duration: 2000
});
} else {
that.getConnectedBluetoothDevices(256);
}
} else {
if (discovering) {
wx.showLoading({
title: '蓝牙搜索中'
})
} else {
if (that.isAndroidPlatform) {
that.startBluetoothDevicesDiscovery();
} else {
that.getConnectedBluetoothDevices(267);
}
}
}
},
fail: function (err) {
console.log('getConnectedBluetoothDevices err 264', err);
if (that.isAndroidPlatform) {
that.startBluetoothDevicesDiscovery();
} else {
that.getConnectedBluetoothDevices(277);
}
}
});
}
startBluetoothDevicesDiscovery: function () {
var that = this;
if (!this.isAndroidPlatform) return;
if (!this.connectedDevice['state']) {
wx.getBluetoothAdapterState({
success: function (res) {
console.log(res);
var available = res.available;
that.isDiscovering = res.discovering;
if (!available) {
wx.showLoading({
title: '请开蓝牙',
icon: 'loading',
duration: 2000
})
} else {
if (res.discovering) {
wx.showLoading({
title: '蓝牙搜索中'
})
} else {
wx.startBluetoothDevicesDiscovery({
services: [],
allowDuplicatesKey: true,
success: function (res) {
that.onBluetoothDeviceFound();
wx.showLoading({
title: '蓝牙搜索中'
})
},
fail: function (err) {
if (err.isDiscovering) {
wx.showLoading({
title: '蓝牙搜索中'
})
} else {
that.startDiscoveryTimer = setTimeout(function () {
if (!that.connectedDevice.state) {
that.startBluetoothDevicesDiscovery();
}
}, 5000)
}
}
});
}
}
},
fail: function (err) {
console.log(err);
}
})
}
[mw_shl_code=javascript,true]onBluetoothDeviceFound: function () {
var that = this;
wx.onBluetoothDeviceFound(function (res) {
console.log('new device list has founded');
if (res.devices[0]) {
var name = res.devices[0]['name'];
if (name.indexOf('FeiZhi') != -1) {
var deviceId = res.devices[0]['deviceId'];
console.log(deviceId);
that.deviceId = deviceId;
if (!that.isConnecting) {
that.startConnectDevices();
}
}
}
})
}
startConnectDevices: function (ltype, array) {
var that = this;
clearTimeout(this.getConnectedTimer);
clearTimeout(this.startDiscoveryTimer);
this.getConnectedTimer = null;
this.startDiscoveryTimer = null;
this.isConnectinng = true;
wx.showLoading({
title: '正在连接'
});
that.stopBluetoothDevicesDiscovery();
wx.createBLEConnection({
deviceId: that.deviceId,
success: function (res) {
console.log('连接成功', res);
wx.showLoading({
title: '正在连接'
});
that.connectedDevice.state = true;
that.connectedDevice.deviceId = that.deviceId;
if (res.errCode == 0) {
setTimeout(function () {
that.getService(that.deviceId);
}, 5000)
}
wx.onBLEConnectionStateChange(function (res) {
console.log('连接变化', res);
that.connectedDevice.state = res.connected;
that.connectedDevice.deviceId = res.deviceId;
if (!res.connected) {
that.init('200');
}
});
},
fail: function (err) {
console.log('连接失败:', err);
wx.hideLoading();
if (ltype == 'loop') {
array = array.splice(0, 1);
console.log(array);
that.loopConnect(array);
} else {
if (that.isAndroidPlatform) {
that.startBluetoothDevicesDiscovery();
} else {
that.getConnectedBluetoothDevices(488);
}
}
},
complete: function () {
that.isConnectinng = false;
}
});
}
getConnectedBluetoothDevices: function (n) {
var that = this;
that.isGettingConnected = true;
wx.showLoading({
title: '蓝牙搜索中'
});
wx.getConnectedBluetoothDevices({
services: [that.serviceId],
success: function (res) {
console.log("获取处于连接状态的设备", res);
var devices = res['devices'],
flag = false,
index = 0,
conDevList = [];
devices.forEach(function (value, index, array) {
if (value['name'].indexOf('FeiZhi') != -1) {
// 如果存在包含FeiZhi字段的设备
flag = true;
index += 1;
conDevList.push(value['deviceId']);
that.deviceId = value['deviceId'];
}
});
if (flag) {
that.connectDeviceIndex = 0;
that.loopConnect(conDevList);
} else {
that.failToGetConnected();
}
},
fail: function (err) {
that.failToGetConnected();
},
complete: function () {
that.isGettingConnected = false;
}
});
}
failToGetConnected: function () {
var that = this;
if (!that.getConnectedTimer) {
clearTimeout(that.getConnectedTimer);
that.getConnectedTimer = null;
}
that.getConnectedTimer = setTimeout(function () {
wx.getBluetoothAdapterState({
success: function (res) {
console.log(res);
var available = res.available;
if (!available) {
wx.showLoading({
title: '请开蓝牙',
icon: 'loading',
duration: 2000
})
} else {
if (!that.connectedDevice['state']) {
that.getConnectedBluetoothDevices();
}
}
},
fail: function (err) {
console.log(err);
}
})
}, 5000);
}
loopConnect: function (array) {
var that = this;
var listLen = array.length;
if (array[0]) {
that.deviceId = array[0];
if (!that.isConnecting) {
that.startConnectDevices('loop', array);
}
} else {
console.log('已配对的设备小程序蓝牙连接失败');
if (!that.isAndroidPlatform) {
that.getConnectedBluetoothDevices(431);
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有