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

news/2024/7/10 0:07:50 标签: javascript, 蓝牙, uni-app, vue

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

javascript"><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,在需要页面引入

javascript"><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>

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


http://www.niftyadmin.cn/n/734833.html

相关文章

mysql数据库实现主从复制

2019独角兽企业重金招聘Python工程师标准>>> MySQL数据库自身提供的主从复制功能可以方便的实现数据的多处自动备份&#xff0c;实现数据库的拓展。多个数据备份不仅可以加强数据的安全性&#xff0c;通过实现读写分离还能进一步提升数据库的负载性能。 下图就描述了…

4,uniapp功能之——APP更新,打包上线安装后的app的随时更新,页面实时显示当前更新的百分比

App.vue这文件都有吧&#xff0c;当然&#xff0c;也可以写在其他页面&#xff0c;看项目需求了。 思路&#xff1a;第一个方法调接口返回来的是版本号和新的版本链接&#xff0c;将这个版本号与当前app的版本号进行比较&#xff0c;接口返回的版本号大于当前的版本号就弹窗提示…

关于springboot自动注入出现Consider defining a bean of type 'xxx' in your configuration问题解决方案...

搭建完spring boot的demo后自然要实现自动注入来体现spring ioc的便利了&#xff0c;但是我在实施过程中出现了这么一个问题&#xff0c;见下面&#xff0c;这里找到解决办法记录下来&#xff0c;供遇到同样的问题的同僚参考 Description:Field helloService in com.example.…

vue生成二维码插件

效果图 1&#xff0c;下插件 cnpm i vue-qr --save2,在需要的页面引入&#xff0c;或者全局引入&#xff0c;我这里是局部引入&#xff0c;因为只有一个页面用到了。 html <vue-qr :text"text" :size"200" :margin"0" />js import VueQ…

1,vue播放视频之—引入.m3u8后缀的hsl视频流

效果图&#xff1a; 我这个是引入js的方式播放的。也可以用npm直接下载hsl.sj进行引入 1.public里面index.html页面引入对应的js <script src"./jquery.min.js"></script> <script src"./ezuikit.js"></script> <script src…

2,vue播放视频之—问题。引入多个视频,视频大小不一样要求铺满全屏问题,停止视频

1&#xff0c;视频全屏铺满父级元素 一行属性&#xff1a;object-fit: fill; 没加之前&#xff1a; 加了之后&#xff1a; 2&#xff0c;停止视频 destroyed () {for(var j0;j<this.players.length;j){this.players[j].stop();}}看图&#xff1a; 如果第一个格式的文件没…

Http请求中Content-Type讲解以及在Spring MVC注解中produce和consumes配置详解

https://blog.csdn.net/shinebar/article/details/54408020 补充&#xff1a; Consumes Produces分别表示入参和出参数吗 可以这样讲。但是不是很到位。是限定作用&#xff0c;类似于filterconsumes&#xff1a; 指定处理请求的提交内容类型&#xff08;Content-Type&#xff0…

vue回车登录,登录完销毁回车事件。

1.在登录页面写 methods:{// 回车登录keyDown(e){if(e.keyCode 13){//13是回车键的keycodeif(this.ruleForm.usename ! && this.ruleForm.password ! ) {this.logoin()}else {this.submitForm(ruleForm)}}} } mounted(){window.addEventListener(keydown,this.keyDo…