'Ionic 4 not print in thermal bluetooth
I tried to print text in a thermal printer "https://www.amazon.it/gp/product/B096KQ99K1/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1" but nothing appens. I uded a plugin ble Bluetooth Low Energy. The connection ok, list device ok and other method ok and write method return "OK". But don't print nothing. What Can I solve? This is my code: - details page .ts detailsPage.html
import { Component, OnInit, NgZone } from '@angular/core';
import {AlertController, LoadingController, ToastController} from '@ionic/angular';
import { ActivatedRoute, Router } from '@angular/router';
import { BLE } from '@awesome-cordova-plugins/ble/ngx';
// Bluetooth UUIDs
let BLE_SERVICE = ''; //= '180A';
let BLE_CHARACTERISTIC = ''; // '2A29';
@Component({
selector: 'app-details',
templateUrl: './details.page.html',
styleUrls: ['./details.page.scss']
})
export class DetailsPage implements OnInit {
dispositivo;
peripheral: any = {};
statusMessage: string;
public dataFromDevice: any;
constructor(public route: ActivatedRoute,
public router: Router,
private ble: BLE,
private toastCtrl: ToastController,
private alertCtrl: AlertController,
private loadingCtrl: LoadingController,
private ngZone: NgZone) {
this.route.queryParams.subscribe(params => {
if (params && params.special) {
const device = JSON.parse(params.special);
this.dispositivo = device;
}
});
}
async presentLoadingText(macAddress) {
await this.loadingCtrl.create({
message: 'Please wait...'
}).then((res) => {
res.present();
});
this.bleConnect(this.dispositivo);
}
bleConnect(device) {
this.ble.connect(device.id).subscribe(
peripheral => this.onConnected(peripheral),
peripheral => this.onDeviceDisconnected(peripheral)
);
}
bleDisconnect() {
this.ble.disconnect(this.peripheral.id).then(
() => console.log('Disconnected ' + JSON.stringify(this.peripheral)),
() => console.log('ERROR disconnecting ' + JSON.stringify(this.peripheral)));
}
bleWrite() {
const inputdata = new Uint8Array(3);
inputdata[0] = 0x53; // S
inputdata[1] = 0x54; // T
inputdata[2] = 0x0a; // LF
this.ble
.write(
this.peripheral.id,
BLE_SERVICE,
BLE_CHARACTERISTIC,
inputdata.buffer
)
.then(
(data) => {
this.subscribe();
},
err => {
console.log(err);
}
);
}
subscribe() {
console.log('Entro?');
this.ble
.startNotification(this.dispositivo.id, "fff0", "fff2")
.subscribe(
(data) => {
this.onValueChange(data);
},
(err) =>
this.showAlert(
'Unexpected Error',
err
).then(() =>{
this.bleDisconnect();
}),
);
}
onValueChange(buffer: ArrayBuffer) {
console.log('Che fa sto metodo?');
this.ngZone.run(() => {
try {
if (this.dataFromDevice === undefined){
console.log('Dati indefiniti?');
this.dataFromDevice = this.bytesToString(buffer).replace(/\s+/g, ' ');
} else {
console.log('Dati DEFINITI? ' +this.dataFromDevice);
this.dataFromDevice += '<br />' + this.bytesToString(buffer).replace(/\s+/g, ' ');
}
} catch (e) {
console.log(e);
}
});
}
bytesToString(buffer) {
return String.fromCharCode.apply(null, new Uint8Array(buffer));
}
onConnected(peripheral) {
this.loadingCtrl.dismiss();
this.ngZone.run(() => {
this.setStatus('');
this.peripheral = peripheral;
const characteristics = peripheral.characteristics;
BLE_SERVICE = characteristics[5].service;
BLE_CHARACTERISTIC = characteristics[5].characteristic;
this.bleWrite();
});
}
async onDeviceDisconnected(peripheral) {
const toast = await this.toastCtrl.create({
message: 'The peripheral unexpectedly disconnected',
duration: 3000,
position: 'middle'
});
toast.present();
}
setStatus(message) {
console.log(message);
this.ngZone.run(() => {
this.statusMessage = message;
});
}
async showAlert(title, message) {
const alert = await this.alertCtrl.create({
header: title,
message: message,
buttons: ['OK']
});
alert.present();
}
// ASCII only
stringToBytes(str) {
const array = new Uint8Array(str.length);
let i;
let l;
for (i = 0, l = str.length; i < l; i++) {
array[i] = str.charCodeAt(i);
}
return array.buffer;
}
}
HTML
<ion-header>
<ion-toolbar>
<ion-title>{{ peripheral.name || 'Device' }}</ion-title>
</ion-toolbar>
</ion-header>
<ion-content class="padding">
<ion-card>
<ion-card-header>
{{ dispositivo.name || 'Unnamed' }}
</ion-card-header>
<ion-card-content (click)="connectToBluetoothPrinter(dispositivo.id)">
{{ dispositivo.id }}
</ion-card-content>
</ion-card>
</ion-content>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
