AbyssalSwamp  ActivaUser
» Guest:  Register | Login | 冻结用户(激活) | Residents

RSS subscription to this AbyssalSwamp  

Previous thread Next thread
       
Title: 蓝牙打印机 App  
 
chnzbq
妙语书生



UID 181233
Digest 0
Points 7
Posts 1513
码币MB 2815 Code
黄金 731 Catty
钻石 85 Pellet
Permissions 10
Register 2019-5-3
Status online
蓝牙打印机 App

视频如下:没有蓝牙打印机,显示未找到打印机。

Att: __UNI__3240BEB_1220173814.apk (2024-12-21 11:28, 14.07 M)
Number of times this attachment has been downloaded 1


Att: 4952ea902df0cb40d16c1cd2492ee898.mp4 (2024-12-21 11:30, 196.81 K)
Number of times this attachment has been downloaded 1
2024-12-21 09:13#1
View profile  Blog  Send a short message  Top
 
chnzbq
妙语书生



UID 181233
Digest 0
Points 7
Posts 1513
码币MB 2815 Code
黄金 731 Catty
钻石 85 Pellet
Permissions 10
Register 2019-5-3
Status online
总结:
如果打印机支持直接打印 PDF,则可以在前端直接通过蓝牙或其他方式将 PDF 发送到打印机。
如果打印机不支持直接打印 PDF,则可以将 PDF 上传到后台,后台将其转换为图像或其他格式,然后将其发送到打印机进行打印。
2024-12-21 11:51#2
View profile  Blog  Send a short message  Top
 
chnzbq
妙语书生



UID 181233
Digest 0
Points 7
Posts 1513
码币MB 2815 Code
黄金 731 Catty
钻石 85 Pellet
Permissions 10
Register 2019-5-3
Status online
总结:
直接打印:如果打印机支持 Word 文档格式,客户端可以直接将 Word 文件发送给打印机。
后台打印:如果打印机不直接支持 Word 文件,可以将 Word 文件上传到后台,后台将其转换为 PDF 或图像格式,然后发送给打印机进行打印。
常见的工具:
mammoth:将 Word 转换为 HTML。
puppeteer:将 HTML 转换为 PDF。
LibreOffice:通过命令行工具将 Word 转换为 PDF,常用于 PHP 后端。
2024-12-21 11:52#3
View profile  Blog  Send a short message  Top
 
chnzbq
妙语书生



UID 181233
Digest 0
Points 7
Posts 1513
码币MB 2815 Code
黄金 731 Catty
钻石 85 Pellet
Permissions 10
Register 2019-5-3
Status online
以下是 打印 图片的 处理流程:

