varunsat / flutter_bluetooth_sharing

Other
0 stars 0 forks source link

Flutter Bluetooth Sharing Plugin

A Flutter plugin for sharing data over Bluetooth between two mobile devices.

Flutter Version

Features

Installation

Add the following line to your pubspec.yaml file:

dependencies: flutter_bluetooth_sharing: ^1.0.0

Example


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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Bluetooth Sharing Example'),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              ElevatedButton(
                onPressed: _startDiscovery,
                child: Text('Start Discovery'),
              ),
              ElevatedButton(
                onPressed: _startAdvertising,
                child: Text('Start Advertising'),
              ),
              ElevatedButton(
                onPressed: _sendData,
                child: Text('Send Data'),
              ),
            ],
          ),
        ),
      ),
    );
  }

  void _startDiscovery() async {
    try {
      await FlutterBluetoothSharing.startDiscovery();
      print('Discovery started.');
    } catch (e) {
      print('Error starting discovery: $e');
    }
  }

  void _startAdvertising() async {
    try {
      await FlutterBluetoothSharing.startAdvertising();
      print('Advertising started.');
    } catch (e) {
      print('Error starting advertising: $e');
    }
  }

  void _sendData() async {
    try {
      String data = 'Hello, this is data from Device A!';
      await FlutterBluetoothSharing.sendData(data);
      print('Data sent successfully: $data');
    } catch (e) {
      print('Error sending data: $e');
    }
  }
}