@ruhiverse/thermal-printer-plugin 1.0.8 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,6 +4,8 @@ import android.Manifest;
4
4
  import android.app.Activity;
5
5
  import android.app.AlertDialog;
6
6
  import android.app.PendingIntent;
7
+ import android.bluetooth.BluetoothAdapter;
8
+ import android.bluetooth.BluetoothDevice;
7
9
  import android.content.BroadcastReceiver;
8
10
  import android.content.Context;
9
11
  import android.content.Intent;
@@ -11,6 +13,8 @@ import android.content.IntentFilter;
11
13
  import android.content.pm.PackageManager;
12
14
  import android.hardware.usb.UsbDevice;
13
15
  import android.hardware.usb.UsbManager;
16
+ import android.os.Handler;
17
+ import android.os.Looper;
14
18
  import android.util.Log;
15
19
  import androidx.core.app.ActivityCompat;
16
20
  import androidx.core.content.ContextCompat;
@@ -35,6 +39,8 @@ import com.ruhiverse.thermalprinter.AsyncBluetoothEscPosPrint;
35
39
  import com.ruhiverse.thermalprinter.AsyncEscPosPrint;
36
40
  import com.ruhiverse.thermalprinter.AsyncEscPosPrinter;
37
41
  import com.ruhiverse.thermalprinter.AsyncUsbEscPosPrint;
42
+ import java.util.HashSet;
43
+ import java.util.Set;
38
44
 
