user9088454
user9088454

Reputation: 1122

Bluetooth serial write is not working in Ionic - v4

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

Answers (1)

Indra Saswita
Indra Saswita

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:

  1. And make partial load for providers on home.module.ts
  2. And dont forget, You need to import ngx files from import {BluetoothSerial} from '@ionic-native/bluetooth-serial/ngx';

On home.page.ts:

  1. You need to import 2 things up,
import { PrinterProvider } from './../../providers/printer/printer';
import { commands } from './../../providers/printer/printer-commands';
  1. Use the PrinterProvider like usual, create variable printer with class PrinterProvider on page constructor.
  2. To use you can type awaits this.printer.connectBluetooth(devicemac).subscribe( ...
  3. dont forget to use async function on awaits statement.
  4. And you can use variable 'commands' without type 'this.*' on its prefix.
  5. done..

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)

  1. You need to print "\x0A" for enter.
  2. or you can call commands.FEED_CONTROL_SEQUENCES.CTL_LF which is build earlier from "providers/printer/printer-commands". There some other command you can use and see on that library.

Upvotes: 1

Related Questions