Skip to content

Commit

Permalink
Add device ID to NiimbotCapacitorBleClient options
Browse files Browse the repository at this point in the history
  • Loading branch information
MultiMote committed Nov 17, 2024
1 parent c4b78a2 commit fe8d527
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/client/capacitor_ble_impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ import { ConnectResult } from "../packets";
import { Utils } from "../utils";
import { BleCharacteristic, BleClient, BleDevice, BleService } from "@capacitor-community/bluetooth-le";

interface NiimbotCapacitorBleClientConnectOptions {
/**
* Skip device picker dialog and connect to given device ID.
*
* On **Android** this is the BLE MAC address.
*
* On **iOS** and **web** it is an identifier.
*/
deviceId?: string;
}

/**
* Uses [@capacitor-community/bluetooth-le](https://github.com/capacitor-community/bluetooth-le)
*
Expand All @@ -22,7 +33,7 @@ export class NiimbotCapacitorBleClient extends NiimbotAbstractClient {
private characteristicUUID?: string;
private packetBuf = new Uint8Array();

public async connect(): Promise<ConnectionInfo> {
public async connect(options?: NiimbotCapacitorBleClientConnectOptions): Promise<ConnectionInfo> {
await this.disconnect();

await BleClient.initialize({ androidNeverForLocation: true });
Expand All @@ -33,7 +44,16 @@ export class NiimbotCapacitorBleClient extends NiimbotAbstractClient {
throw new Error("Bluetooth is not enabled");
}

const device: BleDevice = await BleClient.requestDevice();

This comment has been minimized.

Copy link
@talaviram

talaviram Dec 29, 2024

Is there a reason the Capacitor BLE has no "filtering" while the web one does?

It can easily become very long list:
Image

This comment has been minimized.

Copy link
@MultiMote

MultiMote Dec 29, 2024

Author Owner

Because some printers have different service UUID. See this: MultiMote/niimblue#36

Later I will add configurable uuid parameters instead of hardcoded. And capacitor client filter as well.

This comment has been minimized.

Copy link
@MultiMote

MultiMote Jan 12, 2025

Author Owner

Unfortunately, seems like there are problems with filtering printers with service UUID. NIIMBOT printer do not advertising services.

capacitor-community/bluetooth-le#645

let device: BleDevice;

if (options?.deviceId !== undefined) {
device = {
deviceId: options.deviceId,
name: options.deviceId,
};
} else {
device = await BleClient.requestDevice();
}

await BleClient.connect(device.deviceId, () => this.onBleDisconnect());

Expand Down

0 comments on commit fe8d527

Please sign in to comment.