Uni-app实现蓝牙搜索、连接、传送数据实例

<template>
	<!-- 自定义导航栏 -->
    <view class="bluetooth">
	   <view class="navBarBox">
			<!-- 状态栏占位 -->
			<view class="statusBar" :style="{ paddingTop: statusBarHeight + 'px' }"></view>
			<!-- 真正的导航栏内容 -->
			<view class="navBar">
				<view class="titlefont" style="margin-left: 10px;">通信设备</view>
			</view>
		</view>
        <!-- 发送数据 -->
      <view class="send">
            <view class="uni-textarea">
                <view @tap="send_data" class="fasong">发送</view>
				<view class="">
					蓝牙返回的数据为:
				</view>
            </view>

        </view>
        <!-- 没有匹配的蓝牙设备 -->
		
       <view class="no_match_bluetooth_list" v-if="no_match_list.length!=0">
            <view class="h1">附近蓝牙设备</view>
			<scroll-view scroll-y="100%">
            <flex  v-for="(item,index) in no_match_list" :key="index" >
                <view class="uni-list" @tap="print(item.SN)">
                    <view class="uni-list-cell" hover-class="uni-list-cell-hover">
                        <view class="uni-list-cell-navigate uni-navigate-right uni-media-list ">
                            <view class="uni-media-list-body">
								<image src="@/static/img/lanya.png" class="lanyaimg" mode="scaleToFill"></image>
								<view class="lanySn"> {
  {item.name}} </view>
                            </view>
                        </view>
                    </view>
                </view>
            </flex>
			</scroll-view>
        </view>

        <view>
			<popuphelp :helpshowModal="helpshowPay" @helpclose="oncloseahelp"></popuphelp>
		</view>
        <view class="btn">
            <view class="btn_1" @tap="open_bluetooth()">帮 助</view>
            <view class="btn_3" @tap="search_bluetooth()">刷 新</view>
        </view>
    </view>
</template>

<script>
	import popuphelp from "@/pages/main/popuphelp.vue"
	import blueTool from '../../common/dataFormatUtil.js';
	import uTool from '../../common/buletoothUtil.js'
    var main, Context, BluetoothManager, BluetoothAdapter, BManager, BAdapter,BluetoothDevice,IntentFilter,bluetoothSocket,device,_self;
	export default {
		components:{
			popuphelp
		},
        data() {
            return {
                bArray:[],//用于搜索蓝牙去重用的
                no_match_list:[],//没有配对的蓝牙列表
                send_data_onoff:false,
                send_data_list:'',//要发送的数据
				statusBarHeight: 0,// 状态栏高度
				helpshowPay: false,
				show: false,
				timer: null,
				BDeviceList:[],
				fa: '',
				shou: '',
				serviceId: '', //蓝牙服务 uuid
				connectionid: '', //设备的 id
				characteristicid: '', //特征
				sheb: "",
				mList:[],//搜索到的设备列表
				mDeviceId:0,//连接的设备
				targetNumber:'',//要连接的指定设备
            };
        },
        onLoad() {
			//获取手机状态栏高度
			this.statusBarHeight = uni.getSystemInfoSync()['statusBarHeight'];
            //获取android应用Activity对象
            main = plus.android.runtimeMainActivity();
            Context = plus.android.importClass("android.content.Context");
            BManager = main.getSystemService(Context.BLUETOOTH_SERVICE);
            //蓝牙适配器
            BluetoothAdapter = plus.android.importClass("android.bl
  • 2
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Trouble-Solver

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

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

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

打赏作者

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

抵扣说明:

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

余额充值