Nilam
Nilam

Reputation: 1

tharmal printer print using @capacitor-community/bluetooth-le in ionic capacitor android SDK 33

tharmal printer print using @capacitor-community/bluetooth-le

using this plugin how to connect tharmal printer in ionic capacito for android adk 33

I try tharmal printer print using capacitor Bluetooth is able to connect but also send data to connected Bluetooth tharmal printer device but it's not able to print.

Upvotes: 0

Views: 712

Answers (1)

Nilam
Nilam

Reputation: 1

import { BluetoothLe } from '@capacitor-community/bluetooth-le';
 async  connectToBluetoothDevice() {
    debugger
    try {
     
      await BluetoothLe.initialize();
      const isEnabled = await BluetoothLe.isEnabled();
      if (!isEnabled) {
      console.log('Bluetooth is not enabled.');
        return;
      }
      
      const devices = await BluetoothLe.requestDevice({});
      console.log('devices=>',devices)
      const printerDeviceId = devices.deviceId; 
      const bluetoothDeviceId = 'your_bluetooth_device_id'; 
      const data = new Uint8Array([0x48, 0x65, 0x6C, 0x6C, 0x6F]);
      const text = 'Hello';
      const encoder = new TextEncoder();
      const uintArray4 = encoder.encode(text);
      this.sendDataViaBluetooth(devices.deviceId,uintArray4)
      

    } catch (error) {
      console.error('Error connecting to Bluetooth device:', error);
    }
  }
  async  sendDataViaBluetooth(deviceId: string, dataToSend: any) {
    debugger
    try {
      let sendData;
      sendData = new DataView(dataToSend.buffer);
      console.log('Data sent successfully via Bluetooth.');
    } catch (error) {
      console.error('Error sending data via Bluetooth:', error);
    }
  }

Upvotes: -1

Related Questions