daily pastebin goal
13%
SHARE
TWEET

ViewController FloBLE+

aleix616 Oct 28th, 2016 16 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class ViewController: UIViewController, FmSessionManagerDelegate {
  2.  
  3.     var readerManager : FmSessionManager!
  4.     var device : FmDevice?
  5.    
  6.     override func viewDidLoad() {
  7.         super.viewDidLoad()
  8.        
  9.         readerManager = FmSessionManager()
  10.         readerManager.selectedDeviceType = DeviceType.floBlePlus
  11.         readerManager.delegate = self
  12.        
  13.         let configurationDictionary : [String : Any] =
  14.             [
  15.                 "Scan Sound" : 1,
  16.                 "Scan Period" : 1000,
  17.                 "Reader State" : ReaderStateType.readUuid.rawValue, //readData for NDEF
  18.                 "Power Operation" : PowerOperation.autoPollingControl.rawValue, //bluetoothConnectionControl low power usage
  19.                 "Allow Multiconnect" : false]
  20.        
  21.         readerManager.setConfiguration(configurationDictionary)
  22.         readerManager.createReaders()
  23.         readerManager.startReader(deviceSerialNumber)
  24.        
  25.         NotificationCenter.default.addObserver(
  26.             self,
  27.             selector: #selector(inactive),
  28.             name: NSNotification.Name.UIApplicationDidEnterBackground,
  29.             object: nil)
  30.        
  31.         NotificationCenter.default.addObserver(
  32.             self,
  33.             selector: #selector(active),
  34.             name: NSNotification.Name.UIApplicationDidBecomeActive,
  35.             object: nil)
  36.        
  37.     }
  38.  
  39.     override func didReceiveMemoryWarning() {
  40.         super.didReceiveMemoryWarning()
  41.         // Dispose of any resources that can be recreated.
  42.     }
  43.    
  44.     func inactive() {
  45.         print("App Inactive")
  46.         readerManager.stopReader(deviceSerialNumber)
  47.     }
  48.    
  49.     func active() {
  50.         print("App Activated")
  51.         readerManager.startReader(deviceSerialNumber)
  52.     }
  53.    
  54.     func didFindATagUuid(_ UUID: String!, fromDevice serialNumber: String!, withATR detectedATR: String!, withError error: Error!) {
  55.         DispatchQueue.main.async {
  56.             if let thisUUID = UUID, let thisSerialNumber = serialNumber {
  57.                 print("Did find UUID: \(thisUUID) from Device: \(thisSerialNumber)")
  58.             }
  59.         }
  60.     }
  61.    
  62.     func didFindADataBlock(withNdef ndef: NdefMessage!, fromDevice serialNumber: String!, withError error: Error!) {
  63.         DispatchQueue.main.async {
  64.             if let thisNdef = ndef, let thisSerialNumber = serialNumber {
  65.                 print("Did find NDEF: \(thisNdef) from Device: \(thisSerialNumber)")
  66.             }
  67.         }
  68.     }
  69.    
  70.     func didRespond(toApduCommand response: String!, fromDevice serialNumber: String!, withError error: Error!) {
  71.         DispatchQueue.main.async {
  72.             if let thisResponse = response, let thisSerialNumber = serialNumber {
  73.                 print("Did respond with UPDU: \(thisResponse) from Device: \(thisSerialNumber)")
  74.             }
  75.         }
  76.     }
  77.    
  78.     func didUpdateConnectedDevices(_ devices: [Any]!) {
  79.         //The list of connected devices was updated
  80.         if devices[0] is FmDevice {
  81.             self.device = devices[0] as? FmDevice
  82.         }
  83.     }
  84.    
  85.     func didChange(_ status: CardStatus, fromDevice device: String!) {
  86.         //The card status has entered or left the scan range of the reader
  87.         // Cardstatus:
  88.         // 0:kNotPresent
  89.         // 1:kPresent
  90.         // 2:kReadingData
  91.         let command = "00 A4 04 00 0E 32 50 41 59 2E 53 59 53 2E 44 44 46 30 31 00"
  92.         self.device?.sendApduCommand(command)
  93.     }
  94.    
  95.     func didReceiveReaderError(_ error: Error!) {
  96.         DispatchQueue.main.async {
  97.             print("Error: \(error)")
  98.         }
  99.     }
  100.  
  101.  
  102. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top