angularcapacitorandroid-bluetooth

Capacitor error with @capacitor-community/bluetooth-le


I'm developing print feature for my app using these tech stacks:

"@angular/core": "~13.2.0",
"@capacitor-community/bluetooth-le": "^3.1.4",
"@capacitor/android": "^5.7.4",
"@capacitor/angular": "^2.0.3",
"@capacitor/cli": "^5.7.4",
"@capacitor/core": "^5.7.4",
"@capacitor/ios": "^5.7.4",
...

android/app/src/main/AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
    <uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
    <uses-feature android:name="android.hardware.bluetooth_le" android:required="false" />

app.component.ts

    ...
    async VerifyAndEnabled() {
        console.log("VerifyAndEnabled");
        if (!await BleClient.isEnabled()) {
            await BleClient.enable();
        }
    }

    async Initialize() {
        console.log("Initialize");
        await BleClient.initialize({ androidNeverForLocation: true });
    }

    async Disconnect(deviceId: string) {
        console.log("Disconnect");
        await BleClient.disconnect(deviceId);
    }

    deviceId: any;
    serviceId: any;
    characteristicUuid: any;

    async Scan() {
        console.log("Scan");
        await this.Initialize();
        await this.VerifyAndEnabled();

        console.log("requestDevice");
        let bleDevice = await BleClient.requestDevice({ allowDuplicates: false });
        if (!bleDevice) {
            console.error("failed Connected");
        }

        console.log("Connected to deviceId", bleDevice.deviceId);

        await BleClient.connect(bleDevice.deviceId, this.Disconnect);
        this.deviceId = bleDevice.deviceId;
        await this.AssignServices();
    }

    async AssignServices() {
        let bleService: BleService[] = await BleClient.getServices(this.deviceId);
        if (bleService.length > 0 && bleService[0].characteristics.length > 0) {
            this.serviceId = bleService[0].uuid;
            this.characteristicUuid = bleService[0].characteristics[0].uuid;
        }
    }

I followed this article: https://medium.com/@sunilsvsnlr/printing-short-bills-using-bluetooth-thermal-printer-with-ionic-capacitor-c399272a8dfb

It worked for the browser, but I got error when running in Android device:

Uncaught (in promise): Error: "BluetoothLe" plugin is not implemented on android

Can someone help to fix the issue?

Thank you so much for your attention and participation.


Solution

  • I found the answer myself.

    Remove BluetoothLE plugin from capacitor.config.ts or capacitor.config.json resolve the problem although author mentioned it here:

    https://www.npmjs.com/package/@capacitor-community/bluetooth-le/v/3.1.4

    {
      "...": "other configuration",
      "plugins": {
        "BluetoothLe": { // Remove this solved my problem
          "displayStrings": {
            "scanning": "Am Scannen...",
            "cancel": "Abbrechen",
            "availableDevices": "Verfügbare Geräte",
            "noDeviceFound": "Kein Gerät gefunden"
          }
        }
      }
    }