uniapp实现蓝牙通信

本文通过uniapp演示了如何使用vue2和js进行蓝牙连接操作,包括初始化蓝牙、搜索设备、连接设备、获取蓝牙服务和特征值,以及监听和发送数据。示例代码展示了设备列表、服务列表和特征值列表的UI界面,以及相关方法的实现过程。

uniapp实现读取蓝牙连接蓝牙等操作(vue2+js)直接一把梭版

参考博客地址:https://juejin.cn/post/7093171532318375950

界面demo如下

在这里插入图片描述
在这里插入图片描述

代码部分

<template>
	<view>
		<view class="BuuetoothServiceList">
			<text>蓝牙设备列表</text>
		</view>
		<scroll-view scroll-y class="box">
			<view class="item" v-for="item in blueDeviceList" @click="connect(item)">
				<view>
					<text>id: {
   
   {
   
    item.deviceId }}</text>
				</view>
				<view>
					<text>RSSI:{
   
   {
   
   item.RSSI}}</text>
				</view>
				<view>
					<text>name: {
   
   {
   
    item.name }}</text>
				</view>
			</view>
		</scroll-view>
		<button @click="initBluetooth()"
			style="width: 9rem;height: 3rem;background-color: rgb(138, 145, 159);">①蓝牙连接</button>
		<view class="BuuetoothServiceList">
			<text>蓝牙服务列表</text>
		</view>
		<scroll-view scroll-y class="box">
			<view class="item" v-for="item in sevicesList">
				<view>
					<text user-select="true">uuid: {
   
   {
   
    item.uuid }}</text>
				</view>
			</view>
		</scroll-view>
		<view class="BuuetoothServiceList">
			<text>蓝牙特征值列表</text>
		</view>
		<scroll-view scroll-y class="box">
			<view class="item" v-for="item in eigenvalueList">
				<view>
					<text user-select="true">uuid: {
   
   {
   
    item.uuid }}</text>
				</view>
			</view>
		</scroll-view>

		<!-- <uni-easyinput type="text" v-model="formData.name" placeholder="请输入姓名" /> -->
		<view class="btnBox"><input type="text"
				style="width: 75%;height: 3rem;background-color: rgb(246, 244, 241);margin-left: 5%;border-radius: 20rpx;box-shadow:0 0 8rpx #000;margin-top: 20rpx;"
				placeholder="请输入蓝牙服务表中的uuid" v-model="serviceUuid" />
			<view class="clearInput">
				<icon type="clear" @click="clearIptA()"></icon>
			</view>
		</view>

		<view class="btnBox">
			<!-- <button @click="discovery()"
				style="width: 12rem;height: 3rem;margin-top: 1rem;background-color: rgb(138, 145, 159);">②搜索附近蓝牙设备</button>
			<button @click="getServices()"
				style="width: 10rem;height: 3rem;margin-top: 1rem;background-color: rgb(138, 145, 159);">③获取蓝牙服务</button> -->

			<button @click="getCharacteristics()"
				style="width: 10rem;height: 3rem;margin-top: 1rem;background-color:rgb(138, 145, 159);">②获取特征值</button>
			<!-- <button @click="notify()"
				style="width: 10rem;height: 3rem;margin-top: 1rem;background-color:rgb(138, 145, 159);">③开启消息监听</button> -->
		</view>
		<view class="btnBox">
			<input type="text"
				style="box-shadow:0 0 8rpx #000;width: 75%;height: 3re
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值