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

[Feature] Android: Background Bluetooth Support #930

Open
chipweinberger opened this issue Jul 8, 2024 · 10 comments
Open

[Feature] Android: Background Bluetooth Support #930

chipweinberger opened this issue Jul 8, 2024 · 10 comments

Comments

@chipweinberger
Copy link
Owner

FlutterBluePlus Version

1.32.8

Flutter Version

3.22

What OS?

Android

OS Version

all

Bluetooth Module

all

What is your feature request?

support BLE when app is in the background

Logs

none
@chipweinberger chipweinberger changed the title [Feature] Android background support [Feature] Android: Background Bluetooth Support Jul 8, 2024
@TheValkDokk
Copy link

don't know if this helps but i just having this issue on SM-991N, the scan results doesn't return anything, but on xiaomi 12T, it's fine, i can stream data from the flutter_foreground_task to the main ui thread

@explodus
Copy link

as a specialization, not only in the background, in the background and switched off screen

@TheValkDokk
Copy link

TheValkDokk commented Jul 29, 2024

This is how i do it:

  • Request ignore battery optimization
  • in android manifest, request the type of service of your case
  • use the flutter_foreground_task, have the FlutterBluePlus instance in the TaskHanler itself, not from the main UI thread

My project is private so i can't provide code but i will make an example if i have time

@ConnorDykes
Copy link

@TheValkDokk I am working on a project that desperately needs this, Can I reach out to buy directly for a quick conversation on the topic. I could pay you for your time if need be. My company attempted to contract the owner of this package for support on this but he could not deliver on the contract. You might be a better resource.

@GreedyGrinner
Copy link

On android scanning only works in doze mode if the service is scanning for a specific mac-address or ServiceUUID, otherwise it returns empty. On iOS you need to scan for specific ServiceUUIDs as well. In terms of scanning for all advertising devices i have no found a solution.

@fpozatto
Copy link

fpozatto commented Sep 3, 2024

as a specialization, not only in the background, in the background and switched off screen

You mentioned in the other post that you added a lot of debug information to find where the lib was stopping. Any further luck with that? Were you able to override something?

@explodus
Copy link

explodus commented Oct 2, 2024

A lot of time has passed and I have now solved it without scanning. We changed the hardware and now have a connection and notifications from the device.

The important point to note with iOS is that you set the UIBackgroundModes correctly.

	<key>UIBackgroundModes</key>
	<array>
		<string>bluetooth-central</string>
		<string>bluetooth-peripheral</string>
		<string>fetch</string>
	</array>

The notification from the device comes when the device sends and only when the app is hard killed will no more notifications come.

@Maqcel
Copy link

Maqcel commented Nov 25, 2024

@TheValkDokk can you share an example?

@cos-overclock
Copy link

cos-overclock commented Nov 30, 2024

One way to do this would be to add an option to register the target BLE device as a Companion Device.
I think the problems with using Companion Device are

  • The required callbacks are not supported until Android 12 or later.
  • Pairing and bonding required to register as a Companion Device

https://developer.android.com/develop/connectivity/bluetooth/companion-device-pairing?#keep-awake

I'd like to hear your thoughts on adding Companion Device functionality to FBP.

@3ph
Copy link

3ph commented Dec 16, 2024

On android scanning only works in doze mode if the service is scanning for a specific mac-address or ServiceUUID, otherwise it returns empty. On iOS you need to scan for specific ServiceUUIDs as well. In terms of scanning for all advertising devices i have no found a solution.

You can't even start scanning when the screen is off - startScan reports UNKNOWN_SCAN_ERROR

@chipweinberger It's not possible to recover from the UNKNOWN_SCAN_ERROR even when the app is foregrounded, subsequent startScan invocations would return SCAN_FAILED_ALREADY_STARTED even though the scan was finished or stopScan called... Is this also Android issue rather than FBP issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants