ble_controller 0.0.5
ble_controller: ^0.0.5 copied to clipboard
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';
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 = "00000a60-0000-1000-8000-00805f9b34fb";
final charWriteUUID = "00000a66-0000-1000-8000-00805f9b34fb";
final charNotifyUUID = "00000a67-0000-1000-8000-00805f9b34fb";
final desUUID = "00002902-0000-1000-8000-00805f9b34fb";
var connectReturnVal = false;
var sendDataValue = "";
@override
void initState() {
super.initState();
_bleControllerPlugin.setServiceUUID(serviceUUID);
_bleControllerPlugin.setCharacteristicUUID(charNotifyUUID);
_bleControllerPlugin.setDescriptionUUID(desUUID);
}
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: () {
_bleControllerPlugin.connect(scannedDeviceInfo[index][1]).then((value) {
this.setState(() {
connectReturnVal = value!;
});
});
},
);
},
itemCount: scannedDeviceInfo.length,
),
),
OutlinedButton(onPressed: () {
startScan();
}, child: Text('Scan 테스트')),
Text(' connect 리턴값: $connectReturnVal'),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
OutlinedButton(onPressed: () {
_bleControllerPlugin.sendData('Test').then((value) {
this.setState(() {
sendDataValue = value.toString();
});
});
}, child: Text('Send Data 테스트')),
Text(' 리턴값: $sendDataValue')
],
),
OutlinedButton(onPressed: () {
_bleControllerPlugin.getNotifyStream().asBroadcastStream().listen((event) {
print(event.toString());
});
}, child: Text('stream notify 테스트')),
OutlinedButton(onPressed: () {
_bleControllerPlugin.notify(true).then((value) => print(value));
}, child: Text('notify 테스트')),
],
),
),
),
);
}
}