Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ group 'com.tugberka.mdsflutter'
version '1.0-SNAPSHOT'

buildscript {
ext.kotlin_version = '1.3.50'
ext.kotlin_version = '1.5.31'
repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
classpath 'com.android.tools.build:gradle:7.0.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.10'
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.15'
}
}

Expand Down Expand Up @@ -49,26 +49,37 @@ protobuf {
// Configure the protoc executable
protoc {
// Download from repositories
artifact = 'com.google.protobuf:protoc:3.9.1'
// artifact = 'com.google.protobuf:protoc:3.9.1'
artifact = 'com.google.protobuf:protoc:3.17.3'
}
plugins {
/* plugins {
javalite {
// The codegen for lite comes as a separate artifact
artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
}
}
generateProtoTasks {
} */
/* generateProtoTasks {
all().each { task ->
task.plugins {
javalite { }
}
}
} */
generateProtoTasks {
all().each { task ->
task.builtins {
java {
option "lite"
}
}
}
}
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.google.protobuf:protobuf-lite:3.0.1'
//implementation 'com.google.protobuf:protobuf-lite:3.0.1'
implementation 'com.google.protobuf:protobuf-javalite:3.17.3'
// RxAndroidBle helper library
implementation "com.polidea.rxandroidble2:rxandroidble:1.11.0"
implementation(name: 'mdslib', version: '+', ext: 'aar')
Expand Down
2 changes: 1 addition & 1 deletion android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip
46 changes: 26 additions & 20 deletions example/lib/AppModel.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import 'dart:collection';
import 'dart:convert';

Expand All @@ -11,13 +10,13 @@ import 'package:mdsflutter_example/DeviceConnectionStatus.dart';
import 'dart:developer' as developer;

class AppModel extends ChangeNotifier {

final Set<Device> _deviceList = Set();
final Set<Device?> _deviceList = Set();
bool _isScanning = false;
void Function(Device) _onDeviceMdsConnectedCb;
void Function(Device) _onDeviceDisonnectedCb;
void Function(Device)? _onDeviceMdsConnectedCb;
void Function(Device)? _onDeviceDisonnectedCb;

UnmodifiableListView<Device> get deviceList => UnmodifiableListView(_deviceList);
UnmodifiableListView<Device?> get deviceList =>
UnmodifiableListView(_deviceList);

bool get isScanning => _isScanning;

Expand All @@ -33,7 +32,8 @@ class AppModel extends ChangeNotifier {

void startScan() {
_deviceList.forEach((device) {
if (device.connectionStatus == DeviceConnectionStatus.CONNECTED) {
if (device != null &&
device.connectionStatus == DeviceConnectionStatus.CONNECTED) {
disconnectFromDevice(device);
}
});
Expand All @@ -43,7 +43,7 @@ class AppModel extends ChangeNotifier {

try {
Mds.startScan((name, address) {
Device device = Device(name, address);
Device device = Device(name ?? "", address ?? "");
if (!_deviceList.contains(device)) {
_deviceList.add(device);
notifyListeners();
Expand All @@ -65,41 +65,47 @@ class AppModel extends ChangeNotifier {

void connectToDevice(Device device) {
device.onConnecting();
Mds.connect(device.address,
(serial) => _onDeviceMdsConnected(device.address, serial),
() => _onDeviceDisconnected(device.address),
() => _onDeviceConnectError(device.address)
);
final address = device.address;
if (address == null) return;
Mds.connect(
address,
(serial) => _onDeviceMdsConnected(address, serial),
() => _onDeviceDisconnected(address),
() => _onDeviceConnectError(address));
}

void disconnectFromDevice(Device device) {
Mds.disconnect(device.address);
_onDeviceDisconnected(device.address);
final address = device.address;
if (address == null) return;
Mds.disconnect(address);
_onDeviceDisconnected(address);
}

void _onDeviceMdsConnected(String address, String serial) {
Device foundDevice = _deviceList.firstWhere((element) => element.address == address);
Device? foundDevice =
_deviceList.firstWhere((element) => element?.address == address);
if (foundDevice != null) {
foundDevice.onMdsConnected(serial);
notifyListeners();
if (_onDeviceMdsConnectedCb != null) {
_onDeviceMdsConnectedCb.call(foundDevice);
_onDeviceMdsConnectedCb?.call(foundDevice);
}
}
}

void _onDeviceDisconnected(String address) {
Device foundDevice = _deviceList.firstWhere((element) => element.address == address);
Device? foundDevice =
_deviceList.firstWhere((element) => element?.address == address);
if (foundDevice != null) {
foundDevice.onDisconnected();
notifyListeners();
if (_onDeviceDisonnectedCb != null) {
_onDeviceDisonnectedCb.call(foundDevice);
_onDeviceDisonnectedCb?.call(foundDevice);
}
}
}

void _onDeviceConnectError(String address) {
_onDeviceDisconnected(address);
}
}
}
25 changes: 14 additions & 11 deletions example/lib/Device.dart
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@

import 'package:mdsflutter_example/DeviceConnectionStatus.dart';
import 'package:mdsflutter_example/DeviceModel.dart';

class Device {
String _address;
String _name;
String _serial;
DeviceConnectionStatus _connectionStatus = DeviceConnectionStatus.NOT_CONNECTED;
String? _address;
String? _name;
String? _serial;
DeviceConnectionStatus _connectionStatus =
DeviceConnectionStatus.NOT_CONNECTED;

Device(String name, String address) {
_name = name;
_address = address;
}

String get name => _name != null ? _name : "";
String get address => _address != null ? _address : "";
String get serial => _serial != null ? _serial : "";
String? get name => _name != null ? _name : "";
String? get address => _address != null ? _address : "";
String? get serial => _serial != null ? _serial : "";
DeviceConnectionStatus get connectionStatus => _connectionStatus;

void onConnecting() => _connectionStatus = DeviceConnectionStatus.CONNECTING;
void onMdsConnected(String serial) {
_serial = serial;
_connectionStatus = DeviceConnectionStatus.CONNECTED;
}
void onDisconnected() => _connectionStatus = DeviceConnectionStatus.NOT_CONNECTED;

bool operator ==(o) => o is Device && o._address == _address && o._name == _name;
void onDisconnected() =>
_connectionStatus = DeviceConnectionStatus.NOT_CONNECTED;

bool operator ==(o) =>
o is Device && o._address == _address && o._name == _name;
int get hashCode => _address.hashCode * _name.hashCode;
}
}
27 changes: 14 additions & 13 deletions example/lib/DeviceInteractionWidget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ class DeviceInteractionWidget extends StatefulWidget {
}

class _DeviceInteractionWidgetState extends State<DeviceInteractionWidget> {
AppModel _appModel;
AppModel? _appModel;

@override
void initState() {
super.initState();
_appModel = Provider.of<AppModel>(context, listen: false);
_appModel.onDeviceMdsDisconnected((device) => { Navigator.pop(context) });
_appModel?.onDeviceMdsDisconnected((device) => {Navigator.pop(context)});
}

void _onAccelerometerButtonPressed(DeviceModel deviceModel) {
Expand All @@ -45,20 +45,20 @@ class _DeviceInteractionWidgetState extends State<DeviceInteractionWidget> {

@override
void dispose() {
_appModel.disconnectFromDevice(widget.device);
_appModel?.disconnectFromDevice(widget.device);
super.dispose();
}

@override
Widget build(BuildContext context) {
Device device = widget.device;
return ChangeNotifierProvider(
create: (context) => DeviceModel(device.name, device.serial),
create: (context) => DeviceModel(device.name ?? "", device.serial ?? ""),
child: Consumer<DeviceModel>(
builder: (context, model, child) {
return Scaffold(
appBar: AppBar(
title: Text(device.name),
title: Text(device.name ?? ""),
),
body: Column(
mainAxisSize: MainAxisSize.min,
Expand All @@ -68,8 +68,7 @@ class _DeviceInteractionWidgetState extends State<DeviceInteractionWidget> {
_ledItem(model),
_temperatureItem(model)
],
)
);
));
},
),
);
Expand All @@ -80,8 +79,10 @@ class _DeviceInteractionWidgetState extends State<DeviceInteractionWidget> {
child: ListTile(
title: Text("Accelerometer"),
subtitle: Text(deviceModel.accelerometerData),
trailing: RaisedButton(
child: Text(deviceModel.accelerometerSubscribed ? "Unsubscribe" : "Subscribe"),
trailing: ElevatedButton(
child: Text(deviceModel.accelerometerSubscribed
? "Unsubscribe"
: "Subscribe"),
onPressed: () => _onAccelerometerButtonPressed(deviceModel),
),
),
Expand All @@ -93,7 +94,7 @@ class _DeviceInteractionWidgetState extends State<DeviceInteractionWidget> {
child: ListTile(
title: Text("Heart rate"),
subtitle: Text(deviceModel.hrData),
trailing: RaisedButton(
trailing: ElevatedButton(
child: Text(deviceModel.hrSubscribed ? "Unsubscribe" : "Subscribe"),
onPressed: () => _onHrButtonPressed(deviceModel),
),
Expand All @@ -107,7 +108,7 @@ class _DeviceInteractionWidgetState extends State<DeviceInteractionWidget> {
title: Text("Led"),
trailing: Switch(
value: deviceModel.ledStatus,
onChanged: (b) => { deviceModel.switchLed() },
onChanged: (b) => {deviceModel.switchLed()},
),
),
);
Expand All @@ -118,11 +119,11 @@ class _DeviceInteractionWidgetState extends State<DeviceInteractionWidget> {
child: ListTile(
title: Text("Temperature"),
subtitle: Text(deviceModel.temperature),
trailing: RaisedButton(
trailing: ElevatedButton(
child: Text("Get"),
onPressed: () => deviceModel.getTemperature(),
),
),
);
}
}
}
Loading