@ruhiverse/thermal-printer-plugin 1.0.7 → 1.0.8

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.
@@ -265,18 +265,37 @@ public class ThermalPrinterPlugin extends Plugin {
265
265
  try {
266
266
  BluetoothConnection[] bluetoothPrinters = (new BluetoothPrintersConnections()).getList();
267
267
  JSArray printers = new JSArray();
268
- if (bluetoothPrinters != null) {
268
+
269
+ if (bluetoothPrinters != null && bluetoothPrinters.length > 0) {
269
270
  for (BluetoothConnection printer : bluetoothPrinters) {
270
- JSObject printerObj = new JSObject();
271
- printerObj.put("name", printer.getDevice().getName());
272
- printerObj.put("address", printer.getDevice().getAddress());
273
- printers.put(printerObj);
271
+ try {
272
+ JSObject printerObj = new JSObject();
273
+ String name = printer.getDevice().getName();
274
+ String address = printer.getDevice().getAddress();
275
+
276
+ // Handle null/empty names
277
+ if (name == null || name.isEmpty()) {
278
+ name = "Unknown Device";
279
+ }
280
+
281
+ printerObj.put("name", name);
282
+ printerObj.put("address", address != null ? address : "");
283
+ printers.put(printerObj);
284
+
285
+ Log.d(TAG, "Found Bluetooth printer: " + name + " (" + address + ")");
286
+ } catch (Exception e) {
287
+ Log.e(TAG, "Error processing printer: " + e.getMessage());
288
+ }
274
289
  }
290
+ } else {
291
+ Log.d(TAG, "No paired Bluetooth printers found. Make sure printers are paired in Android Settings.");
275
292
  }
293
+
276
294
  JSObject ret = new JSObject();
277
295
  ret.put("printers", printers);
278
296
  call.resolve(ret);
279
297
  } catch (Exception e) {
298
+ Log.e(TAG, "Error listing Bluetooth printers: " + e.getMessage(), e);
280
299
  call.reject("Error listing Bluetooth printers: " + e.getMessage());
281
300
  }
282
301
  }
@@ -288,18 +307,37 @@ public class ThermalPrinterPlugin extends Plugin {
288
307
  try {
289
308
  UsbConnection[] usbPrinters = (new UsbPrintersConnections(getContext())).getList();
290
309
  JSArray printers = new JSArray();
291
- if (usbPrinters != null) {
310
+
311
+ if (usbPrinters != null && usbPrinters.length > 0) {
292
312
  for (UsbConnection printer : usbPrinters) {
293
- JSObject printerObj = new JSObject();
294
- printerObj.put("name", printer.getDevice().getDeviceName());
295
- printerObj.put("address", printer.getDevice().getSerialNumber()); // Using serial number as address for USB
296
- printers.put(printerObj);
313
+ try {
314
+ JSObject printerObj = new JSObject();
315
+ String name = printer.getDevice().getDeviceName();
316
+ String serial = printer.getDevice().getSerialNumber();
317
+
318
+ // Handle null/empty names
319
+ if (name == null || name.isEmpty()) {
320
+ name = "USB Printer";
321
+ }
322
+
323
+ printerObj.put("name", name);
324
+ printerObj.put("address", serial != null ? serial : printer.getDevice().getDeviceId() + "");
325
+ printers.put(printerObj);
326
+
327
+ Log.d(TAG, "Found USB printer: " + name + " (ID: " + printer.getDevice().getDeviceId() + ")");
328
+ } catch (Exception e) {
329
+ Log.e(TAG, "Error processing USB printer: " + e.getMessage());
330
+ }
297
331
  }
332
+ } else {
333
+ Log.d(TAG, "No USB printers found. Make sure printer is connected via USB.");
298
334
  }
335
+
299
336
  JSObject ret = new JSObject();
300
337
  ret.put("printers", printers);
301
338
  call.resolve(ret);
302
339
  } catch (Exception e) {
340
+ Log.e(TAG, "Error listing USB printers: " + e.getMessage(), e);
303
341
  call.reject("Error listing USB printers: " + e.getMessage());
304
342
  }
305
343
  }
@@ -283,22 +283,27 @@ public class ThermalPrinterPlugin: CAPPlugin, CBCentralManagerDelegate, CBPeriph
283
283
  }
284
284
 
285
285
  @objc func listBluetoothPrinters(_ call: CAPPluginCall) {
286
+ print("ThermalPrinter: listBluetoothPrinters called")
286
287
  var printers: [[String: String]] = []
287
288
 
288
289
  // First, get MFi/ExternalAccessory printers
289
290
  let manager = EAAccessoryManager.shared()
290
291
  let accessories = manager.connectedAccessories
291
292
 
293
+ print("ThermalPrinter: Found \(accessories.count) ExternalAccessory devices")
294
+
292
295
  let mfiPrinters: [[String: String]] = accessories.compactMap { accessory in
293
296
  guard accessory.protocolStrings.contains(where: { supportedPrinterProtocols.contains($0) }) else {
294
297
  return nil
295
298
  }
299
+ print("ThermalPrinter: Found MFi printer: \(accessory.name)")
296
300
  return [
297
301
  "name": accessory.name,
298
302
  "address": accessory.serialNumber.isEmpty ? "\(accessory.connectionID)" : accessory.serialNumber,
299
303
  ]
300
304
  }
301
305
  printers.append(contentsOf: mfiPrinters)
306
+ print("ThermalPrinter: Found \(mfiPrinters.count) MFi printers")
302
307
 
303
308
  // Then scan for BLE printers
304
309
  discoveredPeripherals.removeAll()
@@ -306,30 +311,40 @@ public class ThermalPrinterPlugin: CAPPlugin, CBCentralManagerDelegate, CBPeriph
306
311
 
307
312
  // Initialize central manager if needed
308
313
  if centralManager == nil {
314
+ print("ThermalPrinter: Initializing CBCentralManager")
309
315
  centralManager = CBCentralManager(delegate: self, queue: DispatchQueue.main)
310
316
  }
311
317
 
312
318
  guard let centralManager = centralManager else {
319
+ print("ThermalPrinter: Central manager is nil, returning \(printers.count) printers")
313
320
  call.resolve(["printers": printers])
314
321
  return
315
322
  }
316
323
 
324
+ print("ThermalPrinter: Bluetooth state: \(centralManager.state.rawValue)")
325
+
317
326
  // Check Bluetooth state
318
327
  switch centralManager.state {
319
328
  case .poweredOn:
320
329
  // Start scanning immediately
330
+ print("ThermalPrinter: Bluetooth powered on, starting scan")
321
331
  startBLEScan(for: call, existingPrinters: printers)
322
332
  case .poweredOff:
333
+ print("ThermalPrinter: Bluetooth powered off, returning \(printers.count) printers")
323
334
  call.resolve(["printers": printers])
324
335
  case .unauthorized:
336
+ print("ThermalPrinter: Bluetooth unauthorized")
325
337
  call.reject("Bluetooth access denied. Please enable Bluetooth permissions in Settings.")
326
338
  case .unsupported:
339
+ print("ThermalPrinter: Bluetooth unsupported")
327
340
  call.reject("Bluetooth is not supported on this device.")
328
341
  case .resetting, .unknown:
342
+ print("ThermalPrinter: Bluetooth state unknown/resetting, waiting for update")
329
343
  // Wait for state update
330
344
  // The state will be updated via centralManagerDidUpdateState
331
345
  break
332
346
  @unknown default:
347
+ print("ThermalPrinter: Unknown Bluetooth state, returning \(printers.count) printers")
333
348
  call.resolve(["printers": printers])
334
349
  }
335
350
  }
@@ -345,28 +360,32 @@ public class ThermalPrinterPlugin: CAPPlugin, CBCentralManagerDelegate, CBPeriph
345
360
  return
346
361
  }
347
362
 
363
+ // Clear previous discoveries
364
+ discoveredPeripherals.removeAll()
365
+
348
366
  // Scan for all peripherals
367
+ print("ThermalPrinter: Starting BLE scan...")
349
368
  centralManager.scanForPeripherals(withServices: nil, options: [CBCentralManagerScanOptionAllowDuplicatesKey: false])
350
369
 
351
- // Stop scanning after 5 seconds
352
- DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
370
+ // Stop scanning after 8 seconds (increased from 5 to give more time)
371
+ DispatchQueue.main.asyncAfter(deadline: .now() + 8.0) {
353
372
  centralManager.stopScan()
373
+ print("ThermalPrinter: Stopped BLE scan. Found \(self.discoveredPeripherals.count) peripherals")
354
374
 
355
375
  var printers = existingPrinters
356
376
 
357
- // Add discovered BLE printers - return all devices with names
377
+ // Add discovered BLE printers - return all devices, even without names
358
378
  let blePrinters: [[String: String]] = self.discoveredPeripherals.compactMap { peripheral in
359
- // Return all peripherals with names, not just those matching keywords
360
- // This allows users to see all Bluetooth devices and select their printer
361
- guard let name = peripheral.name, !name.isEmpty else {
362
- return nil
363
- }
379
+ // Include all peripherals, even if they don't have names
380
+ // Some printers don't advertise names until connected
381
+ let name = peripheral.name ?? "Unknown Device"
364
382
  return [
365
383
  "name": name,
366
384
  "address": peripheral.identifier.uuidString,
367
385
  ]
368
386
  }
369
387
 
388
+ print("ThermalPrinter: Returning \(blePrinters.count) BLE printers")
370
389
  printers.append(contentsOf: blePrinters)
371
390
  call.resolve(["printers": printers])
372
391
  self.listPrintersCall = nil
@@ -376,10 +395,13 @@ public class ThermalPrinterPlugin: CAPPlugin, CBCentralManagerDelegate, CBPeriph
376
395
  // MARK: - CBCentralManagerDelegate
377
396
 
378
397
  public func centralManagerDidUpdateState(_ central: CBCentralManager) {
398
+ print("ThermalPrinter: Central manager state updated: \(central.state.rawValue)")
399
+
379
400
  // If we're waiting to list printers, start scanning now
380
401
  if let call = listPrintersCall {
381
402
  switch central.state {
382
403
  case .poweredOn:
404
+ print("ThermalPrinter: Bluetooth now powered on, starting scan")
383
405
  // Get existing MFi printers first
384
406
  let manager = EAAccessoryManager.shared()
385
407
  let accessories = manager.connectedAccessories
@@ -394,15 +416,19 @@ public class ThermalPrinterPlugin: CAPPlugin, CBCentralManagerDelegate, CBPeriph
394
416
  }
395
417
  startBLEScan(for: call, existingPrinters: mfiPrinters)
396
418
  case .poweredOff:
419
+ print("ThermalPrinter: Bluetooth powered off")
397
420
  call.resolve(["printers": []])
398
421
  listPrintersCall = nil
399
422
  case .unauthorized:
423
+ print("ThermalPrinter: Bluetooth unauthorized")
400
424
  call.reject("Bluetooth access denied. Please enable Bluetooth permissions in Settings.")
401
425
  listPrintersCall = nil
402
426
  case .unsupported:
427
+ print("ThermalPrinter: Bluetooth unsupported")
403
428
  call.reject("Bluetooth is not supported on this device.")
404
429
  listPrintersCall = nil
405
430
  default:
431
+ print("ThermalPrinter: Bluetooth state: \(central.state.rawValue)")
406
432
  break
407
433
  }
408
434
  }
@@ -412,6 +438,8 @@ public class ThermalPrinterPlugin: CAPPlugin, CBCentralManagerDelegate, CBPeriph
412
438
  // Avoid duplicates
413
439
  if !discoveredPeripherals.contains(where: { $0.identifier == peripheral.identifier }) {
414
440
  discoveredPeripherals.append(peripheral)
441
+ let name = peripheral.name ?? "Unknown"
442
+ print("ThermalPrinter: Discovered peripheral: \(name) (\(peripheral.identifier.uuidString))")
415
443
  }
416
444
 
417
445
  // If we're looking for a specific printer to print to
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruhiverse/thermal-printer-plugin",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
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",