Estimote / Android-Fleet-Management-SDK

Estimote Fleet Management SDK for Android
https://developer.estimote.com
MIT License
836 stars 451 forks source link

iBeacon Scan stops when using two Services #59

Closed ganddev closed 10 years ago

ganddev commented 10 years ago

Hi I'm trying to use GCM and iBeacon in one app. For the registration to GCM I use a service and start it as following in the onCreate of the activity:

startService(new Intent(this, MessageReceivingService.class));

After that I want to scan for iBeacons, with the BeaconManager.

beaconManager = new BeaconManager(this);

beaconManager.setRangingListener(new BeaconManager.RangingListener() {
            @Override
            public void onBeaconsDiscovered(Region region, List<Beacon> beacons) {
                for (Beacon b : beacons) {
                    if (Utils.proximityFromAccuracy(Utils.computeAccuracy(b))
                            .equals(Utils.Proximity.NEAR)) {
                        Log.d(TAG, b.toString());
                        SharedPreferences prefs = getSharedPreferences(getString(R.string.preferences), Context.MODE_PRIVATE);

                        if(prefs != null && !prefs.getBoolean(getString(R.string.first_launch), true))
                        {
                            ApiService.notifyCustomer(b);
                        }
                    }
                }
            }
        });

The problem is that the app doesn't start BLE scan for iBeacons. When I don't start the GCM Service, everything works fine. Is it a bug in the SDK or do I something wrong?

wiktor commented 10 years ago

I fixed this problem and library is good to use. Please give it a try and tell me how it works.

wiktor commented 10 years ago

ahhh, wrong issues. sorry

wiktor commented 10 years ago

Do you see any errors in device logs? But from what I see is that you need to connect to BeaconManager first (as it is services underneath).

See https://github.com/Estimote/Android-SDK#usage-and-demos and pay attention to #connect method invocation before startRanging method.

ganddev commented 10 years ago

I changed it in the way you mentioned: First initialized the beacon Manager etc. and start My Service in the onStart after

@Override
    public void onStart() {
        super.onStart();
        // Should be invoked in #onStart.
        beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
            @Override
            public void onServiceReady() {
                try {
                    beaconManager.startRanging(ALL_ESTIMOTE_BEACONS);
                } catch (RemoteException e) {
                    Log.e(TAG, "Cannot start ranging", e);
                }
            }
        });
        startService(new Intent(this, MessageReceivingService.class));
    }

But it is the same behavior. BLE Scan doesn't start. Log:

06-24 21:38:07.633: D/BluetoothAdapter(26244): startLeScan(): null
06-24 21:38:07.653: I/onResume(26244): no missed messages
06-24 21:38:07.683: D/BtGatt.GattService(10554): registerClient() - UUID=3ff7701a-8d48-4007-81dc-bbec5154a02d
06-24 21:38:07.683: D/BtGatt.btif(10554): btif_gattc_register_app
06-24 21:38:07.683: D/BtGatt.btif(10554): btgattc_handle_event: Event 1000
06-24 21:38:07.683: D/BtGatt.btif(10554): btif_gattc_upstreams_evt: Event 0
06-24 21:38:07.683: D/BtGatt.GattService(10554): onClientRegistered() - UUID=3ff7701a-8d48-4007-81dc-bbec5154a02d, clientIf=5
06-24 21:38:07.683: D/BluetoothAdapter(26244): onClientRegistered() - status=0 clientIf=5
06-24 21:38:07.683: D/BtGatt.GattService(10554): startScan() - queue=0
06-24 21:38:07.683: D/BtGatt.GattService(10554): startScan() - adding client=5
06-24 21:38:07.683: D/BtGatt.btif(10554): btif_gattc_scan
06-24 21:38:07.683: D/BtGatt.btif(10554): btgattc_handle_event: Event 1002
06-24 21:38:07.703: I/ActivityManager(767): Displayed de.bjoern.ahlfeld.shoplytics/.activities.MainActivity: +116ms (total +2s402ms)
06-24 21:38:07.713: D/dalvikvm(26244): GC_CONCURRENT freed 246K, 2% free 17115K/17392K, paused 3ms+3ms, total 19ms
06-24 21:38:07.713: D/BluetoothAdapter(26244): stopLeScan()
06-24 21:38:07.713: D/BtGatt.GattService(10554): stopScan() - queue=1
06-24 21:38:07.713: D/BtGatt.GattService(10554): stopScan() - queue empty; stopping scan
06-24 21:38:07.713: D/BtGatt.btif(10554): btif_gattc_scan
06-24 21:38:07.713: D/BtGatt.btif(10554): btgattc_handle_event: Event 1003
06-24 21:38:07.713: D/BtGatt.GattService(10554): unregisterClient() - clientIf=5
06-24 21:38:07.713: D/BtGatt.btif(10554): btif_gattc_unregister_app
06-24 21:38:07.713: D/BtGatt.btif(10554): btgattc_handle_event: Event 1001
06-24 21:38:07.713: D/BtGatt.btif(10554): btif_gattc_upstreams_evt: Event 1
wiktor commented 10 years ago