39
45
  @CapacitorPlugin(
40
46
  name = "ThermalPrinter",
@@ -60,6 +66,12 @@ public class ThermalPrinterPlugin extends Plugin {
60
66
  public static final int PERMISSION_BLUETOOTH_CONNECT = 3;
61
67
  public static final int PERMISSION_BLUETOOTH_SCAN = 4;
62
68
  private static final String ACTION_USB_PERMISSION = "com.ruhiverse.thermalprinter.USB_PERMISSION";
69
+
70
+ // Bluetooth discovery
71
+ private Set<BluetoothDevice> discoveredDevices = new HashSet<>();
72
+ private PluginCall discoveryCall;
73
+ private BroadcastReceiver discoveryReceiver;
74
+ private Handler discoveryHandler = new Handler(Looper.getMainLooper());
63
75
 
64
76
  private final BroadcastReceiver usbReceiver = new BroadcastReceiver() {
65
77
  public void onReceive(Context context, Intent intent) {
@@ -263,8 +275,13 @@ public class ThermalPrinterPlugin extends Plugin {
263
275
  this.checkBluetoothPermissions(
264
276
  () -> {
265
277
  try {
266
- BluetoothConnection[] bluetoothPrinters = (new BluetoothPrintersConnections()).getList();
267
278
  JSArray printers = new JSArray();
279
+ discoveredDevices.clear();
280
+ discoveryCall = call;
281
+
282
+ // First, get paired printers
283
+ BluetoothConnection[] bluetoothPrinters = (new BluetoothPrintersConnections()).getList();
284
+ Set<String> pairedAddresses = new HashSet<>();
268
285
 
269
286
  if (bluetoothPrinters != null && bluetoothPrinters.length > 0) {
270
287
  for (BluetoothConnection printer : bluetoothPrinters) {
@@ -281,19 +298,19 @@ public class ThermalPrinterPlugin extends Plugin {
281
298
  printerObj.put("name", name);
282
299
  printerObj.put("address", address != null ? address : "");
283
300
  printers.put(printerObj);
301
+ pairedAddresses.add(address);
284
302
 
285
- Log.d(TAG, "Found Bluetooth printer: " + name + " (" + address + ")");
303
+ Log.d(TAG, "Found paired Bluetooth printer: " + name + " (" + address + ")");
286
304
  } catch (Exception e) {
287
305
  Log.e(TAG, "Error processing printer: " + e.getMessage());
288
306
  }
289
307
  }
290
308
  } else {
291
- Log.d(TAG, "No paired Bluetooth printers found. Make sure printers are paired in Android Settings.");
309
+ Log.d(TAG, "No paired Bluetooth printers found. Starting discovery...");
292
310
  }
293
311
 
294
- JSObject ret = new JSObject();
295
- ret.put("printers", printers);
296
- call.resolve(ret);
312
+ // Start Bluetooth discovery for unpaired devices
313
+ startBluetoothDiscovery(printers, pairedAddresses);
297
314
  } catch (Exception e) {
298
315
  Log.e(TAG, "Error listing Bluetooth printers: " + e.getMessage(), e);
299
316
  call.reject("Error listing Bluetooth printers: " + e.getMessage());
@@ -301,6 +318,123 @@ public class ThermalPrinterPlugin extends Plugin {
301
318
  }
302
319
  );
303
320
  }
321
+
322
+ private void startBluetoothDiscovery(JSArray printers, java.util.Set<String> pairedAddresses) {
323
+ BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
324
+
325
+ if (bluetoothAdapter == null) {
326
+ Log.d(TAG, "Bluetooth not supported on this device");
327
+ JSObject ret = new JSObject();
328
+ ret.put("printers", printers);
329
+ if (discoveryCall != null) {
330
+ discoveryCall.resolve(ret);
331
+ discoveryCall = null;
332
+ }
333
+ return;
334
+ }
335
+
336
+ if (!bluetoothAdapter.isEnabled()) {
337
+ Log.d(TAG, "Bluetooth is not enabled");
338
+ JSObject ret = new JSObject();
339
+ ret.put("printers", printers);
340
+ if (discoveryCall != null) {
341
+ discoveryCall.resolve(ret);
342
+ discoveryCall = null;
343
+ }
344
+ return;
345
+ }
346
+
347
+ // Register receiver for discovered devices
348
+ discoveryReceiver = new BroadcastReceiver() {
349
+ @Override
350
+ public void onReceive(Context context, Intent intent) {
351
+ String action = intent.getAction();
352
+ if (BluetoothDevice.ACTION_FOUND.equals(action)) {
353
+ BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
354
+ if (device != null && !pairedAddresses.contains(device.getAddress())) {
355
+ discoveredDevices.add(device);
356
+ String name = device.getName();
357
+ if (name == null || name.isEmpty()) {
358
+ name = "Unknown Device";
359
+ }
360
+ Log.d(TAG, "Discovered Bluetooth device: " + name + " (" + device.getAddress() + ")");
361
+ }
362
+ } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
363
+ // Discovery finished, add discovered devices to results
364
+ JSArray finalPrinters = printers;
365
+ for (BluetoothDevice device : discoveredDevices) {
366
+ try {
367
+ JSObject printerObj = new JSObject();
368
+ String name = device.getName();
369
+ if (name == null || name.isEmpty()) {
370
+ name = "Unknown Device";
371
+ }
372
+ printerObj.put("name", name);
373
+ printerObj.put("address", device.getAddress());
374
+ finalPrinters.put(printerObj);
375
+ Log.d(TAG, "Added discovered device: " + name + " (" + device.getAddress() + ")");
376
+ } catch (Exception e) {
377
+ Log.e(TAG, "Error adding discovered device: " + e.getMessage());
378
+ }
379
+ }
380
+
381
+ // Unregister receiver
382
+ try {
383
+ getContext().unregisterReceiver(discoveryReceiver);
384
+ } catch (Exception e) {
385
+ Log.e(TAG, "Error unregistering receiver: " + e.getMessage());
386
+ }
387
+
388
+ // Return results
389
+ JSObject ret = new JSObject();
390
+ ret.put("printers", finalPrinters);
391
+ if (discoveryCall != null) {
392
+ discoveryCall.resolve(ret);
393
+ discoveryCall = null;
394
+ }
395
+ discoveryReceiver = null;
396
+ }
397
+ }
398
+ };
399
+
400
+ // Register receiver
401
+ IntentFilter filter = new IntentFilter();
402
+ filter.addAction(BluetoothDevice.ACTION_FOUND);
403
+ filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
404
+ getContext().registerReceiver(discoveryReceiver, filter);
405
+
406
+ // Start discovery
407
+ if (bluetoothAdapter.isDiscovering()) {
408
+ bluetoothAdapter.cancelDiscovery();
409
+ }
410
+
411
+ Log.d(TAG, "Starting Bluetooth discovery...");
412
+ boolean started = bluetoothAdapter.startDiscovery();
413
+
414
+ if (!started) {
415
+ Log.d(TAG, "Failed to start Bluetooth discovery");
416
+ try {
417
+ getContext().unregisterReceiver(discoveryReceiver);
418
+ } catch (Exception e) {
419
+ Log.e(TAG, "Error unregistering receiver: " + e.getMessage());
420
+ }
421
+ JSObject ret = new JSObject();
422
+ ret.put("printers", printers);
423
+ if (discoveryCall != null) {
424
+ discoveryCall.resolve(ret);
425
+ discoveryCall = null;
426
+ }
427
+ discoveryReceiver = null;
428
+ } else {
429
+ // Stop discovery after 8 seconds
430
+ discoveryHandler.postDelayed(() -> {
431
+ if (bluetoothAdapter.isDiscovering()) {
432
+ bluetoothAdapter.cancelDiscovery();
433
+ Log.d(TAG, "Discovery timeout, stopping scan");
434
+ }
435
+ }, 8000);
436
+ }
437
+ }
304
438
 
305
439
  @PluginMethod
306
440
  public void listUsbPrinters(PluginCall call) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruhiverse/thermal-printer-plugin",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "Capacitor plugin for thermal printing via USB and Bluetooth",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",