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"> <view class="page-section">
<view class="page-description">开关</view> <view class="page-section-title"></view>
<view class="page-section"> <view class="page-section-demo icon-list">
<view class="page-section-demo switch-list"> <block a:for="{{iconType}}">
<view class="switch-item"> <view class="item space-between">
<!-- checked:是否被选中初始状态默认开关 onChangechecked 改变时触发event.detail={value:checked}。 --> <icon type="{{item}}" aria-label="{{item}}" size="20" />
<switch onChange="switchopenBluetoothAdapter" aria-label="{{switchopenBluetoothAdapter ? 'switch opened' : 'switch closed'}}" /> <text> {{selectedDevice.name}}</text>
<text> {{discovering}}</text>
<text> {{available}}</text>
</view> </view>
</view> </block>
</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>
</view> </view>
</view> </view>
<view class="page"> <view class="page">
<view class="page-description">图标</view> <view class="page-description"></view>
<view class="page-section"> <view class="page-section">
<view class="page-section-title">Type</view> <view class="section section_gap">
<view class="page-section-demo icon-list"> <form onSubmit="onSubmit" onReset="onReset">
<block a:for="{{iconType}}"> <view class="page-section-demo">
<view class="item"> <radio-group class="radio-group" onChange="radioChange" name="lib">
<icon type="{{item}}" aria-label="{{item}}" size="45" /> <label a:for="{{devices}}" class="radio" key="label-{{index}}">
<text>{{item}}</text> <radio
value="{{item.deviceId}}"
checked="{{item.checked}}"
disabled="{{item.disabled}}"
/>
<text class="radio-text">{{item.name}}</text>
</label>
</radio-group>
</view> </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> </view>
<view class="page"> </view>
<view class="page-description">单选框</view> <view class="page-section-demo switch-list">
<view class="page-section"> <view class="switch-item">
<view class="section section_gap"> <!-- checked:是否被选中初始状态默认开关 onChangechecked 改变时触发event.detail={value:checked}。 -->
<form onSubmit="onSubmit" onReset="onReset"> 启动
<view class="page-section-demo"> <switch onChange="switchopenBluetoothAdapter" aria-label="{{switchopenBluetoothAdapter ? 'switch opened' : 'switch closed'}}" />
<radio-group 搜索
class="radio-group" <switch onChange="startBluetoothDevicesDiscovery" aria-label="{{startBluetoothDevicesDiscovery ? 'switch opened' : 'switch closed'}}" />
onChange="radioChange" </view>
name="lib" </view>
> <view class="page">
<label a:for="{{devices}}" class="radio" key="label-{{index}}"> <view class="page-description"></view>
<radio <view class="page-section">
value="{{item.deviceId}}" <picker onChange="bindPickerChange" value="{{index}}" range="{{array}}">
checked="{{item.checked}}" <view class="row">
disabled="{{item.disabled}}" <view class="row-title">指令集选择</view>
/> <view class="row-extra">当前选择:{{array[index]}}</view>
<text class="radio-text">{{item.name}}</text> </view>
</label> </picker>
</radio-group> </view>
</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>

View File

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

View File

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

View File

@ -1,46 +1,15 @@
Page({ Page({
data: { 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: {}, printdata: {},
blueoptions: [], // printFormat: 'TSPL',
deviceId: '', // maxAllowedLength: 1024,
connected: false, // delayTime: 5000, // 默认延迟 5 秒,单位:毫秒
devices: [], // paperSize: {
showDeviceList: false, // width: 80, // 默认宽度 80mm
currentDeviceServices: [], // height: 60 // 默认高度 60mm
currentDeviceCharacteristics: [], // },
hasLocation: false, // index: 0 //指令集默认选择
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 //指令集默认选择
}, },
onLoad() { onLoad() {
const selectedSaleOrderLines = dd.getStorageSync({ const selectedSaleOrderLines = dd.getStorageSync({
key: 'selectedSaleOrderLines' key: 'selectedSaleOrderLines'
@ -50,5 +19,4 @@ Page({
printdata printdata
}); });
}, },
}); });