Can you enable debug logging by: com.estimote.sdk.utils.L.enableDebugLogging(true).

Side question: if you download Estimote app from Play Store, do you see your beacons?

ganddev commented 10 years ago

Yes I can see my beacons with the estimote app. I'will also receive beacons, when I don't start my own service, which handles gcm stuff.

Log when debugging is en abled.

06-24 21:48:50.343: D/EstimoteSDK(28212): com.estimote.sdk.service.BeaconService$IncomingHandler$1.run:497 Setting background scan period: ScanPeriodData{scanPeriodMillis=5000, waitTimeMillis=0}
06-24 21:48:50.343: V/EstimoteSDK(28212): com.estimote.sdk.service.BeaconService.startRanging:231 Start ranging: Region{identifier=regionId, proximityUUID=b9407f30-f5f8-466e-aff9-25556b57fe6d, major=null, minor=null}
06-24 21:48:50.343: D/BluetoothAdapter(28212): startLeScan(): null
06-24 21:48:50.343: D/BtGatt.GattService(10554): registerClient() - UUID=5a42e21f-055e-4b00-9dfe-adbd4b5ae1f8
06-24 21:48:50.343: D/BtGatt.btif(10554): btif_gattc_register_app
06-24 21:48:50.343: D/BtGatt.btif(10554): btgattc_handle_event: Event 1000
06-24 21:48:50.343: D/BtGatt.btif(10554): btif_gattc_upstreams_evt: Event 0
06-24 21:48:50.343: D/BtGatt.GattService(10554): onClientRegistered() - UUID=5a42e21f-055e-4b00-9dfe-adbd4b5ae1f8, clientIf=5
06-24 21:48:50.343: D/BluetoothAdapter(28212): onClientRegistered() - status=0 clientIf=5
06-24 21:48:50.343: D/BtGatt.GattService(10554): startScan() - queue=0
06-24 21:48:50.343: D/BtGatt.GattService(10554): startScan() - adding client=5
06-24 21:48:50.343: D/BtGatt.btif(10554): btif_gattc_scan
06-24 21:48:50.343: D/BtGatt.btif(10554): btgattc_handle_event: Event 1002
06-24 21:48:50.383: I/ActivityManager(767): Displayed de.bjoern.ahlfeld.shoplytics/.activities.MainActivity: +138ms
06-24 21:48:50.483: I/bt-hci(10554): BLE HCI(id=62) event = 0x02)
06-24 21:48:50.483: I/bt-hci(10554): btu_ble_process_adv_pkt
06-24 21:48:50.483: I/bt-hci(10554): BLE HCI(id=62) event = 0x02)
06-24 21:48:50.483: I/bt-hci(10554): btu_ble_process_adv_pkt
06-24 21:48:50.483: D/BtGatt.btif(10554): btif_gattc_upstreams_evt: Event 4096
06-24 21:48:50.483: D/BtGatt.btif(10554): btif_gattc_add_remote_bdaddr device added idx=0
06-24 21:48:50.483: D/BtGatt.btif(10554): btif_gattc_update_properties BLE device name=estimote len=8 dev_type=2
06-24 21:48:50.483: D/BtGatt.GattService(10554): onScanResult() - address=C3:BC:E3:C3:D7:1F, rssi=-66
06-24 21:48:50.683: I/bt-hci(10554): BLE HCI(id=62) event = 0x02)
06-24 21:48:50.683: I/bt-hci(10554): btu_ble_process_adv_pkt
06-24 21:48:50.683: I/bt-hci(10554): BLE HCI(id=62) event = 0x02)
06-24 21:48:50.683: I/bt-hci(10554): btu_ble_process_adv_pkt
06-24 21:48:50.683: D/BtGatt.btif(10554): btif_gattc_upstreams_evt: Event 4096
06-24 21:48:50.683: D/BtGatt.GattService(10554): onScanResult() - address=C3:BC:E3:C3:D7:1F, rssi=-67
06-24 21:48:50.713: V/EstimoteSDK(28212): com.estimote.sdk.service.BeaconService.stopRanging:238 Stopping ranging: regionId
06-24 21:48:50.713: D/BluetoothAdapter(28212): stopLeScan()
06-24 21:48:50.723: D/BtGatt.GattService(10554): stopScan() - queue=1
06-24 21:48:50.723: D/BtGatt.GattService(10554): stopScan() - queue empty; stopping scan
06-24 21:48:50.723: D/BtGatt.btif(10554): btif_gattc_scan
06-24 21:48:50.723: D/BtGatt.btif(10554): btgattc_handle_event: Event 1003
06-24 21:48:50.723: D/BtGatt.GattService(10554): unregisterClient() - clientIf=5
06-24 21:48:50.723: D/BtGatt.btif(10554): btif_gattc_unregister_app
06-24 21:48:50.723: D/BtGatt.btif(10554): btgattc_handle_event: Event 1001
06-24 21:48:50.723: D/BtGatt.btif(10554): btif_gattc_upstreams_evt: Event 1

