国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

微信小程序藍(lán)牙連接 uniApp藍(lán)牙連接設(shè)備

這篇具有很好參考價值的文章主要介紹了微信小程序藍(lán)牙連接 uniApp藍(lán)牙連接設(shè)備。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

?藍(lán)牙列表期待效果

微信小程序藍(lán)牙連接 uniApp藍(lán)牙連接設(shè)備,筆記

?代碼

<template>
	<view class="bluetooth-list">
		<view class="align-items option" style="justify-content: space-between;" v-for="item in bluetoothList" :key="item.deviceId">
			<view class="">
				<view class="title">{{item.name || item.localName}}</view>
				<view class="desc">{{item.deviceId}}</view>
			</view>
			<view class="bind-btn" @click="onBind(item)">
				綁定設(shè)備
			</view>
		</view>
	</view>
</template>

?js里面注意getBLEDeviceCharacteristics獲取特征值的時候,極個別設(shè)備參數(shù)write,read,notify是亂來的,需要自己打單獨處理,通過對應(yīng)write,read,notify 為true的時候拿到對應(yīng)的uuid,

微信小程序藍(lán)牙連接 uniApp藍(lán)牙連接設(shè)備,筆記文章來源地址http://www.zghlxwxcb.cn/news/detail-766000.html

<script>
	export default {
		data() {
			return {
				bluetoothObj:{},
				bluetoothList:[],
				
				
				services: [],
				serviceId: 0,
				writeCharacter: false,
				readCharacter: false,
				notifyCharacter: false
			};
		},
		onLoad() {
			this.init()
		},
		onUnload() {
			//停止搜索藍(lán)牙設(shè)備
			if (this.isSearching) {
				uni.stopBluetoothDevicesDiscovery();
			}
		},
		methods: {
			// 初始化
			init(){
				let that = this;
				uni.openBluetoothAdapter({
					success(res) {
						uni.getBluetoothAdapterState({
							success(res2) {
								if (res2.available) {
									if (res2.discovering) {
										uni.showToast({
											title: '正在搜索附近打印機設(shè)備',
											icon: "none"
										})
										return;
									}
									//獲取藍(lán)牙設(shè)備信息
									that.getBluetoothDevices()
								} else {
									uni.showModal({
										title: '提示',
										content: '本機藍(lán)牙不可用',
									})
								}
							}
						});
					},
					fail() {
						uni.showModal({
							title: '提示',
							content: '藍(lán)牙初始化失敗,請打開藍(lán)牙',
						})
					}
				})
			},
			
			//獲取藍(lán)牙設(shè)備信息
			getBluetoothDevices() {
				let that = this
				that.bluetoothList = [];
				uni.startBluetoothDevicesDiscovery({
					success(res) {
						//藍(lán)牙設(shè)備監(jiān)聽 uni.onBluetoothDeviceFound
						uni.onBluetoothDeviceFound((result) => {
							let arr = that.bluetoothList;
							let devices = [];
							let list = result.devices;
							for (let i = 0; i < list.length; ++i) {
								if (list[i].name && list[i].name != "未知設(shè)備") {
									let arrNew = arr.filter((item) => {
										return item.deviceId == list[i].deviceId;
									});
									// console.log('arrNew:',arrNew.length)
									if (arrNew.length == 0) {
										devices.push(list[i]);
									}
								}
							}
			
							that.bluetoothList = arr.concat(devices);
							console.log("bluetoothList",that.bluetoothList)
						});
						that.time = setTimeout(() => {
							// uni.getBluetoothDevices
							uni.getBluetoothDevices({
								success(res2) {
									let devices = [];
									let list = res2.devices;
									for (let i = 0; i < list.length; ++i) {
										if (list[i].name && list[i].name != "未知設(shè)備") {
											devices.push(list[i]);
										}
									}
									that.bluetoothList = devices;
								},
							})
							clearTimeout(that.time);
						}, 3000);
					}
				});
			
			},
			
			
			// 綁定藍(lán)牙
			onBind(item){
				uni.stopBluetoothDevicesDiscovery();
				 let that = this;
				 let { deviceId } = item;
				 console.log('item',item)
				 that.bluetoothObj.deviceId = deviceId;
				 that.serviceId = 0;
				 that.writeCharacter = false;
				 that.readCharacter = false;
				 that.notifyCharacter = false;
				 // uni.showLoading({
				 // 	title: '正在連接',
				 // })
				uni.openBluetoothAdapter({
					 success: function () {
						uni.createBLEConnection({
							deviceId,
							success(res) {
								console.log('createBLEConnection success', res)
								uni.hideLoading()
								that.getSeviceId()
							},
							fail(e) {
								console.log('createBLEConnection fail', e)
								uni.hideLoading()
							}
						})
					 },
					 fail: function (error) {
						console.log("openBluetoothAdapter")
					 }
				})
				
			},
			
			//獲取藍(lán)牙設(shè)備所有服務(wù)(service)。
			getSeviceId() {
				let that = this;
				let t=setTimeout(()=>{
					uni.getBLEDeviceServices({
						deviceId: that.bluetoothObj.deviceId,
						success(res) {
							console.log('getBLEDeviceServices success', res)
							that.services = res.services;
							that.getCharacteristics()
						},
						fail: function(e) {
						}
					})
					clearTimeout(t);
				},1500)
			},
			
			
			getCharacteristics() {
				var that = this
				let {
					services: list,
					serviceId: num,
					writeCharacter: write,
					readCharacter: read,
					notifyCharacter: notify
				} = that;
				// uni.getBLEDeviceCharacteristics
				uni.getBLEDeviceCharacteristics({
					deviceId: that.bluetoothObj.deviceId,
					serviceId: list[num].uuid,
					success(res) {
						console.log('getBLEDeviceCharacteristics success', res)
						// console.log(res)
						for (var i = 0; i < res.characteristics.length; ++i) {
							var properties = res.characteristics[i].properties
							var item = res.characteristics[i].uuid
							if (!notify) {
								if (properties.notify) {
									that.bluetoothObj.notifyCharaterId = item;
									that.bluetoothObj.notifyServiceId = list[num].uuid;
									notify = true
								}
							}
							if (!write) {
								if (properties.write) {
									that.bluetoothObj.writeCharaterId = item;
									that.bluetoothObj.writeServiceId = list[num].uuid;
									write = true
								}
							}
							if (!read) {
								if (properties.read) {
									that.bluetoothObj.readCharaterId = item;
									that.bluetoothObj.readServiceId = list[num].uuid;
									read = true
								}
							}
						}
						if (!write || !notify || !read) {
							num++
							that.writeCharacter = write;
							that.readCharacter = read;
							that.notifyCharacter = notify;
							that.serviceId = num;
							if (num == list.length) {
								uni.showModal({
									title: '提示',
									content: '找不到該讀寫的特征值',
								})
							} else {
								that.getCharacteristics()
							}
						} else {
							// ok 
							// wx.writeBLECharacteristicValue
							uni.notifyBLECharacteristicValueChange({
							  state: true, // 啟用 notify 功能
							  // 這里的 deviceId 需要已經(jīng)通過 createBLEConnection 與對應(yīng)設(shè)備建立鏈接
							  deviceId:that.bluetoothObj.deviceId,
							  // 這里的 serviceId 需要在 getBLEDeviceServices 接口中獲取
							  serviceId:that.bluetoothObj.serviceId,
							  // 這里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中獲取
							  characteristicId:that.bluetoothObj.notifyServiceId,
							  success (res) {
								console.log('notifyBLECharacteristicValueChange success', res.errMsg)
								  uni.onBLECharacteristicValueChange(function (e) {
								    /**對設(shè)備發(fā)送過來的參數(shù)進(jìn)行解密 */
								    let str = that.ab2hex(e.value);
									console.log("解密str",str)
								  })
							  }
							})
							console.log("that.bluetoothObj",that.bluetoothObj)
							uni.setStorageSync("bluetoothObj", that.bluetoothObj)
							uni.showToast({
								icon:"none",
								title:"綁定成功"
							})
							setTimeout(()=>{
								uni.navigateBack({
									delta:1
								})
							},1000)
						}
					},
					fail: function(e) {
						console.log("getBLEDeviceCharacteristics fail:",e);
					}
				})
			},
			
			ab2hex: (buffer) => {
			  const hexArr = Array.prototype.map.call(
			    new Uint8Array(buffer),
			    function (bit) {
			      return ('00' + bit.toString(16)).slice(-2)
			    }
			  )
			  return hexArr.join('')
			},
			
			
			str2ab:(str) => {
			  var buf = new ArrayBuffer(str.length / 2);
			  var bufView = new Uint8Array(buf);
			  for (var i = 0, strLen = str.length; i < strLen; i++) {
			    bufView[i] = parseInt(str.slice(i * 2, i * 2 + 2), 16);
			  }
			  return buf;
			}
		}
	}
</script>

<style lang="less" scoped>
	.bluetooth-list{
			background-color: #F3F3F3;
		.option{
			margin: 20rpx;
			padding: 20rpx 32rpx;
			background-color: #fff;
			border-radius: 20rpx;
			.title{
				font-weight: 600;
				font-size: 32rpx;
			}
			.desc{
				font-size: 28rpx;
				color: #999;
				margin-top: 12rpx;
			}
			.bind-btn{
				background-color: #3F96DB;
				color: #fff;
				width: 200rpx;
				height: 70rpx;
				line-height: 70rpx;
				border-radius: 35rpx;
				text-align: center;
				font-size: 30rpx;
				
			}
		}
	}
</style>

到了這里,關(guān)于微信小程序藍(lán)牙連接 uniApp藍(lán)牙連接設(shè)備的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 微信小程序——實現(xiàn)藍(lán)牙設(shè)備搜索及連接功能

    微信小程序——實現(xiàn)藍(lán)牙設(shè)備搜索及連接功能

    ?作者簡介:2022年 博客新星 第八 。熱愛國學(xué)的Java后端開發(fā)者,修心和技術(shù)同步精進(jìn)。 ??個人主頁:Java Fans的博客 ??個人信條:不遷怒,不貳過。小知識,大智慧。 ??當(dāng)前專欄:微信小程序?qū)W習(xí)分享 ?特色專欄:國學(xué)周更-心性養(yǎng)成之路 ??本文內(nèi)容:微信小程序——實

    2024年02月08日
    瀏覽(26)
  • uniapp微信小程序連接藍(lán)牙打印機 打印文字、圖片

    uniapp微信小程序連接藍(lán)牙打印機 打印文字、圖片

    首先感謝幾位的文章分享 https://blog.csdn.net/guairena/article/details/127941515 https://blog.csdn.net/qq_37970097/article/details/119148707 效果圖: 使用的是 芝柯cc3 藍(lán)牙打印機, 我這里沒有存儲藍(lán)牙設(shè)備相關(guān)信息。所以每次打印都會重新初始化并搜索設(shè)備,儲存相關(guān)的代碼下面也有,所以代碼部分

    2024年02月13日
    瀏覽(31)
  • 保姆級微信小程序?qū)铀{(lán)牙設(shè)備教程。微信小程序發(fā)送不同藍(lán)牙指令(定時發(fā)送,斷開重連,判斷是否有藍(lán)牙權(quán)限等)

    本文是一個完整的對接設(shè)備,發(fā)送不同指令監(jiān)聽不同返回的完整示例,可根據(jù)實際項目按需更改。 注: app.showModal 為在app.js中封裝的showModal方法, then(()={}) 代表用戶點擊 confirm ,可用 wx.showModal 代替。 公用方法 請求設(shè)備列表 1. 判斷是否有藍(lán)牙權(quán)限 2. 初始化藍(lán)牙 wx.openBluet

    2024年03月20日
    瀏覽(38)
  • 微信小程序?qū)崿F(xiàn)藍(lán)牙開鎖、開門、開關(guān)、指令發(fā)送成功,但藍(lán)牙設(shè)備毫無反應(yīng)、坑

    wx聯(lián)系本人獲取源碼(開源): MJ682517 需要從下往上閱讀,使用函數(shù)自調(diào)的方式解決 API 不能及時獲取數(shù)據(jù)的問題,替換方案是使用定時器,但是個人覺得定時器不好,所以使用了函數(shù)自調(diào)的方式實現(xiàn)獲取不到數(shù)據(jù)的問題。 getBluetoothDevices 方法中獲取 deviceId ; getBLEDeviceServices 方法

    2024年02月15日
    瀏覽(35)
  • 微信小程序 - 藍(lán)牙連接

    微信小程序 - 藍(lán)牙連接

    官網(wǎng)?藍(lán)牙 (Bluetooth) | 微信開放文檔 ???????藍(lán)牙低功耗是從藍(lán)牙?4.0?起支持的協(xié)議,與經(jīng)典藍(lán)牙相比,功耗極低、傳輸速度更快,但傳輸數(shù)據(jù)量較小。常用在對續(xù)航要求較高且只需小數(shù)據(jù)量傳輸?shù)母鞣N智能電子產(chǎn)品中,比如智能穿戴設(shè)備、智能家電、傳感器等,應(yīng)用場景

    2024年02月05日
    瀏覽(39)
  • 微信小程序之藍(lán)牙連接全過程封裝

    1、初始化藍(lán)牙 不管是ios操作系統(tǒng)還是安卓操作系統(tǒng),第一步都需要初始化藍(lán)牙 2、獲取藍(lán)牙適配器狀態(tài) 3、ios和安卓的操作系統(tǒng)對藍(lán)牙的連接方式不同 安卓是直接對設(shè)備的macAddress進(jìn)行連接 ios需要對周邊的藍(lán)牙設(shè)備就行搜索: 4、藍(lán)牙連接 5、獲取藍(lán)牙多個service 6、開啟notif

    2024年02月15日
    瀏覽(26)
  • Uniapp連接藍(lán)牙設(shè)備

    Uniapp連接藍(lán)牙設(shè)備

    一、效果圖 二、流程圖 三、實現(xiàn) UI

    2024年02月12日
    瀏覽(23)
  • 微信小程序通過藍(lán)牙連接ESP32控制LED燈

    微信小程序通過藍(lán)牙連接ESP32控制LED燈

    本文主要基于網(wǎng)上已有的代碼以及官方給定示例代碼進(jìn)行修改。如有不妥請指出,謝謝啦。 據(jù)我了解,微信小程序只能通過低功耗藍(lán)牙(BLE)進(jìn)行控制。 BLE藍(lán)牙部分設(shè)置流程(通過該程序就能讓esp32廣播藍(lán)牙,同時手機也可搜索到藍(lán)牙設(shè)備): // 獲取藍(lán)牙接收的數(shù)據(jù)與處理

    2024年02月04日
    瀏覽(41)
  • 微信小程序連接藍(lán)牙漢印HM-A300L標(biāo)簽打印機

    微信小程序連接藍(lán)牙漢印HM-A300L標(biāo)簽打印機

    需求: 以下文章針對打印一講解,打印二的代碼放在最后。 打印一 打印二 參考文章: 微信小程序?qū)崿F(xiàn)藍(lán)牙打印 打印機CPCL編程參考手冊(CPCL 語言) 藍(lán)牙打印機CPCL編程手冊~漢印HM-A300 無用小知識: A300系列:先將打印機關(guān)機然后裝好紙,同時按住屏幕左右兩邊的按鍵不放,

    2024年01月18日
    瀏覽(28)
  • 小程序藍(lán)牙通訊設(shè)備數(shù)據(jù)對接實戰(zhàn)uniapp

    小程序藍(lán)牙通訊設(shè)備數(shù)據(jù)對接實戰(zhàn)uniapp

    ? ? ? 最近很閑,但是行業(yè)很卷!因為公司有硬件設(shè)備對接,但是介于原生app。閑來無事,便研究了下這個小程序通過藍(lán)牙與硬件設(shè)備進(jìn)行通訊。廢話少說上干貨! 本次講解的目錄大致分為三模塊。根據(jù)我寫的代碼做講解。 初始化并搜索藍(lán)牙 獲取并啟用service服務(wù) 數(shù)據(jù)讀取

    2024年02月09日
    瀏覽(27)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包