Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scanning in background in Android should use a Pending Intent #699

Open
omercnet opened this issue Oct 20, 2024 · 3 comments · May be fixed by #700
Open

Scanning in background in Android should use a Pending Intent #699

omercnet opened this issue Oct 20, 2024 · 3 comments · May be fixed by #700
Labels
bug Something isn't working

Comments

@omercnet
Copy link

Describe the bug
The current implementation does not allow connecting to BLE device after the app is backgrounrded

To Reproduce
Steps to reproduce the behavior:

  1. Launch a BLE Scan
  2. Close device screen
  3. Broadcast on BLE device
  4. Wait forever

Expected behavior
The app should find the device in the background

Screenshots
If applicable, add screenshots to help explain your problem.

Plugin version:

  • @capacitor-community/bluetooth-le: v6.0.1

Additional context
https://developer.android.com/develop/connectivity/bluetooth/ble/background

In android, to receive scan results in the background, the startScan method should be called with a Pending Intent and not a ScanResult

See https://github.com/android/platform-samples/blob/main/samples/connectivity/bluetooth/ble/src/main/java/com/example/platform/connectivity/bluetooth/ble/BLEScanIntentSample.kt#L114-L120 for an official example

@omercnet omercnet added the bug Something isn't working label Oct 20, 2024
@omercnet omercnet linked a pull request Oct 20, 2024 that will close this issue
@peitschie
Copy link
Collaborator

peitschie commented Oct 28, 2024

Hi @omercnet

Quick question... when you are launching a scan here... are you filtering by a service UUID? Or running an open scan (i.e., no filters)?

I ask because I noticed this line in the startScan doco when looking at your pending intents patch:

Start Bluetooth LE scan. The scan results will be delivered through callback. For unfiltered scans, scanning is stopped on screen off to save power. Scanning is resumed when screen is turned on again. To avoid this, do filtered scanning by using proper ScanFilter.

Pending intents are only needed when the application process itself may be terminated or paused by the OS and should be relaunched when a device is found. However... this approach doesn't work in general for capacitor applications, as modern Android generally doesn't allow a new Activity like the capacitor browser to be launched from a pending intent like this.

I'll run a few more tests with your patch just to confirm my findings here.

@peitschie
Copy link
Collaborator

On an Nokia 3.1 Plus with Android 10, I'm seeing that the normal callback scan continues reporting devices in the background once the screen is off.

Here's the code I'm using:

await BleClient.initialize();
await BleClient.requestLEScan({
  services: [numberToUUID(0x180a)],
  allowDuplicates: true
}, d => console.log(Date.now(), "device detected", d))

I've got a simulated peripheral (another phone using nrf Connect ) configured with an advertising packet with the 0x180a service in it.

I'm seeing that when the screen is off, the updates slow down to a burst every 10s or so... but... the scanning continues to function.

Are you able to provide more specifics about the BLE plugin calls you're making here @omercnet , and the operating environment? (Android phone manufacturer... android version... test conditions, etc.)?

@peitschie
Copy link
Collaborator

Also relates to #643

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants