This commit is contained in:
XiaoJia Chen 2025-04-15 04:00:02 +08:00
parent 40c821350f
commit e4114251fb
7 changed files with 183 additions and 211 deletions

View File

@ -0,0 +1,7 @@
.space-between {
margin-bottom: 10px;
}
.text-space {
margin-left: 10px; /* 这里添加了左侧间距,你可以根据需要调整值 */
}

View File

@ -1,80 +1,62 @@
<view>
<!-- 蓝牙启动关闭开关 = 初始化蓝牙 关闭本机蓝牙 开关 -->
<view class="page">
<view class="page-description">开关</view>
<view class="page-section">
<view class="page-section-demo switch-list">
<view class="switch-item">
<!-- checked:是否被选中初始状态默认开关 onChangechecked 改变时触发event.detail={value:checked}。 -->
<switch onChange="switchopenBluetoothAdapter" aria-label="{{switchopenBluetoothAdapter ? 'switch opened' : 'switch closed'}}" />
<view class="page">
<view class="page-description"></view>
<view class="page-section">
<view class="page-section-title"></view>
<view class="page-section-demo icon-list">
<block a:for="{{iconType}}">
<view class="item space-between">
<icon type="{{item}}" aria-label="{{item}}" size="20" />
<text> {{selectedDevice.name}}</text>
<text> {{discovering}}</text>
<text> {{available}}</text>
</view>
</view>
</view>
</view>
是否正在搜索设备{{discovering}}
蓝牙模块是否可用需支持 BLE 并且蓝牙是打开状态{{available}}
<!-- 开始搜索 停止搜索开关 开关 -->
<view class="page">
<view class="page-description">开关</view>
<view class="page-section">
<view class="page-section-demo switch-list">
<view class="switch-item">
<!-- checked:是否被选中初始状态默认开关 onChangechecked 改变时触发event.detail={value:checked}。 -->
<switch onChange="startBluetoothDevicesDiscovery" aria-label="{{startBluetoothDevicesDiscovery ? 'switch opened' : 'switch closed'}}" />
</view>
</view>
</block>
</view>
</view>
<view class="page">
<view class="page-description">图标</view>
<view class="page-description"></view>
<view class="page-section">
<view class="page-section-title">Type</view>
<view class="page-section-demo icon-list">
<block a:for="{{iconType}}">
<view class="item">
<icon type="{{item}}" aria-label="{{item}}" size="45" />
<text>{{item}}</text>
<view class="section section_gap">
<form onSubmit="onSubmit" onReset="onReset">
<view class="page-section-demo">
<radio-group class="radio-group" onChange="radioChange" name="lib">
<label a:for="{{devices}}" class="radio" key="label-{{index}}">
<radio
value="{{item.deviceId}}"
checked="{{item.checked}}"
disabled="{{item.disabled}}"
/>
<text class="radio-text">{{item.name}}</text>
</label>
</radio-group>
</view>
</block>
<view class="page-section-btns">
<view>
<button size="mini" type="ghost" formType="reset">待开发</button></view>
<view>
<button size="mini" type="primary" formType="submit">打印</button></view>
</view>
</form>
</view>
</view>
<view class="page">
<view class="page-description">单选框</view>
<view class="page-section">
<view class="section section_gap">
<form onSubmit="onSubmit" onReset="onReset">
<view class="page-section-demo">
<radio-group
class="radio-group"
onChange="radioChange"
name="lib"
>
<label a:for="{{devices}}" class="radio" key="label-{{index}}">
<radio
value="{{item.deviceId}}"
checked="{{item.checked}}"
disabled="{{item.disabled}}"
/>
<text class="radio-text">{{item.name}}</text>
</label>
</radio-group>
</view>
<view class="page-section-btns">
<view>
<button size="mini" type="ghost" formType="reset">待开发</button></view>
<view>
<button size="mini" type="primary" formType="submit">连接并打印</button></view>
</view>
</form>
</view>
</view>
</view>
指令集选择 下拉选择框
蓝牙选择框+选择+打印+清空查找更多蓝牙 devices 单选列表框
已经连接蓝牙名名称文本 文本 状态一行
获取蓝牙状态 有两个标识 打勾 状态一行
</view>
<view class="page-section-demo switch-list">
<view class="switch-item">
<!-- checked:是否被选中初始状态默认开关 onChangechecked 改变时触发event.detail={value:checked}。 -->
启动
<switch onChange="switchopenBluetoothAdapter" aria-label="{{switchopenBluetoothAdapter ? 'switch opened' : 'switch closed'}}" />
搜索
<switch onChange="startBluetoothDevicesDiscovery" aria-label="{{startBluetoothDevicesDiscovery ? 'switch opened' : 'switch closed'}}" />
</view>
</view>
<view class="page">
<view class="page-description"></view>
<view class="page-section">
<picker onChange="bindPickerChange" value="{{index}}" range="{{array}}">
<view class="row">
<view class="row-title">指令集选择</view>
<view class="row-extra">当前选择:{{array[index]}}</view>
</view>
</picker>
</view>
</view>

View File

@ -1,6 +1,9 @@
Component({
mixins: [],
data: {
printdata: {},
array: ['TSPL', 'ZPL', 'CPCL', 'ESC/POS'],
index: 0,
// 初始化蓝牙的开关状态false 表示关闭true 表示打开
switchswitchopenBluetoothAdapter: false,
// 搜索蓝牙设备的开关状态false 表示停止搜索true 表示开始搜索
@ -13,7 +16,6 @@ Component({
deviceslist: [],
// 选择的保持连接的蓝牙对象
devices: [],
devices: {},
selectedDevice: {},
// 存储设备名称,可根据需求调整
devicesName: [],
@ -35,7 +37,12 @@ Component({
// 存储所有服务及其特征值
allServicesAndCharacteristics: {}
},
props: {},
props: {
printdata: {
type: Object,
default: () => ({})
}
},
didMount() {
// 获取本机蓝牙模块的状态
this.getBluetoothAdapterState();
@ -90,10 +97,10 @@ Component({
fail: (res) => {
console.log('获取本机蓝牙模块状态失败,错误码:', res.errorCode);
if (res.errorCode === 10000) {
dd.showToast({
title: '请先打开蓝牙开关',
icon: 'none'
});
// dd.showToast({
// title: '请先打开蓝牙开关',
// icon: 'none'
// });
} else {
// dd.showToast({
// title: `获取本机蓝牙模块状态失败,错误码: ${res.errorCode}`,
@ -107,8 +114,10 @@ Component({
});
},
switchopenBluetoothAdapter(e) {
console.log('Data updated:', this.props.printdata);
console.log('switchopenBluetoothAdapter 发生 change 事件,携带值为', e.detail.value);
// 更新组件数据,将开关的新状态存储到 data 中
console.log('Received printdata:', this.data.printdata);
this.setData({
switchswitchopenBluetoothAdapter: e.detail.value,
});
@ -120,10 +129,10 @@ Component({
autoClose: true,
success: (res) => {
console.log('蓝牙模块初始化成功,是否支持 BLE:', res.isSupportBLE);
dd.showToast({
title: '蓝牙模块初始化成功',
icon: 'success'
});
// dd.showToast({
// title: '蓝牙模块初始化成功',
// icon: 'success'
// });
// 监听手机蓝牙状态的改变
dd.onBluetoothAdapterStateChange((res) => {
console.log('蓝牙适配器状态改变:', res);
@ -169,10 +178,10 @@ Component({
dd.closeBluetoothAdapter({
success: () => {
console.log('蓝牙模块已关闭');
dd.showToast({
title: '蓝牙模块已关闭',
icon: 'success'
});
// dd.showToast({
// title: '蓝牙模块已关闭',
// icon: 'success'
// });
},
fail: (res) => {
console.error('关闭蓝牙模块失败,错误码:', res.errorCode);
@ -205,10 +214,10 @@ Component({
// services: ['fff0'],
success: (res) => {
console.log(res);
dd.showToast({
title: '开始搜索蓝牙设备',
icon: 'success'
});
// dd.showToast({
// title: '开始搜索蓝牙设备',
// icon: 'success'
// });
// 开始搜索后获取已发现设备
this.getDiscoveredDevices();
},
@ -233,10 +242,10 @@ Component({
dd.stopBluetoothDevicesDiscovery({
success: (res) => {
console.log(res);
dd.showToast({
title: '停止搜索蓝牙设备',
icon: 'success'
});
// dd.showToast({
// title: '停止搜索蓝牙设备',
// icon: 'success'
// });
},
fail: (res) => {
console.error('停止搜寻蓝牙设备失败,错误码:', res.errorCode);
@ -349,12 +358,7 @@ Component({
deviceId,
success: (res) => {
console.log('连接蓝牙设备成功', res);
this.setData({
isConnected: true,
iconType: [
'success',
],
});
// const title = '蓝牙设备连接成功';
// if (title) {
// dd.showToast({
@ -364,6 +368,12 @@ Component({
// }
// 获取设备的服务列表
this.getBLEDeviceServices(deviceId);
this.setData({
isConnected: true,
iconType: [
'success',
],
});
},
fail: (res) => {
console.error('连接蓝牙设备失败,错误码:', res.errorCode);
@ -382,12 +392,12 @@ Component({
} else {
console.log('请先选择要连接的蓝牙设备');
const title = '请先选择要连接的蓝牙设备';
if (title) {
dd.showToast({
title,
icon: 'none'
});
}
// if (title) {
// dd.showToast({
// title,
// icon: 'none'
// });
// }
}
},
/**
@ -407,34 +417,34 @@ Component({
} else {
console.error('服务的 UUID 和 serviceId 均为空,无法获取特征值。服务详情:', service);
const title = '服务 UUID 和 serviceId 均为空,无法获取特征值';
if (title) {
dd.showToast({
title,
icon: 'none'
});
}
// if (title) {
// dd.showToast({
// title,
// icon: 'none'
// });
// }
}
});
} else {
console.error('未获取到服务列表');
const title = '未获取到服务列表';
if (title) {
dd.showToast({
title,
icon: 'none'
});
}
// const title = '未获取到服务列表';
// if (title) {
// dd.showToast({
// title,
// icon: 'none'
// });
// }
}
},
fail: (serviceErr) => {
console.error('获取服务失败', serviceErr);
const title = '获取服务失败';
if (title) {
dd.showToast({
title,
icon: 'none'
});
}
// const title = '获取服务失败';
// if (title) {
// dd.showToast({
// title,
// icon: 'none'
// });
// }
}
});
},
@ -470,23 +480,23 @@ Component({
writableCharacteristics: [...this.data.writableCharacteristics, ...writable]
});
console.log('获取特征值成功', characteristicRes);
const title = '获取特征值成功';
if (title) {
dd.showToast({
title,
icon: 'success'
});
}
// const title = '获取特征值成功';
// if (title) {
// dd.showToast({
// title,
// icon: 'success'
// });
// }
},
fail: (characteristicErr) => {
console.error('获取特征值失败', characteristicErr);
const title = '获取特征值失败';
if (title) {
dd.showToast({
title,
icon: 'none'
});
}
// const title = '获取特征值失败';
// if (title) {
// dd.showToast({
// title,
// icon: 'none'
// });
// }
}
});
},
@ -518,13 +528,13 @@ Component({
},
fail: (res) => {
console.error('重新连接蓝牙设备失败,错误码:', res.errorCode);
const title = `重新连接蓝牙设备失败,错误码: ${res.errorCode}`;
if (title) {
dd.showToast({
title,
icon: 'none'
});
}
// const title = `重新连接蓝牙设备失败,错误码: ${res.errorCode}`;
// if (title) {
// dd.showToast({
// title,
// icon: 'none'
// });
// }
this.reconnectBluetoothDevice();
},
complete: (res) => {
@ -563,13 +573,13 @@ Component({
},
fail: (res) => {
console.error('关闭蓝牙连接失败,错误码:', res.errorCode);
const title = `关闭蓝牙连接失败,错误码: ${res.errorCode}`;
if (title) {
dd.showToast({
title,
icon: 'none'
});
}
// const title = `关闭蓝牙连接失败,错误码: ${res.errorCode}`;
// if (title) {
// dd.showToast({
// title,
// icon: 'none'
// });
// }
},
complete: (res) => {
console.log('关闭蓝牙连接调用结束', res);
@ -598,13 +608,13 @@ Component({
} = res;
if (connected) {
console.log(`蓝牙设备 ${deviceId} 连接成功`);
const title = `蓝牙设备 ${deviceId} 连接成功`;
if (title) {
dd.showToast({
title,
icon: 'success'
});
}
// const title = `蓝牙设备 ${deviceId} 连接成功`;
// if (title) {
// dd.showToast({
// title,
// icon: 'success'
// });
// }
this.setData({
isConnected: true,
iconType: [
@ -613,13 +623,13 @@ Component({
});
} else {
console.log(`蓝牙设备 ${deviceId} 连接断开,尝试重新连接`);
const title = `蓝牙设备 ${deviceId} 连接断开,尝试重新连接`;
if (title) {
dd.showToast({
title,
icon: 'none'
});
}
// const title = `蓝牙设备 ${deviceId} 连接断开,尝试重新连接`;
// if (title) {
// dd.showToast({
// title,
// icon: 'none'
// });
// }
this.setData({
isConnected: false,
iconType: [
@ -762,5 +772,5 @@ Component({
sendPrintCode(0);
}
}
},
});

View File

@ -1,3 +1,9 @@
{
"component": true
"component": true,
"properties": {
"printdata": {
"type": "Object",
"value": {}
}
}
}

View File

@ -1,5 +1,4 @@
<view class="nav-container">
<navigator open-type="navigate" url="/pages/deal/saleOrder/saleOrder" hover-class="navigator-hover" class="nav-button">销售</navigator>
<bluetooth-component/>
</view>

View File

@ -1,3 +1,3 @@
<view>
<bluetooth-component printdata="{{printdata}}"/>
</view>

View File

@ -1,46 +1,15 @@
Page({
data: {
switch1: '',
switch2: '',
switch3: '',
switch4: '',
iconType: [
'cancel',
],
// 'success',连接成功打勾
// 'info', 感叹号
// 'warn', 黄色感叹号
// 'waiting',时钟
// 'clear',打叉
// 'success_no_circle',打勾
// 'download',下载
// 'cancel',打叉
// 'search',搜索
array: ['TSPL', 'ZPL', 'CPCL', 'ESC/POS'],
value: {},
printdata: {},
blueoptions: [],
deviceId: '',
connected: false,
devices: [],
showDeviceList: false,
currentDeviceServices: [],
currentDeviceCharacteristics: [],
hasLocation: false,
location: {},
deviceCount: 0,
targetDeviceId: 'DC:0D:30:63:9E:0E',
isSearching: true,
printFormat: 'TSPL',
maxAllowedLength: 1024,
delayTime: 5000, // 默认延迟 5 秒,单位:毫秒
paperSize: {
width: 80, // 默认宽度 80mm
height: 60 // 默认高度 60mm
},
index: 0 //指令集默认选择
// printFormat: 'TSPL',
// maxAllowedLength: 1024,
// delayTime: 5000, // 默认延迟 5 秒,单位:毫秒
// paperSize: {
// width: 80, // 默认宽度 80mm
// height: 60 // 默认高度 60mm
// },
// index: 0 //指令集默认选择
},
onLoad() {
const selectedSaleOrderLines = dd.getStorageSync({
key: 'selectedSaleOrderLines'
@ -50,5 +19,4 @@ Page({
printdata
});
},
});