Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'dart:async';
- import 'dart:io' show Platform;
- import 'package:flutter/foundation.dart';
- import 'package:flutter/services.dart';
- import 'package:location_permissions/location_permissions.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
- void main() {
- return runApp(
- const MaterialApp(home: HomePage()),
- );
- }
- class HomePage extends StatefulWidget {
- const HomePage({Key? key}) : super(key: key);
- @override
- HomePageState createState() => HomePageState();
- }
- class HomePageState extends State<HomePage> {
- bool foundDeviceWaitingToConnect = false;
- bool scanStarted = false;
- bool connected = false;
- // Bluetooth related variables
- late DiscoveredDevice ubiqueDevice;
- final flutterReactiveBle = FlutterReactiveBle();
- late StreamSubscription<DiscoveredDevice> scanStream;
- late QualifiedCharacteristic rxCharacteristic;
- // These are the UUIDs of your device
- final Uuid serviceUuid = Uuid.parse("75C276C3-8F97-20BC-A143-B354244886D4");
- final Uuid characteristicUuid =
- Uuid.parse("6ACF4F08-CC9D-D495-6B41-AA7E60C4E8A6");
- void startScan() async {
- // Platform permissions handling stuff
- if (kDebugMode) {
- print("Start scanning...");
- }
- bool permGranted = false;
- setState(() {
- scanStarted = true;
- });
- PermissionStatus permission;
- if (kDebugMode) {
- print(Platform.operatingSystem);
- print(Platform.localeName);
- }
- if (Platform.isAndroid) {
- permission = await LocationPermissions().requestPermissions();
- if (permission == PermissionStatus.granted) permGranted = true;
- } else if (Platform.isIOS) {
- permGranted = true;
- }
- // Main scanning logic happens here ⤵️
- if (permGranted) {
- if (kDebugMode) {
- print("Permission granted, waiting BLE devices...");
- }
- scanStream = flutterReactiveBle
- .scanForDevices(withServices: [serviceUuid]).listen((device) {
- // Change this string to what you defined in Zephyr
- if (device.name == 'HyperTorcia') {
- setState(() {
- ubiqueDevice = device;
- foundDeviceWaitingToConnect = true;
- });
- connectToDevice();
- }
- });
- }
- }
- void connectToDevice() {
- // We're done scanning, we can cancel it
- scanStream.cancel();
- // Let's listen to our connection so we can make updates on a state change
- Stream<ConnectionStateUpdate> currentConnectionStream = flutterReactiveBle
- .connectToAdvertisingDevice(
- id: ubiqueDevice.id,
- prescanDuration: const Duration(seconds: 1),
- withServices: [serviceUuid, characteristicUuid]);
- currentConnectionStream.listen((event) {
- switch (event.connectionState) {
- // We're connected and good to go!
- case DeviceConnectionState.connected:
- {
- rxCharacteristic = QualifiedCharacteristic(
- serviceId: serviceUuid,
- characteristicId: characteristicUuid,
- deviceId: event.deviceId);
- setState(() {
- foundDeviceWaitingToConnect = false;
- connected = true;
- });
- break;
- }
- // Can add various state state updates on disconnect
- case DeviceConnectionState.disconnected:
- {
- break;
- }
- default:
- }
- });
- }
- void partyTime() {
- if (connected) {
- flutterReactiveBle
- .writeCharacteristicWithResponse(rxCharacteristic, value: [
- 0x02,
- ]);
- }
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- backgroundColor: Colors.white,
- body: Container(),
- persistentFooterButtons: [
- // We want to enable this button if the scan has NOT started
- // If the scan HAS started, it should be disabled.
- scanStarted
- // True condition
- ? ElevatedButton(
- style: ElevatedButton.styleFrom(
- backgroundColor: Colors.grey, // background
- foregroundColor: Colors.white, // foreground
- ),
- onPressed: () {},
- child: const Icon(Icons.search),
- )
- // False condition
- : ElevatedButton(
- style: ElevatedButton.styleFrom(
- backgroundColor: Colors.blue, // background
- foregroundColor: Colors.white, // foreground
- ),
- onPressed: startScan,
- child: const Icon(Icons.search),
- ),
- foundDeviceWaitingToConnect
- // True condition
- ? ElevatedButton(
- style: ElevatedButton.styleFrom(
- backgroundColor: Colors.blue, // background
- foregroundColor: Colors.white, // foreground
- ),
- onPressed: connectToDevice,
- child: const Icon(Icons.bluetooth),
- )
- // False condition
- : ElevatedButton(
- style: ElevatedButton.styleFrom(
- backgroundColor: Colors.grey, // background
- foregroundColor: Colors.white, // foreground
- ),
- onPressed: () {},
- child: const Icon(Icons.bluetooth),
- ),
- connected
- // True condition
- ? ElevatedButton(
- style: ElevatedButton.styleFrom(
- backgroundColor: Colors.blue, // background
- foregroundColor: Colors.white, // foreground
- ),
- onPressed: partyTime,
- child: const Icon(Icons.celebration_rounded),
- )
- // False condition
- : ElevatedButton(
- style: ElevatedButton.styleFrom(
- backgroundColor: Colors.grey, // background
- foregroundColor: Colors.white, // foreground
- ),
- onPressed: () {},
- child: const Icon(Icons.celebration_rounded),
- ),
- ],
- );
- }
- }
- Errore generato nell'iPhone con OSX 16.3:
- Launching lib/main.dart on iPhone di Keres in debug mode...
- Automatically signing iOS for device deployment using specified development team in Xcode project: RCT45BYNT4
- Xcode build done. 8,7s
- (lldb) 2023-02-10 15:04:58.521031+0100 Runner[27335:9898606] [SceneConfiguration] Info.plist contained no UIScene configuration dictionary (looking for configuration named "(no name)")
- Warning: Unable to create restoration in progress marker file
- Connecting to VM Service at ws://127.0.0.1:50826/HAdtjnhysow=/ws
- fopen failed for data file: errno = 2 (No such file or directory)
- Errors found! Invalidating cache...
- fopen failed for data file: errno = 2 (No such file or directory)
- Errors found! Invalidating cache...
- flutter: Start scanning...
- flutter: ios
- flutter: it_IT
- flutter: Permission granted, waiting BLE devices...
- Warning! No event channel set up to report a connection update
- reactive_ble_mobile/ConnectTaskController.swift:16: Assertion failed
- reactive_ble_mobile/ConnectTaskController.swift:16: Assertion failed
- * thread #1, queue = 'com.apple.main-thread', stop reason = Assertion failed
- frame #0: 0x00000001cc9b10f8 libswiftCore.dylib`_swift_runtime_on_report
- libswiftCore.dylib`:
- -> 0x1cc9b10f8 <+0>: ret
- libswiftCore.dylib`:
- 0x1cc9b10fc <+0>: b 0x1cc9b10f8 ; _swift_runtime_on_report
- libswiftCore.dylib`:
- 0x1cc9b1100 <+0>: adrp x8, 365868
- 0x1cc9b1104 <+4>: ldrb w0, [x8, #0xabc]
- Target 0: (Runner) stopped.
- Lost connection to device.
- Exited
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement