@ruhiverse/thermal-printer-plugin 1.0.6 → 1.0.7

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.
@@ -26,6 +26,7 @@ public class ThermalPrinterPlugin: CAPPlugin, CBCentralManagerDelegate, CBPeriph
26
26
  private var printCompletion: ((Bool, String?) -> Void)?
27
27
  private var printData: Data?
28
28
  private var targetPrintAddress: String?
29
+ private var listPrintersCall: CAPPluginCall?
29
30
 
30
31
  // Common BLE service UUIDs for thermal printers (Serial Port Profile)
31
32
  // Note: We scan with nil services to find all peripherals, then filter by name
@@ -301,9 +302,11 @@ public class ThermalPrinterPlugin: CAPPlugin, CBCentralManagerDelegate, CBPeriph
301
302
 
302
303
  // Then scan for BLE printers
303
304
  discoveredPeripherals.removeAll()
305
+ listPrintersCall = call
304
306
 
307
+ // Initialize central manager if needed
305
308
  if centralManager == nil {
306
- centralManager = CBCentralManager(delegate: self, queue: nil)
309
+ centralManager = CBCentralManager(delegate: self, queue: DispatchQueue.main)
307
310
  }
308
311
 
309
312
  guard let centralManager = centralManager else {
@@ -311,51 +314,98 @@ public class ThermalPrinterPlugin: CAPPlugin, CBCentralManagerDelegate, CBPeriph
311
314
  return
312
315
  }
313
316
 
314
- // Wait a bit for central manager to be ready, then scan
315
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
316
- if centralManager.state == .poweredOn {
317
- // Scan for all peripherals (we'll filter by name/characteristics)
318
- centralManager.scanForPeripherals(withServices: nil, options: [CBCentralManagerScanOptionAllowDuplicatesKey: false])
319
-
320
- // Stop scanning after 5 seconds
321
- DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
322
- centralManager.stopScan()
323
-
324
- // Add discovered BLE printers
325
- let blePrinters: [[String: String]] = self.discoveredPeripherals.compactMap { peripheral in
326
- guard let name = peripheral.name, !name.isEmpty else {
327
- return nil
328
- }
329
- // Filter for common printer names or accept all if we found them
330
- let printerKeywords = ["printer", "print", "thermal", "pos", "epson", "star", "receipt"]
331
- let isLikelyPrinter = printerKeywords.contains { keyword in
332
- name.lowercased().contains(keyword)
333
- }
334
-
335
- // Include if it's a likely printer or if we have no other printers found
336
- if isLikelyPrinter || printers.isEmpty {
337
- return [
338
- "name": name,
339
- "address": peripheral.identifier.uuidString,
340
- ]
341
- }
342
- return nil
343
- }
344
-
345
- printers.append(contentsOf: blePrinters)
346
- call.resolve(["printers": printers])
317
+ // Check Bluetooth state
318
+ switch centralManager.state {
319
+ case .poweredOn:
320
+ // Start scanning immediately
321
+ startBLEScan(for: call, existingPrinters: printers)
322
+ case .poweredOff:
323
+ call.resolve(["printers": printers])
324
+ case .unauthorized:
325
+ call.reject("Bluetooth access denied. Please enable Bluetooth permissions in Settings.")
326
+ case .unsupported:
327
+ call.reject("Bluetooth is not supported on this device.")
328
+ case .resetting, .unknown:
329
+ // Wait for state update
330
+ // The state will be updated via centralManagerDidUpdateState
331
+ break
332
+ @unknown default:
333
+ call.resolve(["printers": printers])
334
+ }
335
+ }
336
+
337
+ private func startBLEScan(for call: CAPPluginCall, existingPrinters: [[String: String]]) {
338
+ guard let centralManager = centralManager else {
339
+ call.resolve(["printers": existingPrinters])
340
+ return
341
+ }
342
+
343
+ guard centralManager.state == .poweredOn else {
344
+ call.resolve(["printers": existingPrinters])
345
+ return
346
+ }
347
+
348
+ // Scan for all peripherals
349
+ centralManager.scanForPeripherals(withServices: nil, options: [CBCentralManagerScanOptionAllowDuplicatesKey: false])
350
+
351
+ // Stop scanning after 5 seconds
352
+ DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
353
+ centralManager.stopScan()
354
+
355
+ var printers = existingPrinters
356
+
357
+ // Add discovered BLE printers - return all devices with names
358
+ 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
347
363
  }
348
- } else {
349
- // Bluetooth not available, return what we have
350
- call.resolve(["printers": printers])
364
+ return [
365
+ "name": name,
366
+ "address": peripheral.identifier.uuidString,
367
+ ]
351
368
  }
369
+
370
+ printers.append(contentsOf: blePrinters)
371
+ call.resolve(["printers": printers])
372
+ self.listPrintersCall = nil
352
373
  }
353
374
  }
354
375
 
355
376
  // MARK: - CBCentralManagerDelegate
356
377
 
357
378
  public func centralManagerDidUpdateState(_ central: CBCentralManager) {
358
- // State updated, scanning will start when ready
379
+ // If we're waiting to list printers, start scanning now
380
+ if let call = listPrintersCall {
381
+ switch central.state {
382
+ case .poweredOn:
383
+ // Get existing MFi printers first
384
+ let manager = EAAccessoryManager.shared()
385
+ let accessories = manager.connectedAccessories
386
+ let mfiPrinters: [[String: String]] = accessories.compactMap { accessory in
387
+ guard accessory.protocolStrings.contains(where: { supportedPrinterProtocols.contains($0) }) else {
388
+ return nil
389
+ }
390
+ return [
391
+ "name": accessory.name,
392
+ "address": accessory.serialNumber.isEmpty ? "\(accessory.connectionID)" : accessory.serialNumber,
393
+ ]
394
+ }
395
+ startBLEScan(for: call, existingPrinters: mfiPrinters)
396
+ case .poweredOff:
397
+ call.resolve(["printers": []])
398
+ listPrintersCall = nil
399
+ case .unauthorized:
400
+ call.reject("Bluetooth access denied. Please enable Bluetooth permissions in Settings.")
401
+ listPrintersCall = nil
402
+ case .unsupported:
403
+ call.reject("Bluetooth is not supported on this device.")
404
+ listPrintersCall = nil
405
+ default:
406
+ break
407
+ }
408
+ }
359
409
  }
360
410
 
361
411
  public func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruhiverse/thermal-printer-plugin",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
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",