H5使用蓝牙打印机
2022-03-29 本文已影响0人
小雄_Milo
navigator.bluetooth.requestDevice({
filters: [{ services: ['000018f0-0000-1000-8000-00805f9b34fb'] }]
})
.then(device => {
console.log('Connecting to GATT Server...');
// device.addEventListener('gattserverdisconnected', this._disconnect.bind(this));
return device.gatt.connect();
})
.then(server => {
console.log(server)
return server.getPrimaryService("000018f0-0000-1000-8000-00805f9b34fb")
}
)
.then(service => {
console.log(service)
return service.getCharacteristic("00002af1-0000-1000-8000-00805f9b34fb")
}
)
.then(characteristic => {
console.log(characteristic)
//读数据
// console.log("value:",characteristic.readValue())
//监听数据
// characteristic.addEventListener(
// 'characteristicvaluechanged', e => {
// console.log("characteristicvaluechanged:",e.target.value)
// }
// );
// characteristic.startNotifications();
//写数据
var command = tsc.jpPrinter.createNew();
command.setSize(57, 40);
command.setGap(2); //传感器
command.setCls();//清除打印机缓存
command.setDensity(1);//浓度
command.setText(30, 100, 'TSS24.BF2', 1, 1, '蓝牙打印机连接成功');
command.setText(30, 150, 'TSS24.BF2', 1, 1, new Date().toTimeString());
command.setQR(30, 200, 'L', 5, 'A', 'www.sjkjgz.com');
command.setPagePrint();
var data = Array.from(command.getData());
var buffer = new ArrayBuffer(data.length);
var dataView = new DataView(buffer);
for (var j = 0; j < data.length; j++) {
dataView.setUint8(j, data[j]);
}
characteristic.writeValue(dataView)
})
.catch(error => {
console.log('error: ' + error);
});
image.png
image.png