skip to Main Content

We have to create a flutter app how connect to other bluetooth motherboard. Our work is similar to an application for connected light bulbs.

We tried to creat emulator bluetooth app on phone to try to communicate between two phone. But it’s impossible.

So we have few questons :

  • Which library is best for ?
  • How can I test it without raspberry pi or something like that ?
  • How to make sure we can only connect to devices through my app

Thank you, have a great day

2

Answers


  1. i been working with flutter and ble for a while now.

    I personally recommend flutter_blue_plus, it works pretty great for bluetooth low energy.
    Second, i don’t think you can’t test it without a dev module. The ESP32 works great and generally it’s not so expensive. NimBle works for the bluetooth of the board.
    At least, i recommend you use a "password" when you tried to connect, and the app is the only one who send it, idk.

    Login or Signup to reply.
  2. I have also created the same kind of demo using blufi_plugin for scanning and connecting with ble devices.

    import 'dart:convert';
    
    import 'package:flutter/material.dart';
    import 'dart:async';
    
    import 'package:flutter/services.dart';
    import 'package:blufi_plugin/blufi_plugin.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatefulWidget {
      @override
      _MyAppState createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
      String contentJson = 'Unknown';
    
      Map<String, dynamic> scanResult = Map<String, dynamic>();
    
      @override
      void initState() {
        super.initState();
        initPlatformState();
    
        BlufiPlugin.instance.onMessageReceived(successCallback: (String data) {
          print("success data: $data");
          setState(() {
            contentJson = data;
            Map<String, dynamic> mapData = json.decode(data);
            if (mapData.containsKey('key')) {
              String key = mapData['key'];
              if (key == 'ble_scan_result') {
                Map<String, dynamic> peripheral = mapData['value'];
    
                String address = peripheral['address'];
                String name = peripheral['name'];
                int rssi = peripheral['rssi'];
        
    
        print(rssi);
            scanResult[address] = name;
          }
        }
      });
    },
    errorCallback: (String error) {
    
    });
    

    }

     Future<void> initPlatformState() async {
        String platformVersion;
       
        try {
          platformVersion = await BlufiPlugin.instance.platformVersion;
        } on PlatformException {
          platformVersion = 'Failed to get platform version.';
        }
        if (!mounted) return;
      }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              title: const Text('Plugin example app'),
            ),
            body: Column(
              children: [
                TextButton(onPressed: () async {
                  await BlufiPlugin.instance.scanDeviceInfo(filterString: 'VCONNEX');
                }, child: Text('Scan')),
                TextButton(onPressed: () async {
                 await BlufiPlugin.instance.stopScan();
                }, child: Text('Stop Scan')),
                TextButton(onPressed: () async {
                 await BlufiPlugin.instance.connectPeripheral(peripheralAddress: scanResult.keys.first);
                }, child: Text('Connect Peripheral')),
                TextButton(onPressed: () async {
                 await BlufiPlugin.instance.requestCloseConnection();
                }, child: Text('Close Connect')),
                TextButton(onPressed: () async {
                 await BlufiPlugin.instance.configProvision(username: 'ABCXYZ', password: '0913456789');
                }, child: Text('Config Provision')),
    
                TextButton(onPressed: () async {
                  String command =
                      '12345678';
                  await BlufiPlugin.instance.postCustomData(command);
                }, child: Text('Send Custom Data')),
    
                Text(contentJson ?? '')
    
              ],
            ),
          ),
        );
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search