bluetoothadapter 1.1.3

  • Readme
  • Changelog
  • Example
  • Installing
  • new71

bluetoothadapter #

A Flutter plugin to expose the features of Bluetooth Adapter for Android only.

Objective #

The primary purpose of this project was to communicate between flutter and Rasburry Pie using Bluetooth.

Main Features #

  • Setting up a UUID from user end.
  • Checking Bluetooth connection status and giving alerts if its off or not right.
  • Getting list of paired devices.
  • Get a particular paired device info.
  • Start Bluetooth server.
  • Start Bluetooth client.
  • Start Bluetooth client.
  • Send message to a connected device.
  • Stream for listening to received messages.
  • Stream for listening to connection status (CONNECTED, CONNECTING, CONNECTION FAILED, LISTENING, DISCONNECTED).

Getting Started #

For a full example please check /example folder. Here are only the most important parts of the code to illustrate how to use the library.

//Initiatinng the bluetooth adapter
Bluetoothadapter flutterbluetoothadapter = Bluetoothadapter();

//Listening to the connection status listener
String _connectionStatus = "NONE";
StreamSubscription _btConnectionStatusListener =flutterbluetoothadapter.connectionStatus().listen((dynamic status) {
  setState(() {
    _connectionStatus = status.toString();
  });
});

//Listening to the recieved messages
String _recievedMessage;
StreamSubscription _btReceivedMessageListener = flutterbluetoothadapter.receiveMessages().listen((dynamic newMessage) {
  setState(() {
    _recievedMessage = newMessage.toString();
  });
});

Getting paired devices

List<BtDevice> devices = await flutterbluetoothadapter.getDevices();

Getting paired device

await flutterbluetoothadapter.getDevice(deviceAddress);

Starting BT server

await flutterbluetoothadapter.startServer();

Starting BT client

flutterbluetoothadapter.startClient(devices.indexOf(element), true);

Sending message

flutterbluetoothadapter.sendMessage(messageString);

Check BT connection

await flutterbluetoothadapter.checkBluetooth(); //returns bool

Set UUID

flutterbluetoothadapter.initBlutoothConnection(uuid);

1.1.3 #

  • Improved documentation and hints

1.1.2 #

  • Updated README.md

1.1.1 #

  • Added Disconnected connection status type

1.1.0 #

  • Fixed channel mismatch errors

1.0.0 #

  • Added bluetooth functionality for android.

example/lib/main.dart

import 'dart:async';

import 'package:bluetoothadapter/bluetoothadapter.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  Bluetoothadapter flutterbluetoothadapter = Bluetoothadapter();
  StreamSubscription _btConnectionStatusListener, _btReceivedMessageListener;
  String _connectionStatus = "NONE";
  List<BtDevice> devices = [];
  String _recievedMessage;
  TextEditingController _controller = TextEditingController();

  @override
  void initState() {
    super.initState();
    flutterbluetoothadapter
        .initBlutoothConnection("20585adb-d260-445e-934b-032a2c8b2e14");
    flutterbluetoothadapter
        .checkBluetooth()
        .then((value) => print(value.toString()));
    _startListening();
  }

  _startListening() {
    _btConnectionStatusListener =
        flutterbluetoothadapter.connectionStatus().listen((dynamic status) {
      setState(() {
        _connectionStatus = status.toString();
      });
    });
    _btReceivedMessageListener =
        flutterbluetoothadapter.receiveMessages().listen((dynamic newMessage) {
      setState(() {
        _recievedMessage = newMessage.toString();
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Flexible(
                  fit: FlexFit.tight,
                  child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: RaisedButton(
                      onPressed: () async {
                        await flutterbluetoothadapter.startServer();
                      },
                      child: Text('LISTEN'),
                    ),
                  ),
                ),
                Flexible(
                  fit: FlexFit.tight,
                  child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: RaisedButton(
                      onPressed: () async {
                        devices = await flutterbluetoothadapter.getDevices();
                        setState(() {});
                      },
                      child: Text('LIST DEVICES'),
                    ),
                  ),
                )
              ],
            ),
            Text("STATUS - $_connectionStatus"),
            Padding(
              padding: const EdgeInsets.symmetric(
                horizontal: 8.0,
                vertical: 20,
              ),
              child: ListView(
                shrinkWrap: true,
                children: _createDevices(),
              ),
            ),
            Text(
              _recievedMessage ?? "NO MESSAGE",
              style: TextStyle(fontSize: 24),
            ),
            Row(
              children: <Widget>[
                Flexible(
                  flex: 4,
                  fit: FlexFit.tight,
                  child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: TextField(
                      controller: _controller,
                      decoration: InputDecoration(hintText: "Write message"),
                    ),
                  ),
                ),
                Flexible(
                  fit: FlexFit.tight,
                  child: Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: RaisedButton(
                      onPressed: () {
                        flutterbluetoothadapter.sendMessage(
                            _controller.text ?? "no msg",
                            sendByteByByte: false);
//                        flutterbluetoothadapter.sendMessage(".",
//                            sendByteByByte: true);
                        _controller.text = "";
                      },
                      child: Text('SEND'),
                    ),
                  ),
                )
              ],
            )
          ],
        ),
      ),
    );
  }

  _createDevices() {
    if (devices.isEmpty) {
      return [
        Center(
          child: Text("No Paired Devices listed..."),
        )
      ];
    }
    List<Widget> deviceList = [];
    devices.forEach((element) {
      deviceList.add(
        InkWell(
          key: UniqueKey(),
          onTap: () {
            flutterbluetoothadapter.startClient(devices.indexOf(element), true);
          },
          child: Container(
            padding: EdgeInsets.all(4),
            decoration: BoxDecoration(border: Border.all()),
            child: Text(
              element.name.toString(),
              style: TextStyle(fontSize: 18),
            ),
          ),
        ),
      );
    });
    return deviceList;
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  bluetoothadapter: ^1.1.3

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:bluetoothadapter/bluetoothadapter.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
42
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
71
Learn more about scoring.

We analyzed this package on Jul 6, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform linux

Because of import path [package:bluetoothadapter/bluetoothadapter.dart] that declares support for platforms: android, ios

Package does not support Flutter platform macos

Because of import path [package:bluetoothadapter/bluetoothadapter.dart] that declares support for platforms: android, ios

Package does not support Flutter platform web

Because of import path [package:bluetoothadapter/bluetoothadapter.dart] that declares support for platforms: android, ios

Package does not support Flutter platform windows

Because of import path [package:bluetoothadapter/bluetoothadapter.dart] that declares support for platforms: android, ios

Package not compatible with SDK dart

because of import path [bluetoothadapter] that is in a package requiring null.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12 1.14.13
meta 1.1.8 1.2.1
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8
Dev dependencies
flutter_test