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]: iOS Core Bluetooth Background Processing support #846

Open
William-Geng opened this issue Apr 9, 2024 · 14 comments
Open

[Feature]: iOS Core Bluetooth Background Processing support #846

William-Geng opened this issue Apr 9, 2024 · 14 comments
Labels
feature Feature request

Comments

@William-Geng
Copy link

FlutterBluePlus Version

1.31.17

Flutter Version

3.19.3

What OS?

iOS

OS Version

17

Bluetooth Module

nordic nRF52832

What is your feature request?

It would be nice to support iOS core bluetooth background execution mode as described here: https://developer.apple.com/library/archive/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html.

I believe there is a PR for flutter_blue that was never merged: pauldemarco/flutter_blue#191 and pauldemarco/flutter_blue#210.

Logs

none
@William-Geng William-Geng added the feature Feature request label Apr 9, 2024
@chipweinberger
Copy link
Owner

feel free to create new PRs and test them

@MrCsabaToth
Copy link
Contributor

MrCsabaToth commented May 7, 2024

FYI there was a https://github.com/espresso3389/flutter_blue fork of the original pauldemarco flutter_blue back in the day which dealt with foreground task issue as well, along with some emphasis on iOS and also permissions. But seems like that fork died too some times after flutter_blue, but it might be useful to see what PRs they merged and what they did for this.

I'll need some foreground support as well in my app at some point - highly requested -, however this seems to be a major undertaking in my case. More specifically at my app level this looks to be a significant architectural change. Even though there are plugins such as https://pub.dev/packages/flutter_foreground_task/ you can see it's unfortunately not a simple yaml include and a plop in of a code snippet: it's a detailed 5+ step staged procedure. I also don't see a way - once I convert my app - to have that functionality be optional. That's important because if such function would be at the FBP plugin level, it could happen not everyone would want it: because for example it needs extra manifest declarations, you'd need to select type of service, extra permission shenanigans, declaring callbacks, and the list goes on and on.

But ultimately I'd need to tackle this as well for user request. I wonder if it's even doable at FBP level, it'll be hard enough at app level. I'm curious what is your use-case and plans.

@Codel1417
Copy link

My initial testing with the app in the background, using a 15 second keep awake ping, kept my app open.

@h4nkyn
Copy link

h4nkyn commented Jun 13, 2024

What type of keep awake ping are you talking about? Like a characteristic value change?

@Codel1417
Copy link

Codel1417 commented Jun 13, 2024

What type of keep awake ping are you talking about? Like a characteristic value change?

Yeah. Having a characteristic value change wakes the app for 10-15 seconds. If you do this enough the app stays open. The device I connect to uses 2 characteristics for a read/write command setup so I just sent ping which gets a response.

@drolpi
Copy link

drolpi commented Jul 2, 2024

@chipweinberger are there any updates on this? I absolutely need this feature! Otherwise, I have to completely rebuild my app in a different language with a different framework. Can someone please implement IOS BLE State Restoration.

@chipweinberger
Copy link
Owner

chipweinberger commented Jul 2, 2024

@drolpi

Can someone please implement IOS BLE State Restoration.

I don't need this feature, so I wont be working on it.

If you really don't want to implement it yourself, I could implement it for you, but for a fee. email me if interested.

@MrCsabaToth
Copy link
Contributor

MrCsabaToth commented Jul 3, 2024

@chipweinberger are there any updates on this? I absolutely need this feature! Otherwise, I have to completely rebuild my app in a different language with a different framework. Can someone please implement IOS BLE State Restoration.

BLE State Restoration and background services are two different things, although may not be completely unrelated: in case the the background service gets yeeted (this part of Android is not standardized https://dontkillmyapp.com/) then restoration may come into play?

@MrCsabaToth
Copy link
Contributor

I think Chip did some work related to reconnection if I'm not mistaken, however BLE State Restoration sounds more formal.

@chipweinberger
Copy link
Owner

#939

there was this nearly complete PR recently

@chipweinberger
Copy link
Owner

chipweinberger commented Aug 27, 2024

I've pushed this commit (12c17a6) , which should enable this feature. But I have not been able to get it to work in actual testing.

  1. when the app is killed, the app is not restored ❌
  2. when the phone is rebooted, the app is not restored ❌

If anyone else wants to try it, please do, and report back. To use it, just call FlutterBluePlus.setOptions, like this

void main() {
  FlutterBluePlus.setOptions(restoreState: true);
  FlutterBluePlus.setLogLevel(LogLevel.verbose, color: true);
  runApp(const FlutterBlueApp());
}

You also need to update the info.plist

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

@chipweinberger
Copy link
Owner

released version 1.32.13 with State Preservation

@Scottie-Fischer
Copy link

  • when the app is killed, the app is not restored ❌
  • when the phone is rebooted, the app is not restored ❌

I made some minor tweaks using your latest update and was able to get it to restore the CBCentralManager and peripherals.
I did see some issues where when I restore the peripheral and save it in the connectedDevices list that it times out when trying to discover services or subscribe to characteristic using setNotifyingValue. Have not pinned down the issues, but will make a PR.

Additionally I created a restoration state that tells the app whether or not it was restored or the initial start. This may be useful for diverging behaviors, but not technically required.

@chipweinberger
Copy link
Owner

awesome!

i look forward to the PR!

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

No branches or pull requests

7 participants