uniApp開發微信小程序-連接藍牙連接打印機上岸!

歷經波折三次成功上岸!

三次經歷簡單絮叨一下:使用uniApp+vue開發的微信小程序,使用藍牙連接打印機,藍牙所有的接口都是插件中封裝的,用的插件市場中的這個: dothan-lpapi-ble ;所以,開發中,主要是看插件文檔中使用的API。

首次上線

情況說明: 首次上線微信小程序:直接初始化實例,調用的插件API,測試環境成功連接打印,就上線了。

結果

客戶手機一直搜索不到打印機,根據經驗發現沒執行實例的代碼;
想到授權藍牙也沒自動彈框提示,小程序設置中也沒有藍牙開啟設置;

問題定位:

那就問 deepseeek 開干唄!
1. 可能是用戶手機 鴻蒙系統 問題:在系統設置中找到應用管理,找到微信,然后打開微信的權限管理,查看 附近設備的權限 - 開啟(反復開關)。

插件市場遇到的特殊情況
2. 顯示授權:必要的權限聲明
3. 檢查manifest.json配置
4. 微信小程序發版后臺 - 設置 - 基本設置 - 服務內容聲明 - 用戶隱私保護指引 - 添加(藍牙、微信信息收集)

二次上線

情況說明: 二次上線微信小程序:提交審核是其他同事在做,正常提交

結果

客戶手機一直搜索不到打印機,根據經驗發現沒執行實例的代碼;
線上環境用戶還是沒有觸發授權藍牙、位置信息提示;
審核沒通過

問題定位:

1. 提交審核時勾選了:未采集用戶隱私
2. 親自提審、加圖片、視頻輔佐

三次上線

情況說明:審核通過

結果

線上成功授權、成功搜索打印機!

成功版本代碼

manifest.json配置

"mp-weixin": {"appid": "...",//  添加..."permission": {"scope.bluetooth": {"desc": "用于連接藍牙設備"},"scope.userLocation": {"desc": "你的位置信息將用于藍牙設備連接"}},"requiredPrivateInfos": ["getLocation"]},

授權提示 - deepseek友情提供

// 使用-獲取藍牙權限
await this.bringBluetoothRight();// 方法 methods: 
bringBluetoothRight() {let _this = this;return new Promise((resolve, reject) => {uni.getSetting({success: async (res) => {try {console.log(res, 'res')// 1. 同時檢查藍牙和位置權限const needBluetooth = !res.authSetting['scope.bluetooth'];const needLocation = !res.authSetting['scope.userLocation'];if (!needBluetooth && !needLocation) {// 已有全部權限return resolve(await _this.wechatBluetoothAuthUtils());}// 2. 顯示預授權提示(合并說明)await _this.showPreAuthorizeModal('需要藍牙和位置權限','連接打印機需要以下權限:\n\n1. 藍牙權限 - 用于設備配對\n2. 位置權限 - 用于搜索設備');// 3. 按順序請求權限if (needBluetooth) {await _this.requestPermission('scope.bluetooth', '藍牙');}if (needLocation) {await _this.requestPermission('scope.userLocation', '位置');}// 4. 驗證權限獲取結果const currentSettings = await _this.getSettingWithRetry();console.log(currentSettings, 'currentSettings')if ((!needBluetooth || currentSettings['scope.bluetooth']) &&(!needLocation || currentSettings['scope.userLocation'])) {console.log('.......')resolve(await _this.wechatBluetoothAuthUtils());} else {console.log('1111111')// 5. 有權限未被授予,引導去設置頁await _this.showGoSettingModal();const finalSettings = await _this.getSettingWithRetry();if ((!needBluetooth || finalSettings['scope.bluetooth']) &&(!needLocation || finalSettings['scope.userLocation'])) {resolve(await _this.wechatBluetoothAuthUtils());} else {reject(new Error('權限未完全授予'));}}} catch (error) {reject(error);}},fail: reject});});
},
// 新增:帶重試的獲取設置
getSettingWithRetry(retry = 2) {return new Promise((resolve, reject) => {const tryGet = (attempt) => {uni.getSetting({success: resolve,fail: attempt > 0 ?() => setTimeout(() => tryGet(attempt - 1), 500) : reject});};tryGet(retry);});
},// 改進:通用權限請求方法
requestPermission(scope, permissionName) {let _this = this;// 添加最大重試次數控制const MAX_RETRY = 1; // 最多重試1次(即總共2次機會)return new Promise((resolve, reject) => {const tryAuthorize = (retryCount = 0) => {uni.authorize({scope,success: resolve,fail: async () => {// 第一次拒絕時顯示提示,后續直接引導去設置頁if (retryCount < MAX_RETRY) {const confirmed = await _this.showPreAuthorizeModal(`${permissionName}權限被拒絕`,`需要${permissionName}權限才能連接打印機,是否重新授權?`,'重新授權','取消');if (confirmed) {tryAuthorize(retryCount + 1); // 增加重試計數return;}}// 達到最大重試或用戶取消,引導去設置頁const goSetting = await _this.showPreAuthorizeModal('權限不足',`需要${permissionName}權限才能使用打印機功能,是否去設置頁開啟?`,'去設置','暫不開啟');if (goSetting) {uni.openSetting({success: (res) => {res.authSetting[scope] ? resolve() :reject(new Error(`用戶未授權${permissionName}權限`));},fail: () => reject(new Error('打開設置頁失敗'))});} else {reject(new Error(`用戶取消${permissionName}權限授權`));}}});};tryAuthorize(); // 開始首次授權嘗試});
},
// 改進:預授權彈窗(返回Promise<boolean>)
showPreAuthorizeModal(title, content) {return new Promise((resolve) => {uni.showModal({title,content,confirmText: '繼續',cancelText: '取消',success: (res) => {resolve(!!res.confirm);},fail: () => resolve(false)});});
},// 改進:去設置頁彈窗
showGoSettingModal() {return this.showPreAuthorizeModal('需要權限','需要前往設置頁手動開啟權限,是否現在去設置?').then((confirm) => {if (confirm) {return new Promise((resolve) => {uni.openSetting({success: resolve,fail: resolve});});}return Promise.reject(new Error('用戶取消設置'));});
},wechatBluetoothAuthUtils() {let _this = this;const {bluetoothEnabled,platform} = uni.getSystemInfoSync();console.log(bluetoothEnabled,platform)// 設備為IOS時,微信藍牙是否開啟if (platform === 'ios') {return new Promise((resolve, reject) => {// 初始化藍牙模塊(用openBluetoothAdapter 方法解決部分ios設備,授權藍牙失敗的問題)uni.openBluetoothAdapter({success: () => {// 開啟藍牙功能 =》 初始化拾果sdkresolve(true);},fail: (openBlueFail) => {if (openBlueFail.state === 3) {// 說明微信應用藍牙未授權uni.showModal({content: '檢測到您未允許微信訪問手機藍牙權限,是否打開系統設置?',showCancel: false,confirmText: '前往設置',success: () => {// 跳轉微信應用權限uni.openAppAuthorizeSetting();},});} else if (openBlueFail.state === 4) {// 說明系統藍牙未開啟uni.showModal({content: '藍牙設置 - 小程序需要通過藍牙搜索和連接設備,請確認手機藍牙功能是否已開啟?',showCancel: false,confirmText: '我已開啟',success: async () => {const {bluetoothEnabled,platform} = uni.getSystemInfoSync();if (bluetoothEnabled) {// 開啟藍牙功能resolve(true);} else {uni.showToast({icon: 'none',title: '手機藍牙未開啟'})}},});}},});});} else {return new Promise(function(resolve, reject) {// andriodif (!bluetoothEnabled) {// 說明系統藍牙未開啟uni.showModal({content: '藍牙設置 - 小程序需要通過藍牙搜索和連接設備,請確認手機藍牙功能是否已開啟?',showCancel: false,confirmText: '我已開啟',success: async () => {const {bluetoothEnabled,platform} = uni.getSystemInfoSync();if (bluetoothEnabled) {// 開啟藍牙功能resolve(true);} else {toast({title: '手機藍牙未開啟'});}},});} else {// 開啟藍牙功能resolve(true);}});}
},

額外關閉提示:

<button class="search-btn" type="default" @click="toggleLocationTracking">{{ isTrackingLocation ? '關閉位置追蹤' : '開啟位置追蹤' }}</button>// data
isTrackingLocation:false// methods
toggleLocationTracking() {if (this.isTrackingLocation) {this.stopLocationTracking();} else {this.startLocationTracking();}},
startLocationTracking() {uni.authorize({scope: 'scope.userLocation',success: () => {this.isTrackingLocation = true;uni.showToast({title: '已開啟位置服務',icon: 'success'});},fail: () => {uni.showModal({title: '提示',content: '需要位置權限才能搜索藍牙設備',confirmText: '去設置',success: (res) => {if (res.confirm) uni.openSetting();}});}});},
stopLocationTracking() {uni.getSetting({success: (res) => {if (res.authSetting['scope.userLocation']) {uni.showModal({title: '確認',content: '確定要關閉位置服務嗎?關閉后將無法搜索新設備',success: (res) => {if (res.confirm) {// 實際微信小程序無法直接關閉權限,引導用戶去設置頁uni.openSetting({success: () => {this.isTrackingLocation = false;}});}}});}}});},

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/77473.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/77473.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/77473.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

軟件系統安全設計方案,信息化安全建設方案(Word原件)

1.1 總體設計 1.1.1 設計原則 1.2 物理層安全 1.2.1 機房建設安全 1.2.2 電氣安全特性 1.2.3 設備安全 1.2.4 介質安全措施 1.3 網絡層安全 1.3.1 網絡結構安全 1.3.2 劃分子網絡 1.3.3 異常流量管理 1.3.4 網絡安全審計 1.3.5 網絡訪問控制 1.3.6 完…

wsl2+ubuntu22.04安裝blenderproc教程

本章教程,介紹如何在windows操作系統上通過wsl2+Ubuntu22.04上安裝blenderproc。 一、pipi安裝方式 推薦使用minconda3安裝Python環境。 pip install Blenderproc二、源碼安裝 1、下載源碼 git clone https://github.com/DLR-RM/BlenderProc2、安裝依賴 cd BlenderProc &am…

Blender 轉 STL 文件全攻略:從基礎到進階

在 3D 建模與打印領域&#xff0c;Blender 憑借其強大的功能和開源特性&#xff0c;深受創作者喜愛。而 STL 文件格式&#xff0c;作為 3D 打印行業的通用標準&#xff0c;能被絕大多數 3D 打印軟件和設備所識別。因此&#xff0c;將 Blender 模型轉換為 STL 文件&#xff0c;是…

Ansys Electronics 變壓器 ACT

你好&#xff0c; 在本博客中&#xff0c;我將討論如何使用 Ansys 電子變壓器 ACT 自動快速地設計電力電子電感器或變壓器。我將逐步介紹設計和創建電力電子變壓器示例的步驟&#xff0c;該變壓器為同心組件&#xff0c;雙繞組&#xff0c;采用正弦電壓激勵&#xff0c;并應用…

nacos配置達夢數據庫驅動源代碼步驟

1.在父工程pom.xml添加依賴&#xff1a; <dependency><groupId>com.dameng</groupId><artifactId>DmJdbcDriver18</artifactId><version>8.1.1.193</version> </dependency> 2.在nacos-config模塊pom.xml添加依賴&#xff1…

4.9-4.10學習總結 Stream流練習+方法引用+異常

Stream流練習&#xff1a; 1.打印數組內的偶數。 import java.util.*; import java.util.function.BiConsumer; public class test {public static void main(String[] args) {ArrayList<Integer> listnew ArrayList<>();Collections.addAll(list,1,2,3,4,5,6,7,…

FPGA系統開發板調試過程不同芯片的移植步驟介紹

目錄 1.我目前使用的開發板 2.不同開發板的移植 步驟一&#xff1a;芯片型號設置 步驟二&#xff1a;約束修改 步驟三、IP核更新 關于FPGA系統開發板調試過程中不同芯片的移植。我需要先理清楚FPGA開發中移植到不同芯片的一般流程。首先&#xff0c;移植通常涉及到更換FPG…

復現QGIS-MCP教程

由于Claude國內下載不了嘗試使用Cursor 下載安裝Cursor Cursor - The AI Code Editor 本示例安裝的是0.46版本 UV安裝 簡介 安裝 安裝成功 配置環境變量 驗證 下載代碼 git clone gitgithub.com:jjsantos01/qgis_mcp.git QGIS插件安裝 文件拷貝 您需要將 qgis_mcp_plu…

java筆記03

基本數據類型 數據值是存儲在自己的空間中。 特點&#xff1a;賦值給其他變量&#xff0c;也是賦的真實的值。 引用數據類型 數據值是存儲在其他空間中&#xff0c;自己空間中存儲的是地址值。 特點&#xff1a;賦值給其他變量&#xff0c;賦的地址值。 綜合練習 使用 ctrl…

【開發工具】快速自定義圖標元素的顏色

如果你想要一個輕量級、簡單易用 的小工具來快速自定義圖標元素的顏色&#xff08;比如調整 SVG/PNG 圖標的顏色&#xff0c;或者生成多色圖標&#xff09;&#xff0c;可以試試以下工具&#xff1a; 1. 在線工具&#xff08;無需安裝&#xff09; SVG/PNG 圖標改色 - Recol…

【CompletableFuture】異步編程

CompletableFuture異步編程 CompletableFuture介紹與傳統 Future 的對比使用方法1. 使用 supplyAsync&#xff08;有返回值&#xff09;使用 runAsync&#xff08;無返回值&#xff09;指定自定義線程池 處理異步結果1. thenApply&#xff1a;轉換結果2.thenAccept&#xff1a;…

【TS學習】(23)理解類的雙重角色

在 TypeScript 中&#xff0c;類&#xff08;class&#xff09;不僅是一個運行時的值&#xff08;即可以實例化對象的構造函數&#xff09;&#xff0c;同時也是一個類型聲明。具體來說&#xff0c;類在 TypeScript 中既聲明了值&#xff0c;也聲明了類型&#xff0c;并且它的類…

IAP Firmware Upload Tools.exe IAP 網絡固件升級教程

IAP是In Application Programming的簡寫&#xff0c;IAP升級可以被視為固件升級的一種形式,它是一種在應用程序運行過程中對固件進行更新的技術手段。允許MCU在運行過程中對MCU User Flash的部分區域進行燒寫,目的是為了代替編程器對MCU燒錄的依賴。 主程序UI 軟件按鈕說明&a…

Uniapp當中的async/await的作用

一、原始代碼的行為&#xff08;使用 async/await&#xff09; const getUserMessagePlan async () > {// 等待兩個異步操作完成const tabsList await message.getTagesList(); // 等待獲取標簽列表const tagsStateList await message.getTagsStateList(); // 等…

設計模式 Day 5:夯實觀察者模式(Boost 實戰精講)

今天我們繼續深入觀察者模式的學習&#xff0c;不再局限于手寫的抽象結構&#xff0c;而是聚焦于真實項目中如何使用成熟框架&#xff08;如 Boost.Signals2&#xff09;高效落地觀察者模式。 本篇采用**“理論解析 問答講解 實戰用例”**結構&#xff0c;幫助你從設計思想到…

設計模式 Day 3:抽象工廠模式(Abstract Factory Pattern)詳解

經過前兩天的學習&#xff0c;我們已經掌握了單例模式與工廠方法模式&#xff0c;理解了如何控制實例個數與如何通過子類封裝對象的創建邏輯。 今天&#xff0c;我們將進一步深入“工廠”體系&#xff0c;學習抽象工廠模式&#xff08;Abstract Factory Pattern&#xff09;&a…

MySQL:事務的理解

一、CURD不加控制&#xff0c;會有什么問題 &#xff08;1&#xff09;因為&#xff0c;MySQL里面存的是數據&#xff0c;所以很有可能會被多個客戶訪問&#xff0c;所以mysqld可能一次會接受到多個關于CURD的請求。&#xff08;2&#xff09;且mysql內部是采用多線程來完成數…

藍橋杯刷題--寶石組合

在一個神秘的森林里&#xff0c;住著一個小精靈名叫小藍。有一天&#xff0c;他偶然發現了一個隱藏在樹洞里的寶藏&#xff0c;里面裝滿了閃爍著美麗光芒的寶石。這些寶石都有著不同的顏色和形狀&#xff0c;但最引人注目的是它們各自獨特的 “閃亮度” 屬性。每顆寶石都有一個…

DAY06:【pytorch】圖像增強

1、基本概念 數據增強&#xff0c;又稱數據增廣、數據擴增&#xff0c;是對訓練集進行變換&#xff0c;使訓練集更豐富&#xff0c;從而讓模型更具泛化能力 2、裁剪 — — Crop 2.1 transforms.CenterCrop 功能&#xff1a;從圖像中心裁剪圖片 size&#xff1a;所需裁剪圖…

mysql 禁止 讀 某個 表

mysql 禁止 讀 某個 表 mysql禁用某張表,禁用MySQL表的操作 https://shuyeidc.com/wp/89479.html MySQL嚴格禁止讀取表如何避免數據泄露 https://www.kdun.cn/ask/394700.html select host,user from mysql.user; FLUSH PRIVILEGES; 1. MySQL嚴格禁止讀取表如何避免數據泄露…