When I add the `àndroid:process=:my_process`` tag to the estimote service it scans for beacons until the app dies. The problem is that a different SharedPreferences in the RangingListener and in the app, so it is no solution for me.

wiktor commented 10 years ago

Fix for remote process (android:process=:my_process) I have just submitted. From logs it looks like it starts ranging. Can you pass null as proximity id (that is new Region("id", null, null, null)?

Do you have any services in remote processes?

ganddev commented 10 years ago

Logs with null:

06-24 22:07:00.053: D/EstimoteSDK(29503): com.estimote.sdk.service.BeaconService$IncomingHandler$1.run:497 Setting background scan period: ScanPeriodData{scanPeriodMillis=5000, waitTimeMillis=0}
06-24 22:07:00.063: V/EstimoteSDK(29503): com.estimote.sdk.service.BeaconService.startRanging:231 Start ranging: Region{identifier=regionId, proximityUUID=null, major=null, minor=null}
06-24 22:07:00.063: D/BluetoothAdapter(29503): startLeScan(): null
06-24 22:07:00.063: D/BluetoothAdapter(29503): onClientRegistered() - status=0 clientIf=5
06-24 22:07:00.423: V/EstimoteSDK(29503): com.estimote.sdk.service.BeaconService.stopRanging:238 Stopping ranging: regionId
06-24 22:07:00.433: D/BluetoothAdapter(29503): stopLeScan()
06-24 22:08:15.973: V/EstimoteSDK(29503): com.estimote.sdk.service.BeaconService.stopRanging:238 Stopping ranging: regionId
06-24 22:08:15.973: D/BluetoothAdapter(29503): stopLeScan()

Nope there is no service which runs in seperated process, right now.

wiktor commented 10 years ago

Problem lies here that you start ranging at 22:07:00.063 and stops at 22:07:00.423 so there is no chance that you can find any beacons.

For some reason you are stopping ranging or disconnecting from service. Can you check that?

ganddev commented 10 years ago

Well I think the problem is start different service in the same process. As I mentioned before, when I don't start my process the scanning works well.

wiktor commented 10 years ago

Recommended way is to hold only one instance of BeaconManager and communicate with that instance. Can you do that?

ganddev commented 10 years ago

I only have one instance of the BeaconManager. My own Service does something completely different, which has nothing to do with beacons. I uploaded my code to github https://github.com/ganddev/Shoplytics hopefully it's much easier to talk about the problem.

wiktor commented 10 years ago

Can you check if BeaconScanService is not getting onDestroy invoked too quickly?

ganddev commented 10 years ago

Sorry, I uploaded to much code. BeaconScanService is not started in that version. so onDestroy is not called. Now I moved everything back so BeaconScanService is started.

BLE Scan now works but the problem is there is no good/ safe way to access sharedpreferences between to processes.