iOS Tutorial iBeacons

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:
B4X:
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
 

Attachments

  • iBeacon.zip
    3.1 KB · Views: 689

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:

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:

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
 

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
 

b4xscripter

Member
Licensed User
Longtime 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
 

b4xscripter

Member
Licensed User
Longtime 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
 

b4xscripter

Member
Licensed User
Longtime User
Ops, sorry...
Here we go:

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

B4X:
 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

Thanks, again for your great support, Erel!
 

jazzzzzzz

Active Member
Licensed User
Longtime 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.

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
 

jazzzzzzz

Active Member
Licensed User
Longtime 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?
 

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.
 
Top