uniapp/微信小程序获取蓝牙设备

  • view
  • script
  • style

此文章只是获取蓝牙设备,不涉及连接设备,连接设备敬请期待!!!

view

<u-navbar :is-back="true" title="设备" :background="background" title-color="#FFFFFF" back-icon-color="#FFFFFF":border-bottom="false"></u-navbar><button class='button' hover-class="hover" @click="startSearch" :loading='isScanning'> 搜索蓝牙设备 </button><scroll-view class="device_list" scroll-y scroll-with-animation><view v-for="(item,index) in list" :data-title="item.deviceId" :data-name="item.name":data-advertisData="item.advertisServiceUUIDs" :key="item.deviceId" @click="bindViewTap" class="item"hover-class="item_hover"><view style="font-size: 16px; color: #333;">{{item.name}}</view><view style="font-size: 16px; color: #333;">{{item.deviceId}}</view><view style="font-size: 10px">信号强度: {{item.RSSI}}dBm</view></view></scroll-view>

script

export default {data() {return {background: {backgroundColor: '#60BFAC',},list: [],services: [],serviceId: 0,writeCharacter: false,readCharacter: false,notifyCharacter: false,isScanning: false,isOpenBle: false,deviceId: '',globalData: {userInfo: null,platform: "",sysinfo: '',screenWidth: wx.getSystemInfoSync().screenWidth,screenHeight: wx.getSystemInfoSync().screenHeight,},BLEInformation: {platform: "",deviceId: "",writeCharaterId: "",writeServiceId: "",notifyCharaterId: "",notifyServiceId: "",readCharaterId: "",readServiceId: "",}}},onLoad() {//获取手机型号model,微信版本号version,操作系统版本system,客户端平台platform,客户端基础库版本SDKVersionthis.globalData.sysinfo = uni.getSystemInfoSync();console.log(this.globalData);//在页面加载时候初始化蓝牙适配器uni.openBluetoothAdapter({success: e => {console.log('初始化蓝牙成功:' + e.errMsg);this.isOpenBle = true;console.log(this.isOpenBle);// 初始化完毕开始搜索this.startBluetoothDeviceDiscovery()},fail: e => {console.log('初始化蓝牙失败,错误码:' + (e.errCode || e.errMsg));wx.showModal({title: '提示',content: '蓝牙初始化失败,请到设置打开蓝牙',showCancel: false})}});},methods: {startBluetoothDeviceDiscovery() {//在页面显示的时候判断是都已经初始化完成蓝牙适配器若成功,则开始查找设备let self = this;setTimeout(function() {if (self.isOpenBle) {console.log("开始搜寻智能设备");uni.startBluetoothDevicesDiscovery({success: res => {self.onBluetoothDeviceFound();},fail: res => {console.log("查找设备失败!");uni.showToast({icon: "none",title: "查找设备失败!",duration: 3000})}});} else {console.log("未初始化蓝牙是配饰器:" + self.isOpenBle);}}, 300);},/*** 停止搜索蓝牙设备*/stopBluetoothDevicesDiscovery() {uni.stopBluetoothDevicesDiscovery({success: e => {console.log('停止搜索蓝牙设备:' + e.errMsg);},fail: e => {console.log('停止搜索蓝牙设备失败,错误码:' + e.errCode);}});},/*** 发现外围设备*/onBluetoothDeviceFound() {console.log("监听寻找新设备");// this.getBluetoothDevices();uni.onBluetoothDeviceFound(devices => {console.log('开始监听寻找到新设备的事件');this.getBluetoothDevices();});},/*** 获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。*/getBluetoothDevices() {console.log("获取蓝牙设备");uni.getBluetoothDevices({success: res => {console.log('获取蓝牙设备成功:' + res.errMsg);console.log(res)var devices = [];var num = 0;for (var i = 0; i < res.devices.length; i++) {if (res.devices[i].name != "未知设备") {devices[num] = res.devices[i]num++}}this.list = devices;}});},}}

style

.button {margin-top: 20px;width: 90%;background-color: #60BFAC;color: white;border-radius: 98rpx;}.device_list {height: auto;margin-left: 20px;margin-right: 20px;margin-top: 10px;margin-bottom: 20px;border: 1px solid #EEE;border-radius: 5px;width: auto;}.td {display: flex;align-items: center;justify-content: center;margin-top: 10px;}/* .item {padding-top: 10px;padding-bottom: 10px;height: 130rpx;width: 100%;} */.item {display: block;/* font-family:  Arial, Helvetica, sans-serif;font-size:14px;margin: 0 10px;margin-top:10px;margin-bottom: 10px;background-color:#FFA850; border-radius: 5px;border-bottom: 2px solid #68BAEA; */border-bottom: 1px solid #EEE;padding: 4px;color: #666;}.item_hover {background-color: rgba(0, 0, 0, .1);}.block {display: block;color: #ffffff;padding: 5px;}

uniapp/微信小程序获取蓝牙设备相关推荐

  1. uni-app 微信小程序获取位置信息提示“getLocation 需要在 app.json 中申明 permission 字段”

    本文同步发布在:uni-app 微信小程序获取位置信息提示"getLocation 需要在 app.json 中申明 permission 字段 我们在用 uni-app 开发微信小程序时, ...

  2. uni-app微信小程序获取手机号;微信小程序获取手机号,获取到后需要进行解密;微信小程序获取手机号失败 Error:该appId没有权限

    方法1.uni-app登录加获取手机号解密-直接看此篇即可解决 微信小程序获取手机号注意点: 1.需要先登录,微信小程序登录看这篇 2.手机号获取到时加密的,需要后端解密或者前端自己解密(解密时候就会 ...

  3. uniapp微信小程序获取屏幕宽高

    uniapp开发微信小程序的时候,有时候去调整样式 你需要适配各种手机屏幕,使用,你的样式宽高就不能使用rpx 有的朋友觉得可以使用vw  vh  %   是的,当然可以 但是要让你的元素,宽高,比如 ...

  4. uni-app微信小程序生成自定义参数二维码,跳转小程序指定页面,获取参数;uni-app微信小程序获取二维码自定义参数;微信小程序生成动态参数二维码;uni-app微信小程序获取动态参数二维码;

    一.场景需求: 在小程序个人名片页面A页面,生成用户的个人名片二维码(该二维码携带用户的唯一标识id):微信扫一扫或长按图片识别这个二维码,可以跳转到小程序的B页面,并且在B页面拿到二维码上的唯一标识 ...

  5. uniapp微信小程序获取定位(通过经纬度获取地址)

    项目要求首页有个定位功能,但是uni.getLocation在小程序端只能获取到经纬度,拿不到地址 uniapp官网有说明这点:uni.getLocation(OBJECT) | uni-app官网 ...

  6. uniapp微信小程序获取当前位置信息、经纬度转换、导航地图实现

    1.调用接口,官网文档是这样写的 uni.getLocation({type: 'wgs84',success: function (res) {console.log('当前位置的经度:' + re ...

  7. uniapp 微信小程序 获取refs是空对象

    除 加载时机问题.(大前提,DOM加载完成后之类的 排除) this.$nextTick(()=>{this.methods(); }) 一般包裹this.nextTick就可以解决.但是由于微 ...

  8. uniapp 微信小程序获取用户信息、手机号

    <template><!-- #ifdef MP-WEIXIN --><view><view><button type="primary ...

  9. uniapp微信小程序获取位置(高德SDK)

    效果图: 在uniapp官网给出了一个全局api用来获取用户位置 uni.getLocation 成功的回调函数会返回相关的用户信息 但是仔细参考,就会发现就address参数返回的很直接,但是也仅限 ...

最新文章

  1. RabbitMQ入门(4)--路由
  2. IIS状态代码的含义
  3. 查看centos信息
  4. 编写代码的「八荣八耻」- 以用户易用为荣,以复杂歧义为耻
  5. ASP.NET的MVC中使用Cookie做身份验证(附代码下载)
  6. 23.1解析函数的级数表示(二)
  7. 博途v15 固件升级包_使用博途TIA 15.1对SIMATIC S7-1200进行硬件组态和固件版本在线升级(博途V15和1200学习笔记一)...
  8. TM4C123G学习记录(4)--关于ROM前缀函数和HWREG函数
  9. 成都涌现大量网游业创业团队:从业人员近3万(网游亦可创业)
  10. android 表情包下载,表情包制作pro
  11. CommandLineRunner接口介绍
  12. Windows7+CentOS双系统同一硬盘
  13. 管式静态混合器 不锈钢管道混合器
  14. 论文笔记:Convolutional Neural Networks on Graphs with Fast Localized Spectral Filtering
  15. mysql常用函数整理
  16. Davinci Resolve Studio 17d5达芬奇调色软件专业版
  17. 【Android】之【WebView】
  18. 联想笔记本突然没声音了,但是音量却是打开得,F1亮着
  19. MyBatis - 批量插入
  20. 【项目实战】传智健康

热门文章

  1. 最安全的现货白银技巧有哪些?
  2. Revolving Digits
  3. Java学习之路-day18 集合02
  4. NVIDIA DLSS2.0 AI渲染又一大飞跃
  5. Unity3D游戏开发之类对象池优化秘籍残篇
  6. rabbitmq入门(一)简单队列(Hello World!、Work queues)
  7. 历史最全GAN网络及其各种变体整理(附论文及代码实现)
  8. PC端桌面图标变白的修复方法
  9. Elastic实战:nested查询与数组同一元素匹配多个值
  10. vivo X60 Pro+对小米10 Ultra、iPhone 12 Pro Max、X50 Pro+