vaster_blue 0.0.2 copy "vaster_blue: ^0.0.2" to clipboard
vaster_blue: ^0.0.2 copied to clipboard

PlatformAndroid

Bluetooth plugin from vaster.

example/lib/main.dart

import 'dart:ui';

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

void main() {
  DartPluginRegistrant.ensureInitialized();
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

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

class _MyAppState extends State<MyApp> {
  var input = TextEditingController();
  List<Device> devices = [];

  @override
  void initState() {
    super.initState();
    disconnect();
    input.text = 'C,O,M,R,F,1,0,0,C,10,0,0,0,0,0,78,F';
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: [
              Wrap(
                children: [
                  ElevatedButton(
                    onPressed: startSearch,
                    child: const Text("start search"),
                  ),
                  const SizedBox(width: 20),
                  ElevatedButton(
                    onPressed: stopSearch,
                    child: const Text("stop search"),
                  ),
                  const SizedBox(width: 20),
                  ElevatedButton(
                    onPressed: disconnect,
                    child: const Text("disconect"),
                  ),
                  const SizedBox(width: 20),
                ],
              ),
              TextField(
                controller: input,
              ),
              ElevatedButton(
                onPressed: sendTrama,
                child: const Text("send trama"),
              ),
              StreamBuilder<Device>(
                stream: VasterBluePlugin.registerDevicesListener(),
                builder: (c, s) {
                  if (s.hasData && s.data != null) {
                    if (devices
                        .where((e) => e.address == s.data!.address)
                        .isEmpty) {
                      devices.add(s.data!);
                    }
                  }
                  return Column(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: [
                      ...devices.map(
                            (e) => InkWell(
                          onTap: () => connectDevice(s.data!),
                          child: Card(
                            child: Padding(
                              padding: const EdgeInsets.all(20),
                              child: Row(
                                children: [
                                  Text(e.name),
                                  const Spacer(),
                                  Icon(
                                    e.bondState == BondState.BOND_BONDED
                                        ? Icons.link
                                        : null,
                                  )
                                ],
                              ),
                            ),
                          ),
                        ),
                      ),
                    ],
                  );
                },
              ),
              StreamBuilder(
                stream: VasterBluePlugin.registerDeviceConnectionListener(),
                initialData: 0,
                builder: (c, s) {
                  return Text(s.data == 0 ? 'Disconnected' : 'Connected');
                },
              )
            ],
          ),
        ),
      ),
    );
  }

  startSearch() async {
    devices.clear();
    setState(() => {});
    VasterBluePlugin.startSearch();
  }

  stopSearch() async {
    setState(() => {});
    VasterBluePlugin.stopSearch();
  }

  sendTrama() async {
    VasterBluePlugin.sendTrama(input.text.trim());
  }

  disconnect() async {
    VasterBluePlugin.disconnectDevice();
  }

  connectDevice(Device device) async {
    VasterBluePlugin.connect(device.address);
  }
}