How to use multiple Estimote proximity beacon in the same code in Swift?

Hello All,
i have 4 proximity beacons and what I need to do s that when I enter the area of each one of them I need the code to transfer to another View controller to display some content. The problem is that when I put all the credentials( i made an app for each beacon in the Estimote cloud ) to get credentials of all the beacons I cannot detect them but when I put only one it works!

I need to put them all in the main page and the transfer to other pages from this page

can anyone help me with this, please ?

Thank you

import UIKit
import ARKit
import EstimoteProximitySDK

struct Content {
    let title: String
    let subtitle: String
}

@available(iOS 11.0, *)
class Question1ViewController: UIViewController, UICollectionViewDelegateFlowLayout {
    
    //let userId = UserDefaults.standard.object(forKey: "userId") as? [String]
    
    
    @IBOutlet weak var sceneView: ARSCNView!
    
    var proximityObserver: ProximityObserver!
    var flagCoconut = false
    var flagMint = false
    var flagIce = false
    var nearbyContent = [Content]()
    
    override func viewDidLoad() {  DispatchQueue.main.async {
        super.viewDidLoad()
        
        ///////////////////// Coconut /////////////////////
        
        let Coconut = CloudCredentials(appID: "jawab-test-2m5", appToken: "6ff2355c245c7d7953c64eb5c2a912ba")
        
        self.proximityObserver = ProximityObserver(credentials: Coconut, onError: { error in
            print("ProximityObserver error: \(error)")
        })
        
        let zone = ProximityZone(tag: "jawab-test-2m5", range: ProximityRange.near)
        
  
            zone.onEnter = { contexts in
                print("Enter")
      
                let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                let QuestionViewController = storyBoard.instantiateViewController(withIdentifier: "Questionscene") as! QuestionViewController
                
                self.present(QuestionViewController, animated: true, completion: nil)
          
                
 
            
            
            zone.onExit = { contexts in
                print("Exit")
               // self.flagCoconut = false
            }
        }
        self.proximityObserver.startObserving([zone])
        
        
        ///////////////////// Mint /////////////////////
        
       let mint = CloudCredentials(appID: "reem-badr-s-proximity-for--6o4", appToken: "8be2dff5dc16b9747b7fafe97ff53708")
        
        self.proximityObserver = ProximityObserver(credentials: mint, onError: { error in
            print("ProximityObserver error: \(error)")
            
            
        })
        
        let zoneMint = ProximityZone(tag: "reem-badr-s-proximity-for--6o4", range: ProximityRange.near)
        if !self.flagCoconut {
            zoneMint.onEnter = { contexts in
                print("enter")
                self.flagMint = true
                let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
                let QuestViewController = storyBoard.instantiateViewController(withIdentifier: "questScene") as! QuestViewController
                self.present(QuestViewController, animated: true, completion: nil)
            }
            zoneMint.onExit = { contexts in
                print("Exit")
                self.flagMint = false
                
            }
            
        }
        
        self.proximityObserver.startObserving([zoneMint])
    
        
        ////////////////////ice //////////////////////
        
        
        
        
        
       let ice = CloudCredentials(appID: "ice---jawwab-for-a-single--24i", appToken: "4cc922d2dc4834858e08255a67e65785")
        
        self.proximityObserver = ProximityObserver(credentials: ice, onError: { error in
            print("ProximityObserver error: \(error)")
            
            
        })
        
        let zoneIce = ProximityZone(tag: "ice---jawwab-for-a-single--24i", range: ProximityRange.near)
        //  if !self.flagCoconut {
        zoneIce.onEnter = { contexts in
            print("enter ice ")

            
            let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
            let QuestionViewController = storyBoard.instantiateViewController(withIdentifier: "Questionscene") as! QuestionViewController
            
            self.present(QuestionViewController, animated: true, completion: nil)
            
            
          
            
        }
        zoneIce.onExit = { contexts in
            print("Exit")
 
            
        }
        
 
        
        self.proximityObserver.startObserving([zoneIce])
        
        //////////////
        }
        
        
        
        
        
        ///////////////ice////////////////////////
    }
    
    
    
    





override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    let configuration = ARWorldTrackingConfiguration()
    
    sceneView.session.run(configuration)
    
}


override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    sceneView.session.pause()
}


}
  1. Use only 1 Proximity Observer and 1 CloudCredentials:
// make sure the App ID and Token are correct
let credentials = CloudCredentials(appID: "...", appToken: "...")
self.proximityObserver = ProximityObserver(credentials: credentials, onError: { error in
    print("ProximityObserver error: \(error)")
})
  1. In Estimote Cloud, assign unique tags to each of your beacons (e.g., add “my-mint-beacon” tag to your mint beacon, “my-ice-beacon” to your icy beacon, etc.)

  2. Create 4 different Proximity Zones, and use the tags from step 2. (very similar to what you have already)

let zoneMint = ProximityZone(tag: "my-mint-beacon", range: ProximityRange.near)
zoneMint.onEnter = { contexts in
    // ...
}
// ...

let zoneIce = ProximityZone(tag: "my-ice-beacon", range: ProximityRange.near)
zoneIce.onEnter = { contexts in
    // ...
}
// ...

// ...
  1. Call startObserving with all 4 zones:
self.proximityObserver.startObserving([
    zoneMint, zoneIce, zoneCoconut, zoneBlueberry])

Specifically, right now, you’re creating 4 Proximity Observers, but you keep assigning them to the same variable, self.proximityObserver, which means each new Proximity Observer overwrites the last one.

You could do self.proximityObserverMint, self.proximityObserverIce, etc., but better yet, one Proximity Observer is perfectly capable of observing all your beacons, and also uses less resources in the process than 4 Proximity Observers. So it’s better to use just one.

2 Likes