2016-12-08 3 views
0

im Hintergrund laufen lässt Ich habe es geschafft, eine App zu erstellen, die das Telefon in einen Beacon-Sender verwandelt.Wie man das iPhone im Hintergrund sowohl im xcode (swift) als auch im phonegap

Es funktioniert gut auf xcode (Swift) und Phonegap.

Der nächste Schritt für mich ist es im Hintergrund als Beacon zu arbeiten (ich entwickle für iOS 10).

ich ähnliche Fragen zu diesem Online gefunden, aber alle von ihnen waren alte ios veraltet dh Objective-C und outdate Plug-Ins

würde es wirklich schätzen, wenn mir jemand mit einem Arbeitsbeispiel oder Führung zur Verfügung stellen kann mich auf das, was sowohl in der Code geändert werden für eine Xcode s (wift) und phonegap

Xcode (Swift 3)

import UIKit 
import CoreLocation 
import CoreBluetooth 

class ViewController: UIViewController,CLLocationManagerDelegate,CBPeripheralManagerDelegate,UITextFieldDelegate{ 

    var locationManager = CLLocationManager() 
    var startLocation: CLLocation! 
    var localBeacon: CLBeaconRegion! 
    var beaconPeripheralData: NSDictionary! 
    var peripheralManager: CBPeripheralManager! 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     locationManager.delegate = self 

     locationManager.requestAlwaysAuthorization() 

     bleSwitch.addTarget(self, action: #selector(ViewController.bleSwitchUpdated(_:)), for: UIControlEvents.valueChanged) 

     major.delegate=self 
     minor.delegate=self 
     txPower.delegate=self 

    } 

    @IBOutlet weak var minor: UITextField! 
    @IBOutlet weak var major: UITextField! 
    @IBOutlet weak var txPower: UITextField! 
    @IBOutlet weak var bleSwitch: UISwitch! 
    @IBOutlet weak var bleView: UIView! 
    @IBAction func bleSwitchUpdated(_ sender: UISwitch) { 
     if bleSwitch.isOn { 
      initLocalBeacon() 
      bleView.isHidden = false; 
     //bleSwitch.setOn(true, animated: false) 
     } 
     else { 
     stopLocalBeacon() 
      bleView.isHidden = true; 
     } 
    } 

    func initLocalBeacon() { 
     if localBeacon != nil { 
      stopLocalBeacon() 
     } 

     let localBeaconUUID = "5A4BCFCE-174E-4BAC-A814-092E77F6B7E5" 
     let localBeaconMajor: CLBeaconMajorValue = UInt16(major.text!)! 
     let localBeaconMinor: CLBeaconMinorValue = UInt16(minor.text!)! 

     let uuid = UUID(uuidString: localBeaconUUID)! 
     localBeacon = CLBeaconRegion(proximityUUID: uuid, major: localBeaconMajor, minor: localBeaconMinor, identifier: "Your private identifer here") 

     beaconPeripheralData = localBeacon.peripheralData(withMeasuredPower: -59) 
     peripheralManager = CBPeripheralManager(delegate: self, queue: nil, options: nil) 
    } 

    func stopLocalBeacon() { 
     peripheralManager.stopAdvertising() 
     peripheralManager = nil 
     beaconPeripheralData = nil 
     localBeacon = nil 
    } 

    func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) { 
     if peripheral.state == .poweredOn { 
      peripheralManager.startAdvertising(beaconPeripheralData as! [String: AnyObject]!) 
     } else if peripheral.state == .poweredOff { 
      peripheralManager.stopAdvertising() 
     } 
    } 

    func textFieldShouldReturn(_ textField: UITextField) -> Bool { 
     textField.resignFirstResponder() 
     return true 

    } 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
     self.view.endEditing(true) 
     if bleSwitch.isOn == true{ 
     stopLocalBeacon() 
      initLocalBeacon() 
     } 
    } 
    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

Phonegap

