uniapp 蓝牙封装

BLETool.js

import VueFunc from "./vue-func.js"
import Constant from './constant.js'
export default {
	scanDeviceList: [],
	storageDeviceList: [],
	crtName: null,
	conState: -1, //销毁  0:未连接 1:正在连接  2:已连接
	checkTimer: null,
	init() {
		this.openBluetoothAdapter(() => {
			this.startBluetoothDevicesDiscovery();
		});
		uni.onBluetoothDeviceFound(res => {
			this.putScanDevice(res.devices[0]);
		});
		this.storageDeviceList = uni.getStorageSync('storageDeviceList');
		if (!this.storageDeviceList) this.storageDeviceList = [];
		this.conState = 0;
		this.checkTimer = setInterval(() => {
			if (this.conState == 0) {
				this.createBLEConnection(this.crtName);
			}
		}, 60000);
		uni.onBLEConnectionStateChange(res => {
			if (!res.connected) {
				this.conState = 0;
			}
		});
		uni.onBLEConnectionStateChange(res => {


		})
	},
	getDeviceByName(name) {
		for (var item of this.storageDeviceList) {
			if (item.name == name) {
				return item;
			}
		}
		for (var item of this.scanDeviceList) {
			if (item.name == name) {
				return item;
			}
		}
		return null;
	},
	getDeviceById(deviceId) {
		for (var item of this.storageDeviceList) {
			if (item.deviceId == deviceId) {
				return item;
			}
		}
		for (var item of this.scanDeviceList) {
			if (item.deviceId == deviceId) {
				return item;
			}
		}
		return null;
	},
	putScanDevice(device) {
		this.addDevice(device, this.scanDeviceList);
	},
	putStorageDevice(device) {
		if (!this.addDevice(device, this.storageDeviceList)) {
			uni.setStorageSync('storageDeviceList', this.storageDeviceList);
		}
	},
	addDevice(device, deviceList) {
		var isContain = false;
		for (var item of deviceList) {
			if (item.deviceId == device.deviceId) {
				isContain = true;
				item.RSSI = device.RSSI;
				break;
			}
		}
		if (!isContain) {
			deviceList.push(device);
		}
		return isContain;
	},

	openBluetoothAdapter(successCallBack) {
		uni.openBluetoothAdapter({
			success(res) {
				console.log(res);
				if (successCallBack != null &&
					successCallBack != undefined) successCallBack();
			},
			fail() {
				uni.showToast({
					title: "蓝牙启动失败,请确认蓝牙已打开",
					icon: "none",
					duration: 2000
				});
			}
		});
	},
	startBluetoothDevicesDiscovery() {
		uni.startBluetoothDevicesDiscovery({
			success: res => {
				console.log(res)
				setTimeout(() => {
					this.stopBluetoothDevicesDiscovery();
				}, 20000);
			}
		})
	},
	stopBluetoothDevicesDiscovery() {
		uni.stopBluetoothDevicesDiscovery({
			success: res => {
				console.log(res)
			}
		})
	},
	createByName(name) {
		this.crtName = name;
		var device = this.getDeviceByName(name);
		if (device != null) {
			this.conState = 1;
			uni.createBLEConnection({
				deviceId: device.deviceId,
				success: res => {
					if (res.errMsg == 'createBLEConnection:ok') {
						uni.setBLEMTU({
							deviceId: deviceUUID,
							mtu: 512
						})
						console.log("连接成功");
						this.putStorageDevice(device);
						this.conState = 2;
						setTimeout(() => {
							this.getBLEDeviceServices(device);
						}, 1000);
					} else {
						this.conState = 0;
					}
				},
				fail: () => {
					this.conState = 0;
				}
			})
		} else {
			this.startBluetoothDevicesDiscovery();
		}
	},

	createByDeviceId(deviceId) {
		var device = this.getDeviceById(name);
		if (device != null) {
			this.conState = 1;
			uni.createBLEConnection({
				deviceId: device.deviceId,
				success: res => {
					if (res.errMsg == 'createBLEConnection:ok') {
						uni.setBLEMTU({
							deviceId: deviceUUID,
							mtu: 512
						})
						console.log("连接成功");
						this.putStorageDevice(device);
						this.conState = 2;
						setTimeout(() => {
							this.getBLEDeviceServices(device);
						}, 1000);
					} else {
						this.conState = 0;
					}
				},
				fail: () => {
					this.conState = 0;
				}
			})
		} else {
			this.startBluetoothDevicesDiscovery();
		}
	},
	getBLEDeviceServices(deviceId) {
		uni.getBLEDeviceServices({
			deviceId: deviceId,
			success: res => {
				console.log('device getBLEDeviceCharacteristics:', res
					.characteristics)
				setTimeout(() => {
					this.getBLEDeviceCharacteristics(deviceId);
				}, 1000);
			}
		});

	},
	getBLEDeviceCharacteristics(deviceId) {
		uni.getBLEDeviceCharacteristics({
			deviceId: deviceId,
			serviceId: Constant.COM_SERVICE_UUID,
			success: res => {
				console.log('device getBLEDeviceCharacteristics:', res
					.characteristics)
				setTimeout(() => {
					this.notifyBLECharacteristicValueChange(deviceId);
				}, 1000);
			}
		});
	},
	notifyBLECharacteristicValueChange(deviceId) {
		uni.notifyBLECharacteristicValueChange({
			state: true, // 启用 notify 功能
			deviceId: deviceId,
			serviceId: Constant.COM_SERVICE_UUID,
			characteristicId: Constant.RX_CHAR_UUID,
			success: res => {
				console.log('notifyBLECharacteristicValueChange success', res.errMsg)
			}
		});
	},

}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值