Uniapp调用native.js使用经典蓝牙串口通讯方法及问题解决

本人尝试在uniapp环境下开发一款安卓应用,需要与使用经典蓝牙协议的设备进行串口通讯,而uniapp官方给出的蓝牙操作接口目前只支持BLE(低功耗蓝牙),用该接口无法正常获取到我想要连接的设备。

通过大量搜索,终于在gitee平台找到了想要的实现(核心是BluetoothTool.jshttps://gitee.com/booltrue/bluetooth-printer/blob/master/static/libs/BluetoothTool.js文件)

,整个资源参见:BluetoothPrinter: 使用uniapp开发 的蓝牙打印demo,可打印图片,二维码,文字,线条等 仅支持:小程序、android。暂时不支持ioshttps://gitee.com/booltrue/bluetooth-printer

经过不断测试,基本实现了想要的功能,通过真机测试能够正常使用,在打包为apk后,发现无法搜索到蓝牙设备,经过若干天的搜索,终于在上述开源代码的讨论中找到了解决方法,参见:nullhttps://gitee.com/booltrue/bluetooth-printer/issues/I5OX92

主要问题解决代码如下:

javascript 复制代码
// native.js广播搜索前需调用uni.startBluetoothDevicesDiscovery();
// 否则真机运行没问题但打包后可能会搜不到蓝牙
uni.startBluetoothDevicesDiscovery({
	success(res) {
		console.log(res);
		bluetoothTool.discoveryNewDevice();
	}
})

特此记录一下,希望后来者能够闭坑!!!

相关推荐
叱咤少帅(少帅)1 天前
Uniapp开发pc端,小程序和APK
小程序·uni-app
2501_915918412 天前
iOS性能测试工具 Instruments、Keymob的使用方法 不局限 FPS
android·ios·小程序·https·uni-app·iphone·webview
2501_915918412 天前
iOS 混淆流程 提升 IPA 分析难度 实现 IPA 深度加固
android·ios·小程序·https·uni-app·iphone·webview
前端 贾公子2 天前
解决uni-app 输入框,键盘弹起时页面整体上移问题
前端·vue.js·uni-app
Muchen灬2 天前
【uniapp】(5) 创建gitee仓库并推送源码
gitee·uni-app
Muchen灬2 天前
【uniapp】(6) uniapp中使用vuex
uni-app
2501_915909062 天前
React Native 上架 App Store:项目运行与审核构建的流程
android·ios·小程序·https·uni-app·iphone·webview
李庆政3702 天前
uniapp+unicloud打包部署微信小程序
微信小程序·小程序·uni-app
2501_915909062 天前
苹果 Safari 浏览器抓包 页面刷新后的请求分析
android·前端·ios·小程序·uni-app·iphone·safari