Created
June 1, 2019 07:50
-
-
Save kenreilly/d8c8fd5f234c57549fa5100e02d85599 to your computer and use it in GitHub Desktop.
Arduino bluetooth demo - Flutter client-side bluetooth bridge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/services.dart'; | |
import 'dart:async'; | |
class BTController { | |
static const platform = const MethodChannel('flutter.native/helper'); | |
static Function _onData = (String string) => { }; | |
static init(Function onData) { | |
_onData = onData; | |
platform.setMethodCallHandler(_handler); | |
} | |
static Future<dynamic> _handler(MethodCall call) { | |
return Future.value(_onData(call.arguments)); | |
} | |
static Future<List<dynamic>> enumerateDevices() async { | |
print('enumerating devices'); | |
try { return await platform.invokeMethod('enumerate-devices'); } | |
on PlatformException catch (e) { return []; } | |
} | |
static Future<void> connect(String address) async { | |
print('connecting ' + address); | |
try { return await platform.invokeMethod('connect', address); } | |
on PlatformException catch (e) { } | |
} | |
static Future<void> transmit(String data) async { | |
try { return await platform.invokeMethod('transmit', data); } | |
on PlatformException catch (e) { } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment