机型等参数
- HSPOS
- 点密度:576点/行(8dots/mm,203dpi)
- 接口类型: 蓝牙(Bluetooth2.0,4.0双模,支持Android,IOS)
- 打印方式:图形打印(位图)
- 打印指令集: ESC/POS
基本思路
1、 实现蓝牙连接
**B12.js方法封装
class BluetoothTools { constructor() { this.decimalData = []; //获取buffer之前的二进制数据集合;this.deviceId = null;this.initStatus = 0; //蓝牙初始化结果;0:初始化中,1-成功;2-失败;this.linked = false; //蓝牙是否为连接状态;this.connectChangeHandler = null;}// 初始化;init(connectChangeHandler) { this.connectChangeHandler = connectChangeHandler;return new Promise((resolve, reject) => { uni.openBluetoothAdapter({ success: res => { this.initStatus = 1;this.onConnectChange(connectChangeHandler)resolve(res)},fail: err => { this.initStatus = 2;console.log('蓝牙初始化失败:', err);this.toast('蓝牙不可用!')reject(err)}})})}// 获取蓝牙状态,是否可用checkCanUse() { return new Promise((resolve, reject) => { uni.getBluetoothAdapterState({ success: res => { // console.log(res);resolve(res)},fail: err => { // console.log(err);reject(err)}})})} // 开始搜索蓝牙startSearch(cb) { return new Promise(async (resolve, reject) => { // if (this.initStatus === 2) { // this.toast('蓝牙初始化失败!');// reject();// return false;// } else if (this.initStatus === 0) { // this.toast('蓝牙未初始化!');// reject();// return false;// }if (this.initStatus !== 1) { await this.init(this.connectChangeHandler);}this.onFound(cb);uni.startBluetoothDevicesDiscovery({ allowDuplicatesKey: false,services: [],success: (res) => { // console.log('开始搜索蓝牙设备!', res);resolve(res)},fail: err => { // console.log('开启搜索失败:', err);this.toast('开始搜索失败!')reject(err)}})})}// 监听单一设备搜索结果onFound(cb) { uni.onBluetoothDeviceFound(res => { // console.log('蓝牙设备:', res.devices);cb(res.devices) })}// 停止搜索;stopSearch() { return new Promise((resolve, reject) => { uni.stopBluetoothDevicesDiscovery({ success: (res) => { resolve(res)},fail: (err) => { // this.toast('蓝牙停止搜索失败,请重试!');console.log('蓝牙停止搜索失败:', err);reject(err)}})})}// 关闭蓝牙模块(打印完后调用)closeAdapter() { return new Promise((resolve, reject) => { uni.closeBluetoothAdapter({ success: res => { resolve(res);},fail: (err) => { console.log('关闭失败:', err);// this.toast('蓝牙模块关闭失败!')reject(err);}})})}toast(msg) { uni.showToast({ title: msg,icon: 'none',duration: 2200})}// 根据deviceId连接蓝牙;connectBLE(deviceId) { return new Promise(async (resolve, reject) => { if (this.initStatus !== 1) { await this.init(this.connectChangeHandler).catch(err => { reject(err)});}this.checkCanUse().then(async data => { // console.log(data);if (data.available) { uni.createBLEConnection({ deviceId: deviceId,success: (res) => { resolve(res)},fail: err => { console.log('蓝牙连接失败!', err);this.toast('蓝牙连接失败,请重试!')reject(err)}})} else { this.toast('蓝牙不可用')reject()}}).catch(err => { this.toast('蓝牙不可用!!')reject()})})}// 根据deviceId断开蓝牙链接;closeBLE(deviceId) { return new Promise((resolve, reject) => { if (!deviceId) { resolve();return;}uni.closeBLEConnection({ deviceId: deviceId,success: (res) => { // console.log('蓝牙已断开!', res);resolve(res)},fail: err => { console.log('蓝牙断开失败!', err);// this.toast('蓝牙断开失败, 请重试!')reject(err)}})})}// 监听蓝牙连接状态;onConnectChange(cb) { uni.onBLEConnectionStateChange(res => { console.log('监听蓝牙连接状态:', res);if (res.connected) { this.linked = true;this.deviceId = res.deviceId;} else { this.linked = false;this.deviceId = null