How to use flutter_blue with iOS?
Solution 1
I tried flutter_blue for at least two months and I could not get to work in any reliable way. I don't want to go into the details but you can search for my ID in the flutter_blue github repository and get a pretty good picture.
I finally switched to flutter_ble_lib https://pub.dev/packages/flutter_ble_lib and had all my stuff working in days. I had my code pretty well abstracted and switching the lib was no problem. All is working on Android and IOS. Give it a try.
Solution 2
I tried a friends version of the code that worked for him but not me.
See here
Since that code failed in the same spot, I examined the stack trace some more and I noticed an earlier failure in the stack trace.
TCC`__CRASHING_DUE_TO_PRIVACY_VIOLATION__:
As a result, I added the following
<key>NSBluetoothAlwaysUsageDescription</key>
<string>This app uses bluetooth</string>
To the ios/Runner/Info.plist file
.
After that it works.
netskink
Updated on December 22, 2022Comments
-
netskink over 1 year
I am trying to use flutter and flutter_blue with iOS.
I am getting a crash dump with each channel. I'm new to flutter so perhaps this is a simple problem. I know people use this library, so it must be in my usage. Its odd that despite the channel selection it produces the same crash dump each time.
Steps to reproduce
OSX: Version 0.15.5 (19F101) Xcode : Version 11.5 (11E608c)
Using the example on the main page and the actual GitHub src as a guide. These URLs:
- https://pub.dev/packages/flutter_blue#-example-tab-
- https://github.com/pauldemarco/flutter_blue/tree/master/example/lib
- Specify flutter channel and update
I have tried each flutter channel. Each channel produces the same crash dump.
$ flutter channel stable $ flutter upgrade Flutter 1.17.4 • channel stable • https://github.com/flutter/flutter.git Framework • revision 1ad9baa8b9 (9 days ago) • 2020-06-17 14:41:16 -0700 Engine • revision ee76268252 Tools • Dart 2.8.4 $ flutter doctor Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, v1.17.4, on Mac OS X 10.15.5 19F101, locale en-US) [✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3) [✓] Xcode - develop for iOS and macOS (Xcode 11.5) [✓] Android Studio (version 4.0) [!] VS Code (version 1.24.1) ✗ Flutter extension not installed; install from https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter [✓] Connected device (1 available) ! Doctor found issues in 1 category.
- Create a stub project
For simplicity mimic the example.
$ flutter create flutter_blue_example $ cd flutter_blue_example
- Open project in Xcode and make runtime changes
$ open ios/Runner.xcworkspace/
Select TARGETS: Runner then General tab to modify runtime API
General->Deployment Info->Target == iOS 13.5
Select TARGETS: Runner then Signing & Capabilities tab to modify signing bundle identifier and team name.
I noticed in error messages some were related to iOS 8.0 vs iOS 10.0 API capabilities. As a result, I chose the latest API iOS 13.
-
Correct bundle identifier and signing team name
-
Run sample on phone via Xcode.
-
Stop app close Xcode. Run with cli.
-
close and test with cli
$ flutter run
-
When demo app runs, press q to quit
- Modify pubspec.yaml file so the dependency section looks like this:
dependencies: flutter_blue: ^0.7.2 flutter: sdk: flutter
- Remove the lib/main.dart stub/template code and replace with example code from git repo.
$ cd lib $ rm main.dart $ curl https://raw.githubusercontent.com/pauldemarco/flutter_blue/master/example/lib/main.dart --output main.dart $ curl https://raw.githubusercontent.com/pauldemarco/flutter_blue/master/example/lib/widgets.dart --output widgets.dart
- Clean and install dependencies
Each time I change channels I do this same process.
$ cd .. # The rm iOS/Podfile command is only needed when # switching to channel dev # rm iOS/Podfile $ flutter pub get $ flutter clean
- Attempt to build and run the example code
$ flutter run Launching lib/main.dart on JFD_X in debug mode... Automatically signing iOS for device deployment using specified development team in Xcode project: Z84M4MZYKF Running pod install... 1.6s Running Xcode build... └─Compiling, linking and signing... 14.3s Xcode build done. 29.7s Installing and launching... ⣻
On the iPhone it flashes the screen which appears that the app starts, but quickly returns to home screen. The prompt never returns and the command must be exited with
ctrl-c
becauseq
does not respond.Running with verbose options provides this info.
<stuff snipped> [ +155 ms] success [ ] (lldb) safequit [ +109 ms] Process 20400 detached [ +30 ms] Application launched on the device. Waiting for observatory port. [ +9 ms] Attempting to forward device port 49232 to host port 1024 [ ] executing: /Users/davis/progs/flutter/flutter/bin/cache/artifacts/usbmuxd/iproxy 1024 49232 850f92882e33715d639b466a0ba9a01aeb601823 [+1011 ms] Forwarded port ForwardedPort HOST:1024 to DEVICE:49232 [ +17 ms] HttpException: Connection closed before full header was received, uri = http://localhost:1024/ws [+2008 ms] HttpException: , uri = http://localhost:1024/ws [+2007 ms] HttpException: , uri = http://localhost:1024/ws [+2008 ms] HttpException: , uri = http://localhost:1024/ws [+2011 ms] HttpException: , uri = http://localhost:1024/ws [+2006 ms] Failed to connect directly, falling back to mDNS [ +5 ms] Checking for advertised Dart observatories... [+5020 ms] No pointer records found. [ +2 ms] mDNS query failed. Checking for an interface with a ipv4 link local address. [ +2 ms] Found interface "en0": [ ] Bound address: "192.168.86.53" [ ] Found interface "en5": [ ] Bound address: "169.254.128.136" link local [ ] An interface with an ipv4 link local address was found. [ ] Failed to connect with mDNS, falling back to log scanning
Open xcode again and try to run from Xcode.
$ open ios/Runner.xcworkspace/
Run the app and it gives the following crash dump
Channel Stable
libsystem_kernel.dylib`__abort_with_payload: 0x1b44c97ec <+0>: mov x16, #0x209 0x1b44c97f0 <+4>: svc #0x80 -> 0x1b44c97f4 <+8>: b.lo 0x1b44c9810 ; <+36> Thread 3: signal SIGABRT 0x1b44c97f8 <+12>: stp x29, x30, [sp, #-0x10]! 0x1b44c97fc <+16>: mov x29, sp 0x1b44c9800 <+20>: bl 0x1b44a9ad0 ; cerror_nocancel 0x1b44c9804 <+24>: mov sp, x29 0x1b44c9808 <+28>: ldp x29, x30, [sp], #0x10 0x1b44c980c <+32>: ret 0x1b44c9810 <+36>: ret
version check
huo-yang~/progs/flutter/flutter-testy/flutter_blue_example$ flutter --version Flutter 1.17.4 • channel stable • https://github.com/flutter/flutter.git Framework • revision 1ad9baa8b9 (12 days ago) • 2020-06-17 14:41:16 -0700 Engine • revision ee76268252 Tools • Dart 2.8.4
Channel Beta
Gives identical stack trace as above.
version check
huo-yang~/progs/flutter/flutter-testy/flutter_blue_example$ flutter --version Flutter 1.19.0-4.2.pre • channel beta • https://github.com/flutter/flutter.git Framework • revision 9b9b543d92 (7 days ago) • 2020-06-22 12:19:28 -0700 Engine • revision 9a28c3bcf4 Tools • Dart 2.9.0 (build 2.9.0-14.1.beta)
Channel Dev
Gives identical stack trace as above.
version check
$ flutter run --version Flutter 1.20.0-2.0.pre • channel dev • https://github.com/flutter/flutter.git Framework • revision 15a28159bc (6 days ago) • 2020-06-23 04:52:58 -0700 Engine • revision 91a63d6a44 Tools • Dart 2.9.0 (build 2.9.0-19.0.dev 7e72c9ae7e)
-
ByteMe almost 4 yearsSo who gets the bounty?
-
netskink almost 4 yearsI’m not sure. I spent reputation points. It did not refund / award to me. Each day the bounty expires a day. When zero I’ll see what happens.
-
netskink almost 4 yearsI will look at it. My bounty expires tomorrow. Since you at least gave me an answer I awarded you the bounty. Note I had already resolved it. I just hated to see the bounty be wasted. Thanks again.
-
Ride Sun almost 4 yearsWell, thx. I still would give the other lib ago. Just saying.
-
netskink almost 4 yearsOh yes. I did try flutter blue and it worked but a couple of times but it crashed as you said. I’ll switch as you suggest. Thanks again.