@ruhiverse/thermal-printer-plugin 1.0.5 → 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,13 +26,11 @@ 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)
32
+ // Note: We scan with nil services to find all peripherals, then filter by name
31
33
  private let serialPortServiceUUID = CBUUID(string: "00001101-0000-1000-8000-00805F9B34FB") // SPP
32
- private let commonPrinterServiceUUIDs = [
33
- CBUUID(string: "00001101-0000-1000-8000-00805F9B34FB"), // SPP
34
- CBUUID(string: "E7810A71-73AE-499D-8C15-DAA9080B0E"), // Some thermal printers
35
- ]
36
34
 
37
35
  /// Find a connected External Accessory that matches the given (optional) name
38
36
  /// and supports one of the known printer protocols.
@@ -304,9 +302,11 @@ public class ThermalPrinterPlugin: CAPPlugin, CBCentralManagerDelegate, CBPeriph
304
302
 
305
303
  // Then scan for BLE printers
306
304
  discoveredPeripherals.removeAll()
305
+ listPrintersCall = call
307
306
 
307
+ // Initialize central manager if needed
308
308
  if centralManager == nil {
309
- centralManager = CBCentralManager(delegate: self, queue: nil)
309
+ centralManager = CBCentralManager(delegate: self, queue: DispatchQueue.main)
310
310
  }
311
311
 
312
312
  guard let centralManager = centralManager else {
@@ -314,51 +314,98 @@ public class ThermalPrinterPlugin: CAPPlugin, CBCentralManagerDelegate, CBPeriph
314
314
  return
315
315
  }
316
316
 
317
- // Wait a bit for central manager to be ready, then scan
318
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
319
- if centralManager.state == .poweredOn {
320
- // Scan for all peripherals (we'll filter by name/characteristics)
321
- centralManager.scanForPeripherals(withServices: nil, options: [CBCentralManagerScanOptionAllowDuplicatesKey: false])
322
-
323
- // Stop scanning after 5 seconds
324
- DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) {
325
- centralManager.stopScan()
326
-
327
- // Add discovered BLE printers
328
- let blePrinters: [[String: String]] = self.discoveredPeripherals.compactMap { peripheral in
329
- guard let name = peripheral.name, !name.isEmpty else {
330
- return nil
331
- }
332
- // Filter for common printer names or accept all if we found them
333
- let printerKeywords = ["printer", "print", "thermal", "pos", "epson", "star", "receipt"]
334
- let isLikelyPrinter = printerKeywords.contains { keyword in
335
- name.lowercased().contains(keyword)
336
- }
337
-
338
- // Include if it's a likely printer or if we have no other printers found
339
- if isLikelyPrinter || printers.isEmpty {
340
- return [
341
- "name": name,
342
- "address": peripheral.identifier.uuidString,
343
- ]
344
- }
345
- return nil
346
- }
347
-
348
- printers.append(contentsOf: blePrinters)
349
- 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
350
363
  }
351
- } else {
352
- // Bluetooth not available, return what we have
353
- call.resolve(["printers": printers])
364
+ return [
365
+ "name": name,
366
+ "address": peripheral.identifier.uuidString,
367
+ ]
354
368
  }
369
+
370
+ printers.append(contentsOf: blePrinters)
371
+ call.resolve(["printers": printers])
372
+ self.listPrintersCall = nil
355
373
  }
356
374
  }
357
375
 
358
376
  // MARK: - CBCentralManagerDelegate
359
377
 
360
378
  public func centralManagerDidUpdateState(_ central: CBCentralManager) {
361
- // 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
+ }
362
409
  }
363
410
 
364
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.5",
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",