iOS Tutorial iBeacons

Discussion in 'iOS Tutorials' started by Erel, May 7, 2017.

  1. Erel

    Erel Administrator Staff Member Licensed User

    iBeacons are small, low powered peripherals, that use BLE (Bluetooth Low Energy) to advertise their information.

    The BeaconParser class can be used together with the BLE library to search for beacons. On Android, BeaconParser will find iBeacons, however not on iOS. iOS treats iBeacons in a different way.

    The attached example extends the Location library with inline Objective C code to search for iBeacons.

    You need to know the iBeacon UDID of the beacon you are searching for.
    The user will need to first allow the app to access the location services (don't miss the two #PlistExtra lines).


    The DidRangeBeacons event will be raised when a beacon is discovered:
    Code:
    Sub LocManager_DidRangeBeacons (Beacons As List)
       
    For Each Beacon As NativeObject In Beacons
         
    Dim UUID As String = Beacon.GetField("proximityUUID").AsString
         
    Dim Major As Int = Beacon.GetField("major").AsNumber
         
    Dim Minor As Int = Beacon.GetField("minor").AsNumber
         
    Dim proximity As Int = Beacon.GetField("proximity").AsNumber '0 = unknown, 1 = immediate, 2 = near, 3 = far
         Dim rssi As Int = Beacon.GetField("rssi").AsNumber
         TextView1.Text = 
    $"Beacon found: ${UUID}
    Major: ${
    Major}
    Minor: ${
    Minor}
    Proximity: ${
    proximity}
    RSSI: ${
    rssi}
    Time: $Time{
    DateTime.Now}
    "$

       
    Next
    End Sub

    upload_2017-5-7_15-10-33.png


    Note that you can use another iOS device to emulate an iBeacon. For example: https://itunes.apple.com/de/app/beacon-tx/id1036165476?l=en&mt=8
     

    Attached Files:

    MarcoRome likes this.
  2. Christian García S.

    Christian García S. Active Member Licensed User

    Thanks Erel for this contribution.

    I implemented and works excellent, maybe can you help me with two questions.

    1. I followed this tutorial for put in background core location: https://www.b4x.com/android/forum/threads/background-location-tracking.50246/#content, this works when the app is in background and until when I use the phone in others apps, still when the screen power off. The issue is later 30 seconds after the screen is off, SearchForBeacon is stopped.

    I dont know if this is a normal behavior in IOS or I can do anything else with code even when the screen is off for keep searching beacons?

    2. I was searching about data plan but I did not find, I want use webservices with data plan when I dont have WIFI connection, I dont see those permissions in configuration inside my app. do I need put #PlistExtra or other code for requesting that permission?

    Thanks for your help.

    Regards
     
    Last edited: May 8, 2017
  3. Erel

    Erel Administrator Staff Member Licensed User

    1. The iBeacon code will not work when the app is not running. It requires several additional steps that are currently not implemented.

    2. This question is not related to this thread. You don't need to do anything special. It is handled by the OS automatically.
     
  4. Christian García S.

    Christian García S. Active Member Licensed User

    Thanks Erel.

    1. I was reading about Defining a Beacon Region to Be Monitored in

    https://developer.apple.com/library...nessPG/RegionMonitoring/RegionMonitoring.html.

    In your example code you have startRangingBeaconsInRegion, do you think we can change for startMonitoringForRegion for getting the other features from IOS like that:

    Beacon monitoring however continues running even if your app goes to sleep, or gets terminated by the iOS due to memory pressure. When you go in or out of range of a beacon, iOS will launch your app into the background to handle the event, giving it a few seconds (which you can extend up to a few minutes with a Background Execution Task) to do so (you can range during that time), but then it's back to sleep.

    http://stackoverflow.com/questions/...-monitoring-ibeacon-when-screen-is-off-in-ios

    Please you can implement this option, I think with this the beacon subject is would complete.​

    2. I will search about this in the forum or create a new thread.

    Thanks for your help.


     
    Last edited: May 8, 2017
  5. Christian García S.

    Christian García S. Active Member Licensed User

    Hello Erel, thanks for your previous code.

    Can you help us about the last post, please can you implement the ibeacon monitoring in IOS ??, we need this because when you have the app in background that stops reading new beacons.

    The library is not functionally in real world, because you does not have your phone in your hand every time, usually you put phone in your pocket and after 30 seconds almost all apps stops or goes to sleep.

    How you can help us with this, I think when you complete monitoring you complete this subject.

    Thanks,

    Regards,
    Christian
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Please try this project. It implements beacon monitoring. Start with testing it in foreground mode. Does it monitor the beacon properly? It didn't here but I was using another device to simulate the beacon so maybe it was not perfect.
     

    Attached Files:

  7. Christian García S.

    Christian García S. Active Member Licensed User

    Thanks Erel, great job, the code works.

    I have two questions:

    1. I understand beacon monitoring works even when the app is closed or goes to sleep, this is right ??

    2. Which event be raised when new region is founded and the app is closed?, I ask this because I need send a notification when app is not open and invite to user open the app clicking in notification, LocManager_StateChanged is the event ??? I tried this event when the app is active and background and works.

    Thanks for your help.

    Regards,
    Christian
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    It can work in the background. However start with testing it in the foreground. Check the logs and see whether it recognizes the region states.
     
  9. b4xscripter

    b4xscripter Member Licensed User

    So, if I understand well, for iOS, I only can monitor /search for known beacons? Do I need to put their UUID previously in my app?

    Regards
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    That's true.
    You must call SearchForBeacon with a specific UUID. How do you get the UUID is up to you. You can ask the user to enter it or download it from your server for example.
     
  11. b4xscripter

    b4xscripter Member Licensed User

    Ok, and do we have a nice way to search for a list of know beacons? I suppose that I should pass a list to this function:

    Sub SearchForBeacon(UUID As String, Identifier As String) As Object
    If LocManager.IsAuthorized Or LocManager.AuthorizationStatus = LocManager.AUTHORIZATION_NOT_DETERMINED Then
    Dim no As NativeObject = LocManager
    no = no.GetField("manager")
    If LocManager.AuthorizationStatus = LocManager.AUTHORIZATION_NOT_DETERMINED Then
    no.RunMethod("requestWhenInUseAuthorization", Null)
    End If
    Dim nsuuid, br As NativeObject
    nsuuid = nsuuid.Initialize("NSUUID").RunMethod("alloc", Null).RunMethod("initWithUUIDString:", Array(UUID))
    br = br.Initialize("CLBeaconRegion").RunMethod("alloc", Null).RunMethod("initWithProximityUUID:identifier:", _
    Array(nsuuid, Identifier))

    no.RunMethod("startRangingBeaconsInRegion:", Array(br))
    btnStop.Enabled = True
    btnStart.Enabled = False
    Return br
    Else
    Log("Not authorized!")
    Return Null
    End If
    End Sub
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    Please use [code]code here...[/code] tags when posting code.
     
  13. b4xscripter

    b4xscripter Member Licensed User

    Ops, sorry...
    Here we go:

    I suppose that I should pass a list to this function:

    Code:
    Sub SearchForBeacon(UUID As String, Identifier As StringAs Object
    If LocManager.IsAuthorized Or LocManager.AuthorizationStatus = LocManager.AUTHORIZATION_NOT_DETERMINED Then
    Dim no As NativeObject = LocManager
    no = no.GetField(
    "manager")
    If LocManager.AuthorizationStatus = LocManager.AUTHORIZATION_NOT_DETERMINED Then
    no.RunMethod(
    "requestWhenInUseAuthorization"Null)
    End If
    Dim nsuuid, br As NativeObject
    nsuuid = nsuuid.Initialize(
    "NSUUID").RunMethod("alloc"Null).RunMethod("initWithUUIDString:"Array(UUID))
    br = br.Initialize(
    "CLBeaconRegion").RunMethod("alloc"Null).RunMethod("initWithProximityUUID:identifier:", _
    Array(nsuuid, Identifier))

    no.RunMethod(
    "startRangingBeaconsInRegion:"Array(br))
    btnStop.Enabled = 
    True
    btnStart.Enabled = 
    False
    Return br
    Else
    Log("Not authorized!")
    Return Null
    End If
    End Sub
    Thanks, again for your great support, Erel!
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    Just call this method multiple times. One for each UUID.
     
  15. jazzzzzzz

    jazzzzzzz Active Member Licensed User

    Hi ,Erel I have tested this sample project to detect beacon,but in foreground itself its not detecting my estimote beacon.

    in logs it shows "didDetermineState: 1"
    I just got "1" in the textview

    It will be very helpfull if ibeacons can be detected in backround.note that the sample project that you posted in the first section is working good.but not this one
     
  16. Christian García S.

    Christian García S. Active Member Licensed User

    Hello @jazzzzzzz, you can try this code attached few post above:

    MonitorBeacons

    With this code, you have monitoring beacons with IOS and get alerts even when app is closed, works great for me.
     
  17. jazzzzzzz

    jazzzzzzz Active Member Licensed User

    dont know why its not working for me,it just shows "1" in textview when the beacon is found,am using estimote beacon..any idea why?
     
  18. Christian García S.

    Christian García S. Active Member Licensed User

    This is correct, state 1 means beacons was read, and you enter with your phone in region, later you can show some message to alert to users.
     
  19. jazzzzzzz

    jazzzzzzz Active Member Licensed User

    wat about the major and minor values? it does not show that
     
  20. Christian García S.

    Christian García S. Active Member Licensed User

    Users click in messages and they open the app, at this moment you can read beacons normally and you get major and minor.
     
    jazzzzzzz likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice