Git Product home page Git Product logo

Comments (3)

Sunbreak avatar Sunbreak commented on July 27, 2024

Could try WidgetsFlutterBinding.ensureInitialized(); first then call QuickBlue methods afterwards?

from quick_blue.

robnorr avatar robnorr commented on July 27, 2024

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.

robnorr avatar robnorr commented on July 27, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.