3,uniapp功能之—蓝牙秤,连接蓝牙秤获取重量实时显示在页面上(坤宏的蓝牙秤)

1,在component里面新建一个blueTooth.vue组件

<template>
	<view>
		<view v-for="(item,index) in devices" :key="index" class="boxs" @click="BlueIds(item)">
			<view>设备名称:{{item.name}}</view>
		</view>
	</view>
</template>

<script>
	var main, receiver, filter;
	var _codeQueryTag = false;
	export default {
		data() {
			return {
				devices: [],//蓝牙列表
				weightTotal:'',//重量
			}
		},
		methods: {
			// 初始化蓝牙设备
			seach_searchBle() {
				var that = this
				uni.openBluetoothAdapter({
					success(res) {
						uni.setStorage({key:"openBlue",data:1})
						that.seach_onDevice()
						uni.getBluetoothAdapterState({
							success: function(res) {
								if (res.available) {
									if (res.discovering) {
										that.stopFindBule()
									}
									//搜索蓝牙
									uni.startBluetoothDevicesDiscovery({
										success(res) {
											uni.showToast({title:'搜索到以下设备',icon:'none'})
										}
									})
									that.devices = []
								} else {
									uni.showToast({title: '本机蓝牙不可用',icon:'none'})
								}
							},
						})
					}
				})
			},
			// 停止搜索设备
			stopFindBule(){
				wx.stopBluetoothDevicesDiscovery({
					success(res) {
						uni.showToast({title: '停止搜索',icon:'none'})
					}
				})
			},
			// 获取蓝牙设备列表
			seach_onDevice() {
				var that = this
				//监听寻找到新设备的事件
				uni.onBluetoothDeviceFound(function(devices) {
					var re = JSON.parse(JSON.stringify(devices))
					let name = re.devices[0].name;
					//如果没有名字的设备就给个未知设备
					if(name ==''){
						name = "未知设备"
					}
					let deviceId = re.devices[0].deviceId
					that.devices.push({
						name: name,
						deviceId: deviceId,
						services: [],
						RSSI: re.devices[0].RSSI
					})
				})
			},
			// 点击蓝牙列表的某个蓝牙去连接蓝牙
			BlueIds(item) {
				var that = this;
				let deviceId = item.deviceId;
				wx.createBLEConnection({
					deviceId: deviceId,
					success(res) {
						if (res.errMsg == "createBLEConnection:ok") {
							uni.showLoading({title: '连接蓝牙中'})
							setTimeout(() => {that.lianjielanya(deviceId)}, 2000)
						} else {uni.showToast({title: '22连接失败,请重试',icon:'none'})}
					},
					fail(res) {uni.showToast({title: '33连接失败,请重试',icon:'none'})}
				})
			},
			// 连接蓝牙秤获取所有服务
			lianjielanya(deviceId){
				var that = this;
				wx.getBLEDeviceServices({
					deviceId: deviceId,
					success(res) {
						let serviceId = "";
						for (var s = 0; s < res.services.length; s++) {
							let serviceId = res.services[s].uuid
							wx.getBLEDeviceCharacteristics({
								deviceId: deviceId,
								serviceId: serviceId,
								success(res) {
									var re = JSON.parse(JSON.stringify(res));
									for (var c = 0; c < re.characteristics.length; c++) {
										if (re.characteristics[c].properties.write == true) {
											var uuid = re.characteristics[c].uuid
											for (var index in that.devices) {
												if (that.devices[index].deviceId == deviceId) {
													that.devices[index].services.push({
														serviceId: serviceId,
														characteristicId: uuid
													})
													break
												}
											}
											setTimeout(() => {wx.hideLoading()}, 2000)
											uni.showToast({title: '连接成功',icon:'none'})
										}
									}
									that.notifyBLECharacteristicValueChange(deviceId)
								}
							})
						}
					},
					fail(res) {uni.showToast({title: '11连接失败,请重试',icon:'none'})},
				})
			},
			// 获取蓝牙秤的重量
			notifyBLECharacteristicValueChange(deviceId) {
				var that = this
				wx.notifyBLECharacteristicValueChange({
					state: true,
					deviceId: deviceId,
					serviceId: "0000FFE0-0000-1000-8000-00805F9B34FB",
					characteristicId: "0000FFE1-0000-1000-8000-00805F9B34FB",
					success: (res) => {
						wx.onBLECharacteristicValueChange((res) => {
							//获取到的蓝牙秤返回的值不是十进制的数字,需要进行转换
							let resValue = res.value
							var view = new Uint8Array(resValue);
							let length = view.byteLength
							var binary = '';
							for(let i= 0; i < length; i++){
								binary += String.fromCharCode(view[i])
							}
							var splitA = binary.split(",");
							var weight = splitA[2];
							let b = weight.slice(1,8);
						// 接收蓝牙秤的值
						uni.$emit('blueTooth',Number(b))
						this.weightTotal = Number(b)
						})
					},
					fail: (res)=> {
						console.log(res);
						uni.showToast({title:'获取失败,请重试',icon:'none'})
					}
				})
			},
		},
		mounted() {
			//打开页面自动获取蓝牙列表
			this.seach_searchBle()
		},
	}
</script>

<style>
.boxs{
	width: 100%;
	border: 1px solid #ccc;
	background: #ddd;
	font-size: 18px;
	padding: 20px 0;
	margin: 5px 0;
}
</style>

2,在需要页面引入

<template>
	<view>
		<blueTooth></blueTooth>
	</view>
</template>
<script>
	import blueTooth from "../../components/blue.vue";
	export default {
		components: {
			blueTooth,
		},
		onLoad() {
			// 接收蓝牙的值
			uni.$on('blueTooth',(data)=>{
				console.log(data);
			})
		}
	}
</script>

没了,结束了,是不是很简单呐,如有问题,欢迎留言。
最后:如果此篇博文对您有帮助,还请动动小手点点关注点点赞呐~,谢谢 ~ ~

  • 10
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杨同学*

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值