Coder Social home page Coder Social logo

manuel75o / flutter_bluetooth_manager Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 0.0 628 KB

The aim of FlutterBluetoothManager is to handle multiple devices and their streams with ease. It is built on top of the Flutter Bluetooth Package FlutterBluePlus.

Dart 38.06% Kotlin 0.15% Ruby 3.48% Swift 2.30% Objective-C 0.05% CMake 23.28% C++ 28.62% C 1.77% HTML 2.29%

flutter_bluetooth_manager's Introduction

Chat

BluetoothManager

The aim of FlutterBluetoothManager is to handle multiple devices and their streams with ease. It is built on top of the Flutter Bluetooth Package FlutterBluePlus.

BluetoothManager

The BluetoothManager creates a DeviceModel. This represents the device and it's corresponding Services and Characteristics. A device is unique to a DeviceModel. There can be multiple Services and Characteristics per DeviceModel.

The general Structure looks like this:

class DeviceModel {
  final BluetoothDevice device;
  final List<BluetoothService> services;
  final List<CharacteristicStream> characteristicsStream;
  DeviceModel(this.device, this.services, this.characteristicsStream);
}

CharacteristicStream contains the BluetoothCharacteristic and Info wether it's Stream is initialized.

class CharacteristicStream {
  final BluetoothCharacteristic characteristic;
  bool isInitialized;
  CharacteristicStream({
    required this.characteristic,
    this.isInitialized = false,
  });
}

Features

Following pictures shows the example of the BluetoothManager.

Search for devices Connect multiple devices Open single Stream Open multiple Streams

Getting Started

To get started you simply create an instance of the

BluetoothManager:

BluetoothManager bluetoothManager = BluetoothManager();

Connect a device:

bluetoothManager.connectDevice(BluetoothDevice device);

Disconnect a device:

bluetoothManager.disconnectDevice(BluetoothDevice device);

Check device ConnectionState:

A common problem is, if you accidently lose connection to a device it can lead to an exeption. With this function you constantly check the device connectionState for all connected devices and if it loses a connection the device will be disconnected and removed from the DeviceModel preventing exeptions or potential errors.

bluetoothManager.checkDeviceConnection();

Get Characteristic:

To retrieve a Characteristic for subscribing to a stream or writing message you can perform this by calling getCharacteristic. Since there can be multiple Characteristics per Service you can choose it by Number.

bluetoothManager.getCharacteristic(BluetoothDevice device, int characteristicNumber);

Open Stream

To receive a stream you need to call the openStream function. It takes a characteristic and returns/yields a Stream<List<int>>.

bluetoothManager.openStream(BluetoothCharacteristic? characteristic);

StreamHandler

You can handle multiple streams by simply calling the streamHandler. It takes the device index value and a stream as input. You need to use the stream of openStream.

bluetoothManager.streamHandler(index, stream);

Get Stream

Retrieve a stream for Streambuilder by its device index

bluetoothManager.getStream(index)

Close a stream

bluetoothManager.closeStream(index)

Additional information

Tested with IOS17.

flutter_bluetooth_manager's People

Contributors

manuel75o avatar

Stargazers

Yuhei avatar Jure Speh avatar OoZaineee avatar MikesDevCorner avatar  avatar Ryan Christopher Bahillo avatar

Watchers

 avatar

flutter_bluetooth_manager's Issues

Got problems when running it, most likely some dependencies and environment setup issues, pleaes help

Hi Manuel,

I got it cloned to my local directory, and I have tried the followings:

  1. running it on edge and chrome, and it seems not to find any bluetooth devices while I have two cell phones are in discovery mode
  2. tried to run it with android "Pixel_3a_API_34_extension_level_7_x86_64 mobile emulator", I have got the following in DEBUG CONSOLE:
    Launching lib\main.dart on sdk gphone64 x86 64 in debug mode...
    C:\Users\rober\MyDevSandBox\flutter_bluetooth_manager\example\android\app\src\debug\AndroidManifest.xml Error:
    uses-sdk:minSdkVersion 19 cannot be smaller than version 21 declared in library [:flutter_blue_plus] C:\Users\rober\MyDevSandBox\flutter_bluetooth_manager\example\build\flutter_blue_plus\intermediates\merged_manifest\debug\AndroidManifest.xml as the library might be using APIs not available in 19
    Suggestion: use a compatible library with a minSdk of at most 19,
    or increase this project's minSdk version to at least 21,
    or use tools:overrideLibrary="com.lib.flutter_blue_plus" to force usage (may lead to runtime failures)

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ':app:processDebugMainManifest'.

Manifest merger failed : uses-sdk:minSdkVersion 19 cannot be smaller than version 21 declared in library [:flutter_blue_plus] C:\Users\rober\MyDevSandBox\flutter_bluetooth_manager\example\build\flutter_blue_plus\intermediates\merged_manifest\debug\AndroidManifest.xml as the library might be using APIs not available in 19
Suggestion: use a compatible library with a minSdk of at most 19,
or increase this project's minSdk version to at least 21,
or use tools:overrideLibrary="com.lib.flutter_blue_plus" to force usage (may lead to runtime failures)

  • Try:

Run with --stacktrace option to get the stack trace.
Run with --info or --debug option to get more log output.
Run with --scan to get full insights.

BUILD FAILED in 6s
─ Flutter Fix ─────────────────────────────────────────────────────────────────────────────────┐
│ The plugin flutter_blue_plus requires a higher Android SDK version. │
│ Fix this issue by adding the following to the file │
│ C:\Users\rober\MyDevSandBox\flutter_bluetooth_manager\example\android\app\build.gradle: │
│ android { │
│ defaultConfig { │
│ minSdkVersion 21 │
│ } │
│ } │
│ │
│ Following this change, your app will not be available to users running Android SDKs below 21. │
│ Consider searching for a version of this plugin that supports these lower versions of the │
│ Android SDK instead. │
│ For more information, see: │
https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration
└───────────────────────────────────────────────────────────────────────────────────────────────┘
Exception: Gradle task assembleDebug failed with exit code 1

Please help... Appreciated...

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.