my_bluetooth 1.0.2+2 copy "my_bluetooth: ^1.0.2+2" to clipboard
my_bluetooth: ^1.0.2+2 copied to clipboard

My Bluetooth - Connecting and transferring data to other devices using Bluetooth

Buy Me A Coffee

Bluetooth Connection Plugin for Flutter #

A Flutter plugin for connecting and transferring data to other devices using Bluetooth. This package simplifies Bluetooth communication by providing an easy-to-use API for scanning, connecting, and exchanging

Features #

  • Scan for nearby Bluetooth Device
  • Connect to bluetooth device
  • Send and receive data
  • Customizable connection and transfer
Android iOS Description
turnOn() Turn on Bluetooth
adapterState Get the status of adapter
bondedDevices Get Bonded device
discoveryState
startScan() Start scan nearby device
stopScan() Stop scan while device is scanning
lastScanResults
scanResults
connectionState Device connection status
sendingState
connect() (auto connect in IOS)
disconnect()
send data

Permission #

In this plugin we use permissions: Bluetooth, Bluetooth scan, bluetooth connect, access_fine_location

Usage #

Example

To use this plugin: #

dependencies:
    flutter:
        sdk: flutter
    my_bluetooth: ^lastest_version
  • Then, run
flutter pub get
  • Import the package
import 'package:my_bluetooth/my_bluetooth.dart';

Set up for IOS #

Due to ios security issue, name of printer must be added in the ios/Runner/Info.plist

<key>NSBluetoothAlwaysUsageDescription</key>
	<string>This app always needs Bluetooth to function</string>
	<key>NSBluetoothPeripheralUsageDescription</key>
	<string>This app needs Bluetooth Peripheral to function</string>
	<key>UISupportedExternalAccessoryProtocols</key>
	<array>
		<string>net.dsgl.nailpopPro.protocol</string>
		//use your protocol instead
	</array>
	<key>UIBackgroundModes</key>
	<array>
		<string>bluetooth-central</string>
		<string>external-accessory</string>
		<string>fetch</string>
		<string>remote-notification</string>
	</array>

Scan for Device: #

final _myBluetooth = MyBluetooth();
// The scan function can filter to scan only devices with a given name.
void scan() async{
  await _myBluetooth.startScan(
      withKeywords: [
// "Nailpop", "Nailpop Pro",
// "Snap# Kiosk", "DMP"
//     "image box", "android_1"
      ]);

}
  • How to get list device? Use StreamSubscription to get list device return from function scan()
 late StreamSubscription<List<BluetoothDevice>> _scanResultsSubscription;
  List<BluetoothDevice> _scanResults = [];
  
  @override
  void initState() {
    super.initState();
    _scanResultsSubscription = _myBluetooth.scanResults.listen((results) {
      _scanResults = results;
      if (mounted) {
        setState(() {});
      }
    }, onError: (e) {});
  }

  @override
  void dispose() {
    _scanResultsSubscription.cancel();
    super.dispose();
  }

Connect and disconnect #

await _myBluetooth.connect(remoteId:  _scanResults.first.remoteId);
await _myBluetooth.disconnect();

State #

//  Bluetooth state:
MPBluetoothAdapterState _adapterState = MPBluetoothAdapterState.unknown;
// Connection state
MPConnectionStateEnum _connectState = MPConnectionStateEnum.disconnected;

Send data #

Use function sendCmd Future<bool> sendCmd(List<int> byte, {int size = 34}) to send data as bytes example:

static const List<int> data = [0x1B, 0x2A, 0x44, 0x53];
_myBluetooth.sendCmd(data);

Besides, we have made some functions to send text and image data for convenience.

await _myBluetooth.sendText(value: "Hello World");
await _myBluetooth.sendFile(pathImage: image?.path)

Contribute #

Many thanks to the contribution on the plugin with ThaoDoan and DucNguyen

5
likes
160
points
9
downloads

Publisher

verified publisherwongcoupon.com

Weekly Downloads

My Bluetooth - Connecting and transferring data to other devices using Bluetooth

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on my_bluetooth