uniapp 小程序 蓝牙 兼容Android和IOS 微信小程序和支付宝小程序

ly.js

// import { TextDecoder } from 'text-encoding-utf-8';
let bluetoothOpen = false; // 手机蓝牙是否打开
let bluetoothConnect = false; // 设备和蓝牙是否连接
let isHaveDevice = false; // 是否查找到设备
let deviceId = null; // 设备id
let serviceId = null; // 服务id
let notify = null; // 监听uuid
let writeId = null; // 写入uuid
let timer = null
let time = 0 //时间
/**
 * 获取手机蓝牙是否打开
 */
const getBluetoothState = () => {
	// 主机模式
	return new Promise((resolve, reject) => {
		// mode: 'central',
		uni.openBluetoothAdapter({
			success: (r) => {
				console.log("蓝牙初始化成功");
				// 获取蓝牙的匹配状态
				uni.getBluetoothAdapterState({
					success: function(row) {
						console.log('蓝牙状态:', row.available);
						if (row.available) {
							bluetoothOpen = true;
							getLocation().then(res => {
								console.log('4444');
								resolve();
							}).catch(err => {

								bluetoothOpen = false;
								bluetoothConnect = false;
								reject();
							})

						} else {
							// 请开启蓝牙
							uni.showToast({
								title: '请打开蓝牙',
								icon: 'none'
							})
							bluetoothOpen = false;
							bluetoothConnect = false;

							reject();
						}
					},
					fail: function(err) {
						// 请开启蓝牙
						uni.showToast({
							title: '请打开蓝牙',
							icon: 'none'
						})
						bluetoothOpen = false;
						bluetoothConnect = false;
						reject();
					}
				})
			},
			fail: (err) => {
				// 请开启蓝牙
				console.log('蓝牙初始化失败', err);
				if (err.errno == 103) {
					uni.showModal({
						title: '温馨提示',
						content: '您已拒绝授权,是否去设置打开?',
						confirmText: "确认",
						cancelText: "取消",
						success: function(res) {
							// console.log(res);
							if (res.confirm) {
								uni.openSetting({
									success: (res) => {
										console.log(res, '设置');
										// res.authSetting[scope] = true
										// resolve()
									}
								});
							}
						},

					});
				} else if (err.errCode == 10001) {

					uni.showToast({
						title: '请前往手机设置,打开此APP蓝牙权限',
						icon: 'none'
					})

				} else {

你可能感兴趣的:(小程序,uni-app,android)