Reputation: 1122
I am working on a Bluetooth serial plugin. Using this plugin I am able to detect devices and connect(pair) them but after connecting device, I try to send a message is not working. I have already connected(paired) but whenever I try to use the write method is showing Unable to connect
. here the link of git repo: ionic-bluetooth-serial-example
searchBluetooth(){
this.bluetoothSerial.discoverUnpaired().then(response => {
console.log(JSON.stringify(response));
}).catch((error) => {
console.log(JSON.stringify(error));
}
}
deviceConnection(id: string) {
this.bluetoothSerial.connect(id).subscribe((res) => {
console.log(JSON.stringify(res));
}), (error: any) => {
console.log(JSON.stringify(error));
}
}
deviceConnected() {
this.bluetoothSerial.subscribe('\n').subscribe(success => {
alert("Connected Successfullly" + JSON.stringify(success));
}, error => {
alert("error" + JSON.stringify(error));
});
}
sendMessage(): Observable<any> {
return Observable.create(observer => {
this.bluetoothSerial.isConnected().then((isConnected) => {
this.reader = from(this.bluetoothSerial.write("hello, world")).pipe(mergeMap(() => {
return this.bluetoothSerial.subscribeRawData();
})).pipe(mergeMap(() => {
return this.bluetoothSerial.readUntil('\n'); // <= delimitador
}));
this.reader.subscribe(data => {
observer.next(data);
});
}, notConected => {
observer.next('BLUETOOTH.NOT_CONNECTED');
observer.complete();
});
});
}
This is a small part of the example, in the above link he explained much better than this. You can check on a git repository. Please help...
Upvotes: 2
Views: 1654
Reputation: 11
You can download providers folder from Luccas repository.
The example, can be shown on pages/home.ts (it build on v3 ionic) you should change some code on that, for example:
import {BluetoothSerial} from '@ionic-native/bluetooth-serial/ngx';
On home.page.ts
:
import { PrinterProvider } from './../../providers/printer/printer';
import { commands } from './../../providers/printer/printer-commands';
awaits this.printer.connectBluetooth(devicemac).subscribe( ...
It will print after you use Enter
There are 2 things are the same to call Enter (\n
or <br>
cannot be use on escpos printer)
"\x0A"
for enter.Upvotes: 1