|
@@ -6,176 +6,86 @@ class bleManager {
|
|
that.isAvailable = false;
|
|
that.isAvailable = false;
|
|
that.hasPermission = false;
|
|
that.hasPermission = false;
|
|
that.scanDevices = [];
|
|
that.scanDevices = [];
|
|
- that.device = null;
|
|
|
|
|
|
+ that.publicDevice = null;
|
|
}
|
|
}
|
|
|
|
|
|
- // 初始化蓝牙适配器
|
|
|
|
|
|
+ /// 监控蓝牙打开状态
|
|
initBluetoothAdapter() {
|
|
initBluetoothAdapter() {
|
|
- var that = this
|
|
|
|
- wx.openBluetoothAdapter({
|
|
|
|
- success: (res) => {
|
|
|
|
- that.isAvailable = true;
|
|
|
|
- },
|
|
|
|
- fail: (err) => {
|
|
|
|
- that.isAvailable = false;
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
|
|
+ var that = this;
|
|
wx.onBluetoothAdapterStateChange(function (res) {
|
|
wx.onBluetoothAdapterStateChange(function (res) {
|
|
that.isAvailable = res.available;
|
|
that.isAvailable = res.available;
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
|
|
- // 获取蓝牙权限
|
|
|
|
- async getBluetoothPermission() {
|
|
|
|
|
|
+ ///监听搜索设备列表
|
|
|
|
+ getBluetoothDevices(callBack) {
|
|
var that = this;
|
|
var that = this;
|
|
- console.log("gadsfasdfqwwerqewrqr");
|
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
|
- wx.getSetting({
|
|
|
|
- success(res) {
|
|
|
|
- if (res.authSetting["scope.bluetooth"]) {
|
|
|
|
- that.hasPermission = true;
|
|
|
|
- getApp().globalData.scopeBluetooth = true;
|
|
|
|
- resolve(true);
|
|
|
|
- } else if (res.authSetting["scope.bluetooth"] === undefined) {
|
|
|
|
- that.hasPermission = false;
|
|
|
|
- getApp().globalData.scopeBluetooth = false;
|
|
|
|
- resolve(false);
|
|
|
|
- wx.authorize({
|
|
|
|
- scope: "scope.bluetooth",
|
|
|
|
- complete() {
|
|
|
|
- that.getBluetoothPermission()
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
- } else {
|
|
|
|
- that.hasPermission = false;
|
|
|
|
- that.globalData.scopeBluetooth = false;
|
|
|
|
- wx.showModal({
|
|
|
|
- title: '请打开系统蓝牙进行配网',
|
|
|
|
- content: '如已打开蓝牙仍然弹框,请尝试重启小程序',
|
|
|
|
- success(res) {
|
|
|
|
- if (res.confirm) {
|
|
|
|
- console.log('用户点击确定')
|
|
|
|
- wx.openSetting({
|
|
|
|
- complete() {}
|
|
|
|
- })
|
|
|
|
- } else if (res.cancel) {
|
|
|
|
- console.log('用户点击取消')
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
- resolve(false);
|
|
|
|
- };
|
|
|
|
- },
|
|
|
|
- fail(err) {
|
|
|
|
- resolve(false);
|
|
|
|
|
|
+ wx.onBluetoothDeviceFound(function (res) {
|
|
|
|
+ ///第一种情况
|
|
|
|
+ if (res.deviceId) {
|
|
|
|
+ if (res.name == getApp().globalData.connectWillDevice.clientType) {
|
|
|
|
+ res.advertisData = res.advertisData ? that.buf2hex(res.advertisData) : '';
|
|
|
|
+ callBack(res);
|
|
}
|
|
}
|
|
- });
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- ///获取定位权限
|
|
|
|
- async getLocalSetting() {
|
|
|
|
- var that = this;
|
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
|
- wx.getSetting({
|
|
|
|
- success(res) {
|
|
|
|
- if (res.authSetting["scope.userLocation"]) {
|
|
|
|
- resolve(true);
|
|
|
|
- } else if (res.authSetting["scope.userLocation"] === undefined) {
|
|
|
|
- wx.authorize({
|
|
|
|
- scope: "scope.userLocation",
|
|
|
|
- success() {
|
|
|
|
- resolve(that.getLocalSetting());
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
- } else {
|
|
|
|
- wx.showModal({
|
|
|
|
- title: '请打开系统位置获取定位权限',
|
|
|
|
- success(res) {
|
|
|
|
- if (res.confirm) {
|
|
|
|
- wx.openSetting({
|
|
|
|
- complete() {}
|
|
|
|
- })
|
|
|
|
- } else if (res.cancel) {}
|
|
|
|
- }
|
|
|
|
- })
|
|
|
|
- reject(false);
|
|
|
|
|
|
+ }
|
|
|
|
+ ///第二种情况
|
|
|
|
+ else if (res.devices) {
|
|
|
|
+ for (var i = 0; i < res.devices.length; i++) {
|
|
|
|
+ var temp = res.devices[i];
|
|
|
|
+ if (temp.name == getApp().globalData.connectWillDevice.clientType) {
|
|
|
|
+ temp.advertisData = temp.advertisData ? that.buf2hex(temp.advertisData) : '';
|
|
|
|
+ callBack(temp);
|
|
|
|
+ break;
|
|
}
|
|
}
|
|
- },
|
|
|
|
- fail(err) {
|
|
|
|
- reject(false);
|
|
|
|
}
|
|
}
|
|
- });
|
|
|
|
|
|
+ }
|
|
|
|
+ ///第三种情况
|
|
|
|
+ else if (res[0]) {
|
|
|
|
+ if (res[0].name == getApp().globalData.connectWillDevice.clientType) {
|
|
|
|
+ res[0].advertisData = res[0].advertisData ? that.buf2hex(res[0].advertisData) : '';
|
|
|
|
+ callBack(res[0]);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
- // 开始搜索蓝牙设备
|
|
|
|
- async startScan(callBack, boolean) {
|
|
|
|
- var that = this;
|
|
|
|
- if (!that.isAvailable) {
|
|
|
|
- wx.showToast({
|
|
|
|
- title: '请打开蓝牙',
|
|
|
|
- icon: 'none',
|
|
|
|
- duration: 2000
|
|
|
|
- });
|
|
|
|
- boolean(false);
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- var open = await that.getLocalSetting();
|
|
|
|
- if (!open) {
|
|
|
|
- wx.showToast({
|
|
|
|
- title: '请打开定位权限',
|
|
|
|
- icon: 'none',
|
|
|
|
- duration: 2000
|
|
|
|
- });
|
|
|
|
- boolean(false);
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
|
|
+ ///获取数据
|
|
|
|
+ buf2hex(buffer) {
|
|
|
|
+ return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
|
|
|
|
+ }
|
|
|
|
|
|
- ///没有权限再获取
|
|
|
|
- if (!that.hasPermission) {
|
|
|
|
- var per = await that.getBluetoothPermission();
|
|
|
|
- if (!per) {
|
|
|
|
- wx.showToast({
|
|
|
|
- title: '请打开蓝牙权限',
|
|
|
|
- icon: 'none',
|
|
|
|
- duration: 2000
|
|
|
|
- });
|
|
|
|
- boolean(false);
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- // services: ["ffc0", "ab00", "ffe5"],
|
|
|
|
- wx.startBluetoothDevicesDiscovery({
|
|
|
|
- allowDuplicatesKey: true,
|
|
|
|
- success: (res) => {
|
|
|
|
- if (res) {
|
|
|
|
- console.log("gadsfasdfqwerqwerdfasdfqr==000===");
|
|
|
|
- wx.onBluetoothDeviceFound((devicesRes) => {
|
|
|
|
- for (var i = 0; i < devicesRes.devices.length; i++) {
|
|
|
|
- if (devicesRes.devices[i].name != "") {
|
|
|
|
- if (devicesRes.devices[i].name === getApp().globalData.connectWillDevice.clientType) {
|
|
|
|
- console.log("gadsfasdfqwerqwerdfasdfqr=====" + devicesRes.devices[i].name);
|
|
|
|
- callBack(devicesRes.devices[i]);
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
- },
|
|
|
|
- fail: (err) => {
|
|
|
|
- if (err.errno == 1509008) {
|
|
|
|
- wx.showToast({
|
|
|
|
- title: '搜索蓝牙需要先开启定位权限',
|
|
|
|
- icon: 'none',
|
|
|
|
- duration: 2000
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
- boolean(false);
|
|
|
|
|
|
+ // 开始搜索蓝牙设备
|
|
|
|
+ startScan(boolean) {
|
|
|
|
+ wx.closeBluetoothAdapter({
|
|
|
|
+ complete: function (res) {
|
|
|
|
+ wx.openBluetoothAdapter({
|
|
|
|
+ success: function (res) {
|
|
|
|
+ wx.getBluetoothAdapterState({
|
|
|
|
+ success: function (res) {},
|
|
|
|
+ fail(err) {
|
|
|
|
+ boolean(false);
|
|
|
|
+ },
|
|
|
|
+ })
|
|
|
|
+ wx.startBluetoothDevicesDiscovery({
|
|
|
|
+ allowDuplicatesKey: false,
|
|
|
|
+ success: function (res) {
|
|
|
|
+ boolean(true);
|
|
|
|
+ },
|
|
|
|
+ fail(err) {},
|
|
|
|
+ })
|
|
|
|
+ },
|
|
|
|
+ fail: function (res) {
|
|
|
|
+ wx.showModal({
|
|
|
|
+ title: '提示',
|
|
|
|
+ content: '请检查手机蓝牙是否打开',
|
|
|
|
+ showCancel: false,
|
|
|
|
+ success: function (res) {}
|
|
|
|
+ });
|
|
|
|
+ boolean(false);
|
|
|
|
+ }
|
|
|
|
+ })
|
|
}
|
|
}
|
|
- });
|
|
|
|
|
|
+ })
|
|
}
|
|
}
|
|
|
|
|
|
// 停止搜索
|
|
// 停止搜索
|
|
@@ -194,117 +104,25 @@ class bleManager {
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
- ///获取已经连接过手机的设备
|
|
|
|
- async getAllOnlineDevices(callBack) {
|
|
|
|
- var that = this;
|
|
|
|
- if (!that.isAvailable) {
|
|
|
|
- setTimeout(() => {
|
|
|
|
- that.getAllOnlineDevices(callBack);
|
|
|
|
- }, 3 * 1000);
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (!that.hasPermission) {
|
|
|
|
- var per = await that.getBluetoothPermission();
|
|
|
|
- if (!per) {
|
|
|
|
- setTimeout(() => {
|
|
|
|
- that.getAllOnlineDevices(callBack);
|
|
|
|
- }, 3 * 1000);
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- that.getConnectedDevices();
|
|
|
|
- wx.getBluetoothDevices({
|
|
|
|
- success: (res) => {
|
|
|
|
- // && res.devices.length > 0
|
|
|
|
- if (res) {
|
|
|
|
- callBack(res.devices);
|
|
|
|
- }
|
|
|
|
- setTimeout(() => {
|
|
|
|
- that.getAllOnlineDevices(callBack);
|
|
|
|
- }, 3 * 1000);
|
|
|
|
- },
|
|
|
|
- fail: (err) => {
|
|
|
|
- setTimeout(() => {
|
|
|
|
- that.getAllOnlineDevices();
|
|
|
|
- }, 3 * 1000);
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 获取已连接的蓝牙设备
|
|
|
|
- getConnectedDevices() {
|
|
|
|
- var that = this;
|
|
|
|
- if (!that.isAvailable && !that.hasPermission) {
|
|
|
|
- return [];
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
|
- wx.getConnectedBluetoothDevices({
|
|
|
|
- // services: ["FFC0", "ffc0", "FFC1", "FFC2", "ffc1", "ffc2", "AB00", "ab00", "AB01", "AB02", "FFF1", "fff1", "FFE2", "FFE5",],
|
|
|
|
- // services: ["ab00", "ffe5", "1111", "FFC0", "FFC1", "FFF1", ],
|
|
|
|
- // services: [],
|
|
|
|
- // services: [
|
|
|
|
- // "0000ab00-0000-1000-8000-00805f9b34fb",
|
|
|
|
- // "0000ffc0-0000-1000-8000-00805f9b34fb",
|
|
|
|
- // "0000FFF0-0000-1000-8000-00805F9B34FB",
|
|
|
|
- // "0000FFF1-0000-1000-8000-00805F9B34FB",
|
|
|
|
- // "0000FFE5-0000-1000-8000-00805F9B34FB",
|
|
|
|
- // ],
|
|
|
|
- success: (res) => {
|
|
|
|
- console.log('已连接的蓝牙设备==11==:', newDevices);
|
|
|
|
- let newDevices = that.fiterDevice(res)
|
|
|
|
- console.log('已连接的蓝牙设备==22==:', newDevices);
|
|
|
|
- resolve(newDevices);
|
|
|
|
- },
|
|
|
|
- fail: (err) => {
|
|
|
|
- console.error('获取已连接的蓝牙设备失败:', err);
|
|
|
|
- reject([]);
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- // 获取获取在蓝牙模块生效期间所有搜索到的蓝牙设备。包括已经和本机处于连接状态的设备。
|
|
|
|
- getAllConnectedDevices() {
|
|
|
|
- var that = this;
|
|
|
|
- if (!that.isAvailable && !that.hasPermission) {
|
|
|
|
- return [];
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
|
- wx.getBluetoothDevices({
|
|
|
|
- success: (res) => {
|
|
|
|
- let newDevices = that.fiterDevice(res)
|
|
|
|
- resolve(newDevices);
|
|
|
|
- },
|
|
|
|
- fail: (err) => {
|
|
|
|
- console.error('已扫描过的蓝牙设备失败:', err);
|
|
|
|
- reject([]);
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
closeBle() {
|
|
closeBle() {
|
|
console.log('关闭蓝牙了')
|
|
console.log('关闭蓝牙了')
|
|
- wx.closeBluetoothAdapter();
|
|
|
|
|
|
+ closeBluetoothAdapter();
|
|
}
|
|
}
|
|
|
|
|
|
// 断开与指定设备的连接
|
|
// 断开与指定设备的连接
|
|
disconnect() {
|
|
disconnect() {
|
|
var that = this;
|
|
var that = this;
|
|
- let device = that.device ?? {};
|
|
|
|
|
|
+ let device = that.publicDevice ?? {};
|
|
let deviceId = device.deviceId ?? ""
|
|
let deviceId = device.deviceId ?? ""
|
|
if (deviceId.length === 0) {
|
|
if (deviceId.length === 0) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
+
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
wx.closeBLEConnection({
|
|
wx.closeBLEConnection({
|
|
- deviceId: that.device.deviceId ?? "",
|
|
|
|
|
|
+ deviceId: that.publicDevice.deviceId ?? "",
|
|
success: (res) => {
|
|
success: (res) => {
|
|
- that.device = null;
|
|
|
|
|
|
+ that.publicDevice = null;
|
|
console.log('断开连接成功:', res);
|
|
console.log('断开连接成功:', res);
|
|
resolve(res);
|
|
resolve(res);
|
|
},
|
|
},
|
|
@@ -335,9 +153,9 @@ class bleManager {
|
|
|
|
|
|
console.log('开始发送数据:', data, buffer);
|
|
console.log('开始发送数据:', data, buffer);
|
|
wx.writeBLECharacteristicValue({
|
|
wx.writeBLECharacteristicValue({
|
|
- deviceId: that.device.deviceId,
|
|
|
|
- serviceId: that.device.serviceId,
|
|
|
|
- characteristicId: that.device.characteristicId,
|
|
|
|
|
|
+ deviceId: that.publicDevice.deviceId,
|
|
|
|
+ serviceId: that.publicDevice.serviceId,
|
|
|
|
+ characteristicId: that.publicDevice.characteristicId,
|
|
value: buffer,
|
|
value: buffer,
|
|
success: (res) => {
|
|
success: (res) => {
|
|
// console.log('数据发送成功:');
|
|
// console.log('数据发送成功:');
|
|
@@ -351,23 +169,6 @@ class bleManager {
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
- // 监听发现新设备的事件
|
|
|
|
- onBluetoothDeviceFound(callback) {
|
|
|
|
- var that = this;
|
|
|
|
- wx.onBluetoothDeviceFound((res) => {
|
|
|
|
- let newDevices = that.fiterDevice(res)
|
|
|
|
- // that.devices.push(...devices);
|
|
|
|
- if (newDevices.length > 0) {
|
|
|
|
- // console.log('发现设备1:', res);
|
|
|
|
- }
|
|
|
|
- if (callback) {
|
|
|
|
- callback(newDevices);
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
- getMac() {
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
ab2hex(buffer) {
|
|
ab2hex(buffer) {
|
|
var hexArr = Array.prototype.map.call(
|
|
var hexArr = Array.prototype.map.call(
|
|
new Uint8Array(buffer),
|
|
new Uint8Array(buffer),
|
|
@@ -377,6 +178,7 @@ class bleManager {
|
|
)
|
|
)
|
|
return hexArr.join(':');
|
|
return hexArr.join(':');
|
|
}
|
|
}
|
|
|
|
+
|
|
fiterDevice(res) {
|
|
fiterDevice(res) {
|
|
var that = this;
|
|
var that = this;
|
|
var devices = res.devices.filter(device => {
|
|
var devices = res.devices.filter(device => {
|
|
@@ -428,12 +230,12 @@ class bleManager {
|
|
wx.createBLEConnection({
|
|
wx.createBLEConnection({
|
|
deviceId: device.deviceId,
|
|
deviceId: device.deviceId,
|
|
success: (res) => {
|
|
success: (res) => {
|
|
- that.device = device
|
|
|
|
|
|
+ that.publicDevice = device
|
|
console.log('连接成功:', res);
|
|
console.log('连接成功:', res);
|
|
resolve(true);
|
|
resolve(true);
|
|
},
|
|
},
|
|
fail: (err) => {
|
|
fail: (err) => {
|
|
- that.device = null
|
|
|
|
|
|
+ that.publicDevice = null
|
|
console.error('连接失败:', err);
|
|
console.error('连接失败:', err);
|
|
resolve(false);
|
|
resolve(false);
|
|
}
|
|
}
|
|
@@ -449,7 +251,7 @@ class bleManager {
|
|
wx.getBLEDeviceServices({
|
|
wx.getBLEDeviceServices({
|
|
deviceId: deviceId,
|
|
deviceId: deviceId,
|
|
success: (res) => {
|
|
success: (res) => {
|
|
- // that.device.services = res.services;
|
|
|
|
|
|
+ // that.publicDevice .services = res.services;
|
|
let service_id = "";
|
|
let service_id = "";
|
|
for (let i = 0; i < res.services.length; i++) {
|
|
for (let i = 0; i < res.services.length; i++) {
|
|
if (res.services[i].uuid.toUpperCase().indexOf("AB00") != -1 ||
|
|
if (res.services[i].uuid.toUpperCase().indexOf("AB00") != -1 ||
|
|
@@ -463,14 +265,14 @@ class bleManager {
|
|
service_id = res.services[i].uuid;
|
|
service_id = res.services[i].uuid;
|
|
|
|
|
|
}
|
|
}
|
|
- that.device.serviceId = service_id;
|
|
|
|
|
|
+ that.publicDevice.serviceId = service_id;
|
|
console.log('发现服务2:', service_id);
|
|
console.log('发现服务2:', service_id);
|
|
|
|
|
|
resolve(service_id);
|
|
resolve(service_id);
|
|
// resolve(res.services);
|
|
// resolve(res.services);
|
|
},
|
|
},
|
|
fail: (err) => {
|
|
fail: (err) => {
|
|
- that.device.serviceId = null;
|
|
|
|
|
|
+ that.publicDevice.serviceId = null;
|
|
|
|
|
|
console.error('发现服务失败:', err);
|
|
console.error('发现服务失败:', err);
|
|
reject([]);
|
|
reject([]);
|
|
@@ -483,7 +285,7 @@ class bleManager {
|
|
discoverCharacteristics(deviceId, serviceId) {
|
|
discoverCharacteristics(deviceId, serviceId) {
|
|
var that = this;
|
|
var that = this;
|
|
console.log('发现特征值:' + deviceId + " , " + serviceId);
|
|
console.log('发现特征值:' + deviceId + " , " + serviceId);
|
|
- // if (deviceId !== that.device.deviceId) {
|
|
|
|
|
|
+ // if (deviceId !== that.publicDevice .deviceId) {
|
|
// console.log('设备id不匹配')
|
|
// console.log('设备id不匹配')
|
|
// return false
|
|
// return false
|
|
// }
|
|
// }
|
|
@@ -494,11 +296,11 @@ class bleManager {
|
|
success: (res) => {
|
|
success: (res) => {
|
|
// that.characteristics[serviceId] = res.characteristics;
|
|
// that.characteristics[serviceId] = res.characteristics;
|
|
console.log('发现特征值2:', res);
|
|
console.log('发现特征值2:', res);
|
|
- // that.device.characteristics = res.characteristics;
|
|
|
|
|
|
+ // that.publicDevice .characteristics = res.characteristics;
|
|
resolve(res.characteristics);
|
|
resolve(res.characteristics);
|
|
},
|
|
},
|
|
fail: (err) => {
|
|
fail: (err) => {
|
|
- that.device.characteristics = null;
|
|
|
|
|
|
+ that.publicDevice.characteristics = null;
|
|
console.error('发现特征值失败:', err);
|
|
console.error('发现特征值失败:', err);
|
|
reject("");
|
|
reject("");
|
|
}
|
|
}
|
|
@@ -512,8 +314,8 @@ class bleManager {
|
|
console.log('开始读取特征值', characteristicId)
|
|
console.log('开始读取特征值', characteristicId)
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
wx.readBLECharacteristicValue({
|
|
wx.readBLECharacteristicValue({
|
|
- deviceId: that.device.deviceId,
|
|
|
|
- serviceId: that.device.serviceId,
|
|
|
|
|
|
+ deviceId: that.publicDevice.deviceId,
|
|
|
|
+ serviceId: that.publicDevice.serviceId,
|
|
characteristicId: characteristicId,
|
|
characteristicId: characteristicId,
|
|
success: (res) => {
|
|
success: (res) => {
|
|
const name = that.parseBLEValue(res.value); // 解析读取到的值
|
|
const name = that.parseBLEValue(res.value); // 解析读取到的值
|
|
@@ -531,13 +333,14 @@ class bleManager {
|
|
parseBLEValue(buffer) {
|
|
parseBLEValue(buffer) {
|
|
return String.fromCharCode.apply(null, new Uint8Array(buffer));
|
|
return String.fromCharCode.apply(null, new Uint8Array(buffer));
|
|
}
|
|
}
|
|
|
|
+
|
|
// 监听特征值变化
|
|
// 监听特征值变化
|
|
notifyCharacteristicValueChange(characteristicId, callback) {
|
|
notifyCharacteristicValueChange(characteristicId, callback) {
|
|
var that = this;
|
|
var that = this;
|
|
- console.log('监听特征值变化:', characteristicId, that.device.deviceId, that.device.serviceId);
|
|
|
|
|
|
+ console.log('监听特征值变化:', characteristicId, that.publicDevice.deviceId, that.publicDevice.serviceId);
|
|
wx.notifyBLECharacteristicValueChange({
|
|
wx.notifyBLECharacteristicValueChange({
|
|
- deviceId: that.device.deviceId, //设备mac IOS和安卓系统不一样
|
|
|
|
- serviceId: that.device.serviceId, //服务通道,这里主要是notify
|
|
|
|
|
|
+ deviceId: that.publicDevice.deviceId, //设备mac IOS和安卓系统不一样
|
|
|
|
+ serviceId: that.publicDevice.serviceId, //服务通道,这里主要是notify
|
|
characteristicId: characteristicId, //notify uuid
|
|
characteristicId: characteristicId, //notify uuid
|
|
state: true,
|
|
state: true,
|
|
success: function (res) {
|
|
success: function (res) {
|
|
@@ -569,8 +372,8 @@ class bleManager {
|
|
setWrite(wirte, characteristicId) {
|
|
setWrite(wirte, characteristicId) {
|
|
var that = this;
|
|
var that = this;
|
|
console.log('写入特征值:', characteristicId)
|
|
console.log('写入特征值:', characteristicId)
|
|
- // that.device.wirte = wirte
|
|
|
|
- that.device.characteristicId = characteristicId;
|
|
|
|
|
|
+ // that.publicDevice .wirte = wirte
|
|
|
|
+ that.publicDevice.characteristicId = characteristicId;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// const ble = new bleManager();
|
|
// const ble = new bleManager();
|
|
@@ -614,4 +417,37 @@ module.exports = bleManager;
|
|
// reject(false);
|
|
// reject(false);
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
-// })
|
|
|
|
|
|
+// })
|
|
|
|
+
|
|
|
|
+// // 获取已连接的蓝牙设备
|
|
|
|
+// getConnectedDevices() {
|
|
|
|
+// var that = this;
|
|
|
|
+// if (!that.isAvailable && !that.hasPermission) {
|
|
|
|
+// return [];
|
|
|
|
+// }
|
|
|
|
+
|
|
|
|
+// return new Promise((resolve, reject) => {
|
|
|
|
+// wx.getConnectedBluetoothDevices({
|
|
|
|
+// // services: ["FFC0", "ffc0", "FFC1", "FFC2", "ffc1", "ffc2", "AB00", "ab00", "AB01", "AB02", "FFF1", "fff1", "FFE2", "FFE5",],
|
|
|
|
+// // services: ["ab00", "ffe5", "1111", "FFC0", "FFC1", "FFF1", ],
|
|
|
|
+// // services: [],
|
|
|
|
+// // services: [
|
|
|
|
+// // "0000ab00-0000-1000-8000-00805f9b34fb",
|
|
|
|
+// // "0000ffc0-0000-1000-8000-00805f9b34fb",
|
|
|
|
+// // "0000FFF0-0000-1000-8000-00805F9B34FB",
|
|
|
|
+// // "0000FFF1-0000-1000-8000-00805F9B34FB",
|
|
|
|
+// // "0000FFE5-0000-1000-8000-00805F9B34FB",
|
|
|
|
+// // ],
|
|
|
|
+// success: (res) => {
|
|
|
|
+// console.log('已连接的蓝牙设备==11==:', newDevices);
|
|
|
|
+// let newDevices = that.fiterDevice(res)
|
|
|
|
+// console.log('已连接的蓝牙设备==22==:', newDevices);
|
|
|
|
+// resolve(newDevices);
|
|
|
|
+// },
|
|
|
|
+// fail: (err) => {
|
|
|
|
+// console.error('获取已连接的蓝牙设备失败:', err);
|
|
|
|
+// reject([]);
|
|
|
|
+// }
|
|
|
|
+// });
|
|
|
|
+// });
|
|
|
|
+// }
|