ble_controller 0.0.13 copy "ble_controller: ^0.0.13" to clipboard
ble_controller: ^0.0.13 copied to clipboard

outdated

This is a Bluetooth Plugin.

example/lib/main.dart

import 'dart:ffi';

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

import 'package:flutter/services.dart';
import 'package:ble_controller/ble_controller.dart';
import 'package:shared_preferences/shared_preferences.dart';

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

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  final _bleControllerPlugin = BleController();
  List<List> scannedDeviceInfo = List<List>.empty(growable: true);
  final serviceUUID = "6E400001-B5A3-F393-E0A9-E50E24DCCA9E";
  final charWriteUUID = "6E400002-B5A3-F393-E0A9-E50E24DCCA9E";
  final charNotifyUUID = "6E400003-B5A3-F393-E0A9-E50E24DCCA9E";
  final desUUID = "00002902-0000-1000-8000-00805f9b34fb";
  var connectReturnVal = false;
  var sendDataValue = "";
  var receiveData = "";


  @override
  void initState() {
    super.initState();
    _bleControllerPlugin.setServiceUUID(serviceUUID);
    _bleControllerPlugin.setCharacteristicUUID(charWriteUUID);
    _bleControllerPlugin.setCharacteristicNotifyUUID(charNotifyUUID);
    _bleControllerPlugin.setDescriptionUUID(desUUID);
    autoConnect();

  }

  autoConnect() async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    final String? address = await prefs.getString('bondedAddress');

    if (address != null) {
      print ("auto 시도");
      _bleControllerPlugin.connect(address).then((value) {
        setState(() {
          connectReturnVal = value!;
        });
      });

    } else {
      print ("auto 실패");
    }
  }

  Future<void> initPlatformState() async {
    var platformVersion;
    try {
      platformVersion = await _bleControllerPlugin.getPlatformVersion() ??
          'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    if (!mounted) return;
  }

  void startScan() {
    _bleControllerPlugin.getScanStream().asBroadcastStream().listen((deviceInfo) {
      setState(() {

        scannedDeviceInfo.add(deviceInfo);
        print("${deviceInfo[0]} 이름");
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: [
              Expanded(
                child: ListView.separated(
                  separatorBuilder: (context, index) {
                    return Divider(color: Colors.black);
                  },
                  itemBuilder: (BuildContext ctx, int index) {
                    return ListTile(
                      title: Text(
                        scannedDeviceInfo[index][0],
                      ),
                      onTap: () async {

                        _bleControllerPlugin.connect(scannedDeviceInfo[index][1]).then((value) {

                          setState(() {
                            connectReturnVal = value!;
                          });
                        });

                        final SharedPreferences prefs = await SharedPreferences.getInstance();
                        await prefs.setString('bondedAddress', scannedDeviceInfo[index][1]);


                      },
                    );
                  },
                  itemCount: scannedDeviceInfo.length,
                ),
              ),
              OutlinedButton(onPressed: () {
                startScan();
              }, child: Text('Scan 테스트')),
              StreamBuilder(
                initialData: false,
                stream: _bleControllerPlugin.getConnectionStateStream().asBroadcastStream(),
                builder: (context, snapshot) {
                  return Text(' connect 리턴값: ${snapshot.data}');
                }
              ),

              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  OutlinedButton(onPressed: () {
                    _bleControllerPlugin.sendData('TEMP_REQUEST:SINGLE').then((value) {
                      this.setState(() {
                        sendDataValue = value.toString();
                      });
                    });
                  }, child: Text('Send Data 테스트')),
                  Text('  리턴값: $sendDataValue')
                ],
              ),

              OutlinedButton(onPressed: () {

                _bleControllerPlugin.getNotifyStream().asBroadcastStream().listen((event) {

                  print(event.toString());

                  setState(() {
                    receiveData = event.toString();
                  });
                });
              }, child: Text('stream notify 테스트')),

              Text("리시비데이터 $receiveData"),

              OutlinedButton(onPressed: () {
                _bleControllerPlugin.notify(true).then((value) => print(value));
              }, child: Text('notify 테스트')),
            ],
          ),
        ),
      ),
    );
  }
}
1
likes
0
points
73
downloads

Publisher

unverified uploader

Weekly Downloads

This is a Bluetooth Plugin.

License

unknown (license)

Dependencies

flutter, plugin_platform_interface, text_write_read_controller

More

Packages that depend on ble_controller

Packages that implement ble_controller