var app = (function() 
{ 

      // Application object. 
    var app = {}; 

    // Background flag. 
    var mAppInBackground = false; 

     app.initialize = function() 
    { 
     document.addEventListener('deviceready', onDeviceReady, false); 
     document.addEventListener('pause', onAppToBackground, false); 
     document.addEventListener('resume', onAppToForeground, false); 
    }; 

    function onDeviceReady() 
    { 
     startMonitoringAndRanging(); 
     startNearestBeaconDisplayTimer(); 
     displayRegionEvents(); 

    } 

    function onAppToBackground() 
    { 
     mAppInBackground = true; 
     stopNearestBeaconDisplayTimer(); 
    } 

    function onAppToForeground() 
    { 
     mAppInBackground = false; 
     startNearestBeaconDisplayTimer(); 
     displayRegionEvents(); 
    } 


    function startMonitoringAndRanging() 
    { 

     // Request permission from user to access location info. 
     cordova.plugins.locationManager.requestAlwaysAuthorization(); 

      BleconOn(); 

    } 


      function BleconOn(){ 
      var uuid = '00000000-0000-0000-0000-000000000000'; 
      var identifier = 'advertisedBeacon'; 
      var minor = 2000; 
      var major = 5; 
      var beaconRegion = new cordova.plugins.locationManager.BeaconRegion(identifier, uuid, major, minor); 

      var delegate = new cordova.plugins.locationManager.Delegate(); 


      delegate.peripheralManagerDidStartAdvertising = function(pluginResult) { 
      console.log('peripheralManagerDidStartAdvertising: '+ JSON.stringify(pluginResult.region)); 
      }; 
      // Event when bluetooth transmission state changes 
      // If 'state' is not set to BluetoothManagerStatePoweredOn when advertising cannot start 
      delegate.peripheralManagerDidUpdateState = function(pluginResult) { 
      console.log('peripheralManagerDidUpdateState: '+ pluginResult.state); 
      }; 

      cordova.plugins.locationManager.setDelegate(delegate); 

      // Verify the platform supports transmitting as a beacon 
      cordova.plugins.locationManager.isAdvertisingAvailable() 
      .then(function(isSupported){ 

       if (isSupported) { 
       cordova.plugins.locationManager.startAdvertising(beaconRegion) 
       .fail(conole.error) 
       .done(); 
       } else { 
       console.log("Advertising not supported"); 
       } 
       }) 
      .fail(function(e) { console.error(e); }) 
      .done(); 
      } 

      function BleconOff(){ 

      cordova.plugins.locationManager.stopAdvertising() 
      .fail(console.error) 
      .done(); 
      } 



    return app; 

})(); 

app.initialize(); 

für beide Codes werden die Standortaktualisierungen und der Hintergrundabruf in den Hintergrundfunktionen für das Ziel überprüft. Außerdem wird in beiden Wiedergabelisten Privatsphäre - Standort Immer Verwendungsbeschreibung hinzugefügt.

Antwort

2

Leider blockiert Apple die Fähigkeit, als ein Leuchtfeuer im Hintergrund auf iOS zu übertragen. Alle Anzeigen von CoreBluetooth-Standard werden tatsächlich blockiert, da Werbeplätze eine begrenzte Ressource sind und mehrere Apps im Hintergrund diese Ressource schnell ausschöpfen.

Apple hat ein proprietäres Backup-System für die Werbung von Bluetooth-Diensten im Hintergrund, das einen speziellen Überlaufbereich verwendet, um die Service-IDs aufzunehmen. Aber während dieses Schema für die Werbung für Bluetooth-Dienste im Hintergrund funktioniert, funktioniert es nicht für Werbebaken, weil es das Format bricht.

Fazit: Hintergrund Beacon Werbung auf iOS funktioniert einfach nicht. Es tut uns leid.

+0

Wie wäre es mit der Suche nach Beacons auf dem Hintergrund? – nero

+0

Ja, das Scannen funktioniert im Hintergrund. Siehe meine Antwort hier: http://stackoverflow.com/questions/19127282/ibeacon-notification-when-the-app-is-not-running – davidgyoung

+0

Vielen Dank für Ihre Antwort, ich habe Ihr Beispiel von https: // github heruntergeladen. com/RadiusNetworks/ibeacon-background-demo Ich habe einige Änderungen daran vorgenommen, um RSSI-Werte für ein Beacon zu erhalten, wenn die App im Hintergrund ist, und es bekommt nur 5 RSSI-Lesungen, wenn es im Hintergrund ist und dann Nach einer Weile liefert es weitere 5 RSSI-Werte. Gibt es einen Weg, damit es weiter reicht? @davidgyoung – nero

Verwandte Themen