日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

uniapp蓝牙模块demo

發布時間:2023/12/20 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 uniapp蓝牙模块demo 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<template><view><button type="primary" @click="startBluetoothDeviceDiscovery">搜索藍牙</button><button type="primary" @click="onBluetoothDeviceFound">發現外圍設備</button><button type="primary" @click="getBluetoothDevices">獲取藍牙設備信息</button><button type="primary" @click="getBLEDeviceServices">獲取藍牙所有服務</button><button type="primary" @click="getBLEDeviceCharacteristics">進入特征</button><view v-for="(item, index) in list" :key="index">{{ item.name }}:{{ item.deviceId }}<button type="primary" @click="createBLEConnection(item.deviceId)">連接</button></view><button type="primary" @click="writeBLECharacteristicValue">寫入藍牙</button><button type="primary" @click="stopBluetoothDevicesDiscovery">停止搜索藍牙</button></view> </template><script>export default {data() {return {list:[],deviceId:'',serviceId:'',characteristics:[],characteristicId:'',}},onShow(){uni.openBluetoothAdapter({success:(res)=> { //已打開uni.getBluetoothAdapterState({//藍牙的匹配狀態success:(res1)=>{console.log(res1,'本機設備的藍牙已打開')// 開始搜索藍牙設備this.startBluetoothDeviceDiscovery()},fail(error) {uni.showToast({icon:'none',title: '查看手機藍牙是否打開'});}});},fail:err=>{ //未打開 uni.showToast({icon:'none',title: '查看手機藍牙是否打開'});}})},methods: {// 開始搜索藍牙設備startBluetoothDeviceDiscovery(){uni.startBluetoothDevicesDiscovery({success: (res) => {console.log('startBluetoothDevicesDiscovery success', res)// 發現外圍設備this.onBluetoothDeviceFound()},fail:err=>{console.log(err,'錯誤信息')}})},// 發現外圍設備onBluetoothDeviceFound() {uni.onBluetoothDeviceFound((res) => {console.log(JSON.stringify(res.devices))if(this.list.indexOf(res.devices[0].deviceId)==-1){this.list.push({name:res.devices[0].name,deviceId:res.devices[0].deviceId})}})},//獲取在藍牙模塊生效期間所有已發現的藍牙設備。包括已經和本機處于連接狀態的設備。getBluetoothDevices() {console.log("獲取藍牙設備");uni.getBluetoothDevices({success: res => {console.log('獲取藍牙設備成功:');console.log(res.devices);}});},//選擇設備連接吧deviceId傳進來createBLEConnection(deviceId){//data里面建立一個deviceId,存儲起來this.deviceId = deviceId,//連接藍牙uni.createBLEConnection({// 這里的 deviceId 需要已經通過 createBLEConnection 與對應設備建立鏈接deviceId:this.deviceId,success(res) {console.log(res)console.log("藍牙連接成功")},fail(res) {console.log("藍牙連接失敗",res)}})},// 停止搜尋藍牙設備stopBluetoothDevicesDiscovery(){uni.stopBluetoothDevicesDiscovery({success: e => {this.loading = falseconsole.log('停止搜索藍牙設備:' + e.errMsg);},fail: e => {console.log('停止搜索藍牙設備失敗,錯誤碼:' + e.errCode);}});},//獲取藍牙特征getBLEDeviceCharacteristics(){console.log("進入特征");setTimeout(()=>{uni.getBLEDeviceCharacteristics({// 這里的 deviceId 需要已經通過 createBLEConnection 與對應設備建立鏈接deviceId:this.deviceId,// 這里的 serviceId 需要在 getBLEDeviceServices 接口中獲取serviceId:this.serviceId,success:(res)=>{console.log(res,'特征getBLEDeviceCharacteristics')this.characteristics = res.characteristicsconsole.log(this.characteristics)res.characteristics.forEach((item)=>{if(item.uuid.indexOf("470A") != -1){this.characteristicId = item.uuid//console.log('characteristicId:', item.uuid)//利用傳參的形勢傳給下面的notify,這里的uuid如果都需要用到,就不用做判斷了,建議使用setTimeout進行間隔性的調用此方法this.notifyBLECharacteristicValueChange(item.uuid)}})},fail:(res)=>{console.log(res)}})},1000)},// 啟用 notify 功能notifyBLECharacteristicValueChange(characteristicId){console.log(characteristicId,'characteristicId')uni.notifyBLECharacteristicValueChange({state: true, // 啟用 notify 功能// 這里的 deviceId 需要已經通過 createBLEConnection 與對應設備建立鏈接deviceId:this.deviceId,// 這里的 serviceId 需要在 getBLEDeviceServices 接口中獲取serviceId:this.serviceId,// 這里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中獲取characteristicId:characteristicId,success:(res)=> {console.log(res)// console.log(this.characteristicId)console.log('notifyBLECharacteristicValueChange success', res.errMsg)},fail:(res)=> {console.log('notifyBLECharacteristicValueChange fail', res.errMsg)}})},//獲取藍牙的所有服務getBLEDeviceServices(){setTimeout(()=>{uni.getBLEDeviceServices({// 這里的 deviceId 需要已經通過 createBLEConnection 與對應設備建立鏈接deviceId:this.deviceId,success:(res)=>{console.log('device services:', res)//E95D93AF-251D-470A-A062-FA1922DFA9A8//6E400001-B5A3-F393-E0A9-E50E24DCCA9Eres.services.forEach((item)=>{if(item.uuid.indexOf("470A")!=-1){this.serviceId = item.uuid;console.log(this.serviceId)//獲取特征this.getBLEDeviceCharacteristics()}})}})},1000)},//寫入藍牙writeBLECharacteristicValue() {let buffer = new ArrayBuffer(1) let dataView = new DataView(buffer)dataView.setUint8(0, 0x20)uni.writeBLECharacteristicValue({deviceId: this.deviceId,serviceId: this.serviceId,characteristicId: this.characteristicId,value:buffer,success(res) {console.log('writeBLECharacteristicValue success', res.errMsg)},fail(res) { console.log(JSON.stringify(res))console.log(JSON.stringify(buffer))}})},}} </script><style> </style>

總結

以上是生活随笔為你收集整理的uniapp蓝牙模块demo的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。