Hi
Having read that since Android (contrary to iOS) does not automatically scan for beacons in the background, I am trying to use a Timer with the easibeacon example.
I am thus trying to modify the example code's Discovery Service.
This is my code:
However, I am experiencing three problems:
1. My timer gets called but the call to StartScan() does not work (ie. it does not re-scan every 30 seconds).
2. I see an exception (shown as a ToastMessage: libcore.io.ErrnoException: recvfrom failed: ETIMEDOUT (Connection timed out) when using B4ABridge ?
3. It seems that the ExitRegion handler is never called ??? EnterRegion handler is called though...
Also, an additional question (I am new to the whole beacon thing):
- How do I know that the beacon in Enter/ExitRegion Sub is one of my beacons and not somebody else's ?
Thanks
/Henrik
Having read that since Android (contrary to iOS) does not automatically scan for beacons in the background, I am trying to use a Timer with the easibeacon example.
I am thus trying to modify the example code's Discovery Service.
This is my code:
B4X:
#Region Service Attributes
#StartAtBoot: False
#End Region
Sub Process_Globals
Public IBeaconProtocol1 As IBeaconProtocol
Dim ScanTimer As Timer
Dim ScanInterval As Int = 30000
End Sub
Sub Service_Create
Dim IBeaconListener1 As IBeaconListener
IBeaconListener1.Initialize("IBeaconListener1")
IBeaconProtocol1.Initialize
IBeaconProtocol1.SetListener(IBeaconListener1)
ScanTimer.Initialize("ScanTimer", ScanInterval)
ScanTimer.Enabled = True
End Sub
Sub Service_Start (StartingIntent As Intent)
End Sub
Sub ScanTimer_Tick
ToastMessageShow("Timer tick", False)
StartScan
End Sub
Sub Service_Destroy
StopScan(True)
Dim IBeaconProtocol1 As IBeaconProtocol
End Sub
Sub IBeaconListener1_BeaconFound(IBeacon1 As IBeacon)
Log("IBeaconListener1_BeaconFound")
CallSubDelayed3(Main, "BeaconFound", IBeacon1, True)
End Sub
Sub IBeaconListener1_EnterRegion(IBeacon1 As IBeacon)
Log("IBeaconListener1_EnterRegion")
CallSubDelayed2(Main, "BeaconArriving", IBeacon1)
End Sub
Sub IBeaconListener1_ExitRegion(IBeacon1 As IBeacon)
Log("IBeaconListener1_ExitRegion")
CallSubDelayed2(Main, "BeaconLeaving", IBeacon1)
End Sub
Sub IBeaconListener1_OperationError(Status As Int)
Log("IBeaconListener1_OperationError Status="&Status)
End Sub
Sub IBeaconListener1_SearchState(State As Int)
Log("IBeaconListener1_SearchState")
Select State
Case IBeaconProtocol1.SEARCH_END_EMPTY
ToastMessageShow("Scan complete - no beacons found", False)
Case IBeaconProtocol1.SEARCH_END_SUCCESS
ToastMessageShow("Scan complete - one or more beacons found", False)
Case IBeaconProtocol1.SEARCH_STARTED
ToastMessageShow("Scan started", False)
End Select
End Sub
Sub StartScan
StopScan(False)
IBeaconProtocol1.StartScan
End Sub
Sub StopScan(Reset As Boolean)
If IBeaconProtocol1.IsScanning Then
IBeaconProtocol1.StopScan
ToastMessageShow("Scan stopped", False)
End If
If Reset Then
' calling Reset clears any previously found beacons
IBeaconProtocol1.Reset
End If
End Sub
However, I am experiencing three problems:
1. My timer gets called but the call to StartScan() does not work (ie. it does not re-scan every 30 seconds).
2. I see an exception (shown as a ToastMessage: libcore.io.ErrnoException: recvfrom failed: ETIMEDOUT (Connection timed out) when using B4ABridge ?
3. It seems that the ExitRegion handler is never called ??? EnterRegion handler is called though...
Also, an additional question (I am new to the whole beacon thing):
- How do I know that the beacon in Enter/ExitRegion Sub is one of my beacons and not somebody else's ?
Thanks
/Henrik