Comments (3)
Could try WidgetsFlutterBinding.ensureInitialized();
first then call QuickBlue
methods afterwards?
from quick_blue.
Unfortunately that doesn't seem to have changed anything. I've tried to create a minimal example of the issue here:
import 'dart:async'; // for StreamSubscription
import 'dart:typed_data'; // for Uint8List
import 'package:flutter/material.dart';
import 'package:quick_blue/quick_blue.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
BluetoothConnection bluetoothConnector = BluetoothConnection();
void _buttonClick() {
setState(() {
bluetoothConnector.connect();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
),
),
floatingActionButton: FloatingActionButton(
onPressed: _buttonClick,
tooltip: 'Connect',
child: const Icon(Icons.add),
),
);
}
}
class BluetoothConnection
{
BluetoothConnection()
{
_subscription = QuickBlue.scanResultStream.listen((result) {
if (result.name == "BluetoothDevice") {
device = result;
}
});
QuickBlue.setConnectionHandler(_handleConnectionChange);
QuickBlue.setServiceHandler(_handleServiceDiscovery);
QuickBlue.setValueHandler(_handleValueChange);
}
StreamSubscription<BlueScanResult>? _subscription;
BlueScanResult? device;
void connect()
{
QuickBlue.startScan();
while(device == null);
QuickBlue.connect(device!.deviceId);
}
void disconnect() {
QuickBlue.disconnect(device!.deviceId);
_subscription?.cancel();
QuickBlue.setValueHandler(null);
QuickBlue.setServiceHandler(null);
QuickBlue.setConnectionHandler(null);
}
void _handleConnectionChange(String deviceId, BlueConnectionState state) {
print('_handleConnectionChange $deviceId, $state');
}
void _handleServiceDiscovery(String deviceId, String serviceId) {
print('_handleServiceDiscovery $deviceId, $serviceId');
}
void _handleValueChange(String deviceId, String characteristicId, Uint8List value) {
print('_handleValueChange $deviceId, $characteristicId, ${String.fromCharCodes(value)}');
}
}
When I run this, I get the LateInitializationError immediately. Adding WidgetsFlutterBinding.ensureInitialized();
to the constructor of BluetoothConnection
doesn't change anything for me.
from quick_blue.
Alright, I found the issue. Unfortunately my dart/flutter understanding isn't good enough to know what/why/how this is set, or why it causes an issue but I'll post the fix here for anyone else:
In .dart_tool/package_config_subset
change
file:///Users/user/.pub-cache/hosted/pub.dartlang.org/quick_blue_platform_interface-0.3.2/
file:///Users/user/.pub-cache/hosted/pub.dartlang.org/quick_blue_platform_interface-0.3.2/lib/
to
file:///Users/user/.pub-cache/hosted/pub.dartlang.org/quick_blue_platform_interface-0.3.1+1/
file:///Users/user/.pub-cache/hosted/pub.dartlang.org/quick_blue_platform_interface-0.3.1+1/lib/
And in .dart_tool/package_config
change
"rootUri": "file:///Users/user/.pub-cache/hosted/pub.dartlang.org/quick_blue_platform_interface-0.3.2",
to
"rootUri": "file:///Users/user/.pub-cache/hosted/pub.dartlang.org/quick_blue_platform_interface-0.3.1+1",
btw, I forgot to mention in the original post I was building macos when I encountered this error.
from quick_blue.
Related Issues (20)
- Stream for host bluetooth hardware status? HOT 1
- Android device disconnect error HOT 13
- Unhandled Exception: LateInitializationError: Field '_instance@609118451' has not been initialized HOT 5
- If something else on the computer is using BTLE, quick_blue throws EXCEPTION_ACCESS_VIOLATION_READ HOT 3
- Microsoft.Windows.CppWinRT.props was not found in Windows 10 HOT 6
- Possibility to update to bluez 0.8.0 package HOT 3
- MacOS no devices are found HOT 11
- Multiple devices connected on the same printer HOT 1
- error on Android HOT 1
- location permission is always denied. HOT 5
- Bad deviceId causes hard crash HOT 3
- The Flutter application cannot start if it has this plugin and there is no bluetooth dongle inserted into the Windows computer. HOT 4
- Request: Add option to filter for service uuid
- Please upgrade `bluez` dependency to version 0.8.0
- Connection response lags and hangs up
- Exception Unhandled HOT 1
- Guidance on testing / MissingPluginException event.scanResult
- [Windows] Unhandled Exception: PlatformException(IllegalArgument, Unknown devicesId: .... , null, null) HOT 2
- Help wanted HOT 1
- ScanResult always empty on Android tablet
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from quick_blue.