<template>
  <view class="container">
    <!-- 初始化蓝牙按钮 -->
    <button @click="initBluetooth" :disabled="isBluetoothAvailable">初始化蓝牙</button>

    <!-- 扫描设备按钮 -->
    <button @click="startScan" :disabled="!isBluetoothAvailable || isScanning">扫描设备</button>

    <!-- 显示设备列表 -->
    <view v-if="devices.length > 0">
      <view v-for="(device, index) in devices" :key="index" class="device" @click="selectDevice(device)">
        <text>{{ device.name }} (ID: {{ device.deviceId }})</text>
      </view>
    </view>

    <!-- 连接打印机按钮 -->
    <button @click="connectPrinter" :disabled="!selectedDevice || isConnected">连接打印机</button>

    <!-- 打印图片按钮 -->
    <button @click="printImage" :disabled="!isConnected">打印图片</button>

    <!-- 显示选中的打印机信息 -->
    <view v-if="selectedDevice">
      <text>已选择打印机: {{ selectedDevice.name }}</text>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      isBluetoothAvailable: false, // 蓝牙是否可用
      isConnected: false, // 是否连接打印机
      isScanning: false, // 是否正在扫描设备
      selectedDevice: null, // 存储选中的设备
      devices: [], // 存储扫描到的设备列表
    };
  },
  methods: {
    // 初始化蓝牙
    initBluetooth() {
      uni.openBluetoothAdapter({
        success: (res) => {
          console.log('蓝牙初始化成功', res);
          this.isBluetoothAvailable = true;
          uni.onBluetoothAdapterStateChange((state) => {
            if (!state.available) {
              this.isBluetoothAvailable = false;
              uni.showToast({
                title: '蓝牙不可用',
                icon: 'none',
              });
            }
          });
        },
        fail: (err) => {
          console.log('蓝牙初始化失败', err);
          this.isBluetoothAvailable = false;
          uni.showToast({
            title: '蓝牙初始化失败',
            icon: 'none',
          });
        }
      });
    },

    // 扫描蓝牙设备
    startScan() {
      this.isScanning = true;
      uni.startBluetoothDevicesDiscovery({
        success: (res) => {
          console.log('开始扫描设备', res);
        },
        fail: (err) => {
          console.log('扫描设备失败', err);
          this.isScanning = false;
        }
      });

      // 获取设备列表
      uni.getBluetoothDevices({
        success: (res) => {
          console.log('扫描到设备列表', res.devices);
          this.devices = res.devices;
          this.isScanning = false;
        },
        fail: (err) => {
          console.log('获取设备列表失败', err);
          this.isScanning = false;
        }
      });
    },

    // 用户选择设备
    selectDevice(device) {
      this.selectedDevice = device;
      uni.showToast({
        title: `已选择打印机: ${device.name}`,
        icon: 'none',
      });
    },

    // 连接到选中的蓝牙打印机
    connectPrinter() {
      if (!this.selectedDevice) {
        uni.showToast({
          title: '请选择打印机',
          icon: 'none',
        });
        return;
      }

      uni.createBLEConnection({
        deviceId: this.selectedDevice.deviceId,
        success: (res) => {
          console.log('连接打印机成功', res);
          this.isConnected = true;
          uni.showToast({
            title: '连接打印机成功',
            icon: 'success',
          });
        },
        fail: (err) => {
          console.log('连接打印机失败', err);
          uni.showToast({
            title: '连接打印机失败',
            icon: 'none',
          });
        }
      });
    },

    // 打印图片
    printImage() {
      if (!this.isConnected) {
        uni.showToast({
          title: '未连接打印机',
          icon: 'none',
        });
        return;
      }

      const imagePath = '/static/logo.png'; // 假设要打印的图片路径
      uni.getImageInfo({
        src: imagePath,
        success: (res) => {
          const { path } = res;
          this.sendPrintCommand(path); // 发送打印命令
        },
        fail: (err) => {
          console.log('获取图片信息失败', err);
        }
      });
    },

    // 向打印机发送打印命令
    sendPrintCommand(imagePath) {
      // 假设打印机接受图像二进制数据,发送给蓝牙打印机
      const data = this.imageToBinary(imagePath); // 将图片转为二进制数据
      if (data) {
        uni.writeBLECharacteristicValue({
          deviceId: this.selectedDevice.deviceId,
          serviceId: 'your_service_id',  // 蓝牙服务 ID
          characteristicId: 'your_characteristic_id', // 蓝牙特征 ID
          value: data,  // 发送图像数据
          success: (res) => {
            console.log('打印命令已发送', res);
            uni.showToast({
              title: '打印命令已发送',
              icon: 'success',
            });
          },
          fail: (err) => {
            console.log('发送打印命令失败', err);
            uni.showToast({
              title: '发送打印命令失败',
              icon: 'none',
            });
          }
        });
      } else {
        uni.showToast({
          title: '图片转换失败',
          icon: 'none',
        });
      }
    },

    // 将图片转换为二进制数据(示例代码,具体实现根据打印机协议来)
    imageToBinary(imagePath) {
      // 这里可以使用 canvas 或其他方式将图片转为二进制
      // 假设这里简单返回一个空的 ArrayBuffer
      // 实际的实现需要根据打印机协议转换图像
      console.log('将图片转换为二进制数据', imagePath);
      return new ArrayBuffer();  // 示例返回空的 ArrayBuffer,需根据协议实现
    }
  },

  onUnload() {
    // 确保在页面卸载时关闭蓝牙
    if (this.isConnected) {
      uni.closeBLEConnection({
        deviceId: this.selectedDevice.deviceId,
        success: (res) => {
          console.log('蓝牙连接已断开', res);
        }
      });
    }
    uni.closeBluetoothAdapter();
  }
};
</script>

<style scoped>
.container {
  padding: 20px;
}
button {
  margin: 10px 0;
  padding: 10px;
  background-color: #007AFF;
  color: white;
  border: none;
  border-radius: 5px;
}
.device {
  margin: 10px 0;
  padding: 10px;
  background-color: #f0f0f0;
  border-radius: 5px;
  cursor: pointer;
}
.device:hover {
  background-color: #d0d0d0;
}
</style>
2024-12-21 11:54#4
View profile  Blog  Send a short message  Top
       


  Printable version | Recommend to a friend | Subscribe to topic | Favorite topic  


 


All times are GMT+8, and the current time is 2025-6-27 10:09 Clear informations ->sessions/cookies - Contact Us - CAFFZ - ZAKE