flutter_bluetooth_print_plugin 1.0.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 52

flutter_bluetooth_print_plugin #

A new flutter plugin project.

Getting Started #

This project is a starting point for a Flutter plug-in package, a specialized package that includes platform-specific implementation code for Android and/or iOS.

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

1.0.0 #

  • Connect bluetooth device.

example/lib/main.dart

import 'dart:ffi';

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

void main() => runApp(MyApp());

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

class _MyAppState extends State<MyApp> {

  // 蓝牙状态
  BluetoothState _bluetoothState = BluetoothState.unknown;
  // 设备信息
  List<Map> _peripheralInfoList = [];
  // 设备连接状态
  ConnectState _connectState = ConnectState.not_found_device;

  @override
  void initState() {
    super.initState();
    FlutterBluetoothPrintPlugin.receivePeripheralInfo((value) {
      setState(() {
        _peripheralInfoList.add(value);
      });
    });

    FlutterBluetoothPrintPlugin.receiveConnectState((value) {
      setState(() {
        _connectState = value;
      });
    });
  }

  @override
  void dispose() {
    print('dispose');
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: <Widget>[
               Row(
                 mainAxisAlignment: MainAxisAlignment.spaceAround,
                 children: <Widget>[
                   RaisedButton(
                    child: Text('获取蓝牙状态'),
                    onPressed: () {
                      FlutterBluetoothPrintPlugin.bluetoothState.then((value) {
                        setState(() {
                          _bluetoothState = value;
                          if (value == BluetoothState.poweredOn) { // 扫描设备
                            _scanPeripheral();
                          }
                        });
                      });
                    },
                  ),
                  Container(
                    padding: EdgeInsets.all(10),
                    child: Text(_getConnectStateText()),
                  )
                 ],
               ),
              Container(
                child: Text(_getBluetoothStateText()),
              ),
              Expanded(
                child: ListView.builder(
                  itemCount: _peripheralInfoList.length,
                  itemBuilder: (context, index) {
                    return InkWell(
                      onTap: () {
                        String uuid = _peripheralInfoList[index]['UUID'];
                        FlutterBluetoothPrintPlugin.startConnectPeripheral(uuid, 10);
                      },
                      child: ListTile(
                        title: Text(_peripheralInfoList[index]['name']),
                        subtitle: Text(_peripheralInfoList[index]['UUID']),
                      ),
                    );
                  },
                ),
              )
            ],
          ),
        ),
      ),
    );
  }

  String _getBluetoothStateText() {
    switch (_bluetoothState) {
      case BluetoothState.unknown:
        return '未知';
        break;
      case BluetoothState.unsupported:
        return '蓝牙不支持';
        break;
      case BluetoothState.unauthorized:
        return '蓝牙未授权';
        break;
      case BluetoothState.resetting:
        return '蓝牙正在重置';
        break;
      case BluetoothState.poweredOff:
        return '蓝牙未开启';
        break;
      case BluetoothState.poweredOn:
        return '蓝牙已开启';
        break;
      default:
        return '';
    }
  }

  String _getConnectStateText() {
    switch (_connectState) {
      case ConnectState.not_found_device:
        return '未发现设备';
        break;
      case ConnectState.connecting:
        return '正在连接...';
        break;
      case ConnectState.connected:
        return '连接成功';
        break;
      case ConnectState.failure:
        return '连接失败';
        break;
      case ConnectState.timeout:
        return '连接超时';
        break;
      default:
        return '';
    }
  }

  // 扫描设备
  _scanPeripheral() async {
    FlutterBluetoothPrintPlugin.scanPeripheral(null);
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  flutter_bluetooth_print_plugin: ^1.0.0

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:flutter_bluetooth_print_plugin/flutter_bluetooth_print_plugin.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
12
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
83
Overall:
Weighted score of the above. [more]
52
Learn more about scoring.

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

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Health issues and suggestions

Document public APIs. (-1 points)

29 out of 29 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Fix lib/flutter_bluetooth_print_plugin.dart. (-0.50 points)

Analysis of lib/flutter_bluetooth_print_plugin.dart reported 1 hint:

line 2 col 8: Unused import: 'dart:ffi'.

Maintenance suggestions

The package description is too short. (-17 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

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