davidgyoung/android-beacon-library-reference-kotlin

Some Errors

NimaaZx opened this issue · 2 comments

Hello David. Thanks for sharing your knowledge . I don't know it is good place to ask a question or not.
I followed you from a year ago. when I have worked on beacon scanning app. at first I want to ask you a question ,

Edit: I could handle this problem, I forgot to add < application > tag in manifest. so a question number 1 and 2 are not needed to be answered .
1- Did You run these codes successfully?
I got some errors and changed some code logics but I could not get it running.

Honestly I want to use MVVM architecture with your library and my previous architecture is MVP and it is too messy. if you remember I talked to you (in stackoverflow : https://stackoverflow.com/questions/63880564/why-is-didrangebeaconsinregion-called-two-times-in-using-altbeacon-android ) about why binding and unbinding do not have similar behavior in different android version from 5 to 9 that I tested. I wrote that problem in a comment section in stackoverflow link. for example I had a button for starting and stopping scanning. when I disconnected beacon and rescanned nothing was shown in 50 percent of situation or calling binding did not work in version android 5 . so I had to wrote a different code for this version. (I did not tested in android 6 7 8, I just tested the code in android 5 and 9). in 9 it worked perfect. In the following I had to bind beacon manager in different situation , like :

OnCreateView()
{ if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1)
                 beaconManager.bind(this)
             else
                 startScan()
}

  fun startScan() {
        try {
            if(!beaconManager.isBound(this))
            {
                beaconManager.bind(this)
            }
            if (isScanFirstTime and beaconManager.isBound(this))
            {
                beaconManager.startMonitoringBeaconsInRegion(region)
                beaconManager.startRangingBeaconsInRegion(region)
                isScanFirstTime = false
            }
            else
            {
                beaconManager.startRangingBeaconsInRegion(region)
            }
        }
        catch (e: RemoteException) {
            Log.d("startScanFunction", "Start scan beacon problem", e)
        }
    }

  override fun onBeaconServiceConnect() {

       if( Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP_MR1)
       {
           beaconManager.foregroundScanPeriod = 7000L
           beaconManager.foregroundBetweenScanPeriod = 2000L
           beaconManager.updateScanPeriods()
       }
...
}

as you see I called binding for android 5 differently because it was not working correctly if I did not add that version checking and that codes . at now I want to write a clean and right code for working in every version.

2 -now even In this GitHub project I have some errors and I changed a logic, You wrote a comment in BeaconReferenceApplication class.

    // If you don't need background detection, you can use a variant to the above call that looks
   // like this:
   // beaconManager.bind(this) /* You will need to make this class implement BeaconConsumer */
   // beaconManager.startMonitoringBeaconsInRegion(region); // put this in the onBeconServiceConnected callback

the main problem is oncreate method of BeaconReferenceApplication is never called in my code. I had to change this line
" beaconReferenceApplication = application as BeaconReferenceApplication" to "beaconReferenceApplication = BeaconReferenceApplication()" in main Activty.
probably it causes this problem.

If you agree and have free a time, we will continue to give you more information and how to solve this problem and writing unique code for all android versions .

Thank you so much.

The key thing that a that you cannot instantiate BeaconReferenceApplication yourself. The Android framework must do it for you. You must declare the Application class in your AndroidManifest.xml in the <application android:name=“...” ...> look at the reference app source code for how it does this.

Thanks again David, I did it and it is worked .I'm stuck in a special situation. Sorry for asking my questions in a bad place.

Could you see my two files project please?
it is based from your codes.
my project link : https://github.com/NimaaZx/beaconTest/tree/master/app/src/main/java/nima/mythird/beacontest

my app can scan beacons for first time and for the rest usage the app , the user should start scanning by scan button . I thought I could rely on onBeaconServiceConnect( ) method for finding out scanning is done or in progress. but I realize that onBeaconServiceConnect is Called once when I instantiate beaconRefrenceApplication.kt and for the rest just "didRangeBeaconsInRegion" is called many times.
(I did them in my project)

so could you give a suggestion how to follow and set observer for scanning Bluetooth status in real time? I mean I need to realize when scanning is started - in progress and done.

I saw java docs of library and CycledLeScanner protected class and their protected properties. I found something but they are not usable.
Do you know a approach for doing this?