Skip to content

Add typed data for old flutter #634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
3 changes: 3 additions & 0 deletions .fvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"flutter": "3.19.6"
}
49 changes: 44 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,20 +1,59 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.idea
.buildlog/
.history
.svn/
.gradle/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# Flutter/Dart/Pub related
**/doc/api/
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
/build/
*.g.dart

# Web related
lib/generated_plugin_registrant.dart

# Symbolication related
app.*.symbols

# Obfuscation related
app.*.map.json

# Exceptions to above rules.
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
/ios/Flutter/.last_build_id
/ios/build/


# FVM Version Cache
.fvm/

# Previously included from original FlutterBleLib
build/
ios/.generated/
packages
pubspec.lock
*.iml
example/android/res/values/strings_en.arb
res/values/strings_en.arb
lib/generated/
example/lib/generated/
example/.flutter-plugins-dependencies
.dart_tool/
example/ios/Podfile.lock
.vscode/settings.json
org.eclipse.buildship.core.prefs
.project
Expand Down
10 changes: 4 additions & 6 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
include: package:pedantic/analysis_options.yaml
include: package:flutter_lints/flutter.yaml

analyzer:
exclude: [test/**]

exclude:
- "**.mocks.dart"
linter:
rules:
prefer_single_quotes: false
omit_local_variable_types: false
unawaited_futures: false
constant_identifier_names: false
13 changes: 6 additions & 7 deletions android/.gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
*.iml
.gradle
gradle-wrapper.jar
/.gradle
/captures/
/gradlew
/gradlew.bat
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
GeneratedPluginRegistrant.java
4 changes: 2 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'com.android.tools.build:gradle:7.3.0'
}
}

Expand Down Expand Up @@ -36,5 +36,5 @@ android {

dependencies {
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'com.github.Polidea:MultiPlatformBleAdapter:0.1.8'
implementation 'com.github.dotintent:MultiPlatformBleAdapter:0.1.8'
}
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.6.4-bin.zip
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.polidea.flutter_ble_lib;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import com.polidea.flutter_ble_lib.constant.ArgumentKey;
import com.polidea.flutter_ble_lib.constant.ChannelName;
import com.polidea.flutter_ble_lib.constant.MethodName;
Expand All @@ -12,8 +15,8 @@
import com.polidea.flutter_ble_lib.delegate.DescriptorsDelegate;
import com.polidea.flutter_ble_lib.delegate.DeviceConnectionDelegate;
import com.polidea.flutter_ble_lib.delegate.DevicesDelegate;
import com.polidea.flutter_ble_lib.delegate.LogLevelDelegate;
import com.polidea.flutter_ble_lib.delegate.DiscoveryDelegate;
import com.polidea.flutter_ble_lib.delegate.LogLevelDelegate;
import com.polidea.flutter_ble_lib.delegate.MtuDelegate;
import com.polidea.flutter_ble_lib.delegate.RssiDelegate;
import com.polidea.flutter_ble_lib.event.AdapterStateStreamHandler;
Expand All @@ -31,15 +34,17 @@
import java.util.LinkedList;
import java.util.List;

import androidx.annotation.NonNull;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

public class FlutterBleLibPlugin implements MethodCallHandler {
public class FlutterBleLibPlugin implements MethodCallHandler, FlutterPlugin, ActivityAware {

static final String TAG = FlutterBleLibPlugin.class.getName();

Expand All @@ -53,16 +58,32 @@ public class FlutterBleLibPlugin implements MethodCallHandler {

private List<CallDelegate> delegates = new LinkedList<>();

public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), ChannelName.FLUTTER_BLE_LIB);
static MethodChannel channel;
private Activity activity;

private static FlutterBleLibPlugin factory(Context context, Activity activity) {
FlutterBleLibPlugin plugin = new FlutterBleLibPlugin();
plugin.context = context;
plugin.activity = activity;
return plugin;
}

/**
* Initializes the plugin.
*
* @param context registrar.context() or binding.getApplicationContext()
* @param messenger registrar.messenger() or binding.getBinaryMessenger()
*/
private static void init(Context context, BinaryMessenger messenger, Activity activity) {
channel = new MethodChannel(messenger, ChannelName.FLUTTER_BLE_LIB);

final EventChannel bluetoothStateChannel = new EventChannel(registrar.messenger(), ChannelName.ADAPTER_STATE_CHANGES);
final EventChannel restoreStateChannel = new EventChannel(registrar.messenger(), ChannelName.STATE_RESTORE_EVENTS);
final EventChannel scanningChannel = new EventChannel(registrar.messenger(), ChannelName.SCANNING_EVENTS);
final EventChannel connectionStateChannel = new EventChannel(registrar.messenger(), ChannelName.CONNECTION_STATE_CHANGE_EVENTS);
final EventChannel characteristicMonitorChannel = new EventChannel(registrar.messenger(), ChannelName.MONITOR_CHARACTERISTIC);
final EventChannel bluetoothStateChannel = new EventChannel(messenger, ChannelName.ADAPTER_STATE_CHANGES);
final EventChannel restoreStateChannel = new EventChannel(messenger, ChannelName.STATE_RESTORE_EVENTS);
final EventChannel scanningChannel = new EventChannel(messenger, ChannelName.SCANNING_EVENTS);
final EventChannel connectionStateChannel = new EventChannel(messenger, ChannelName.CONNECTION_STATE_CHANGE_EVENTS);
final EventChannel characteristicMonitorChannel = new EventChannel(messenger, ChannelName.MONITOR_CHARACTERISTIC);

final FlutterBleLibPlugin plugin = new FlutterBleLibPlugin(registrar.context());
final FlutterBleLibPlugin plugin = factory(context, activity);

channel.setMethodCallHandler(plugin);

Expand All @@ -73,10 +94,6 @@ public static void registerWith(Registrar registrar) {
characteristicMonitorChannel.setStreamHandler(plugin.characteristicsMonitorStreamHandler);
}

private FlutterBleLibPlugin(Context context) {
this.context = context;
}

private void setupAdapter(Context context) {
bleAdapter = BleAdapterFactory.getNewAdapter(context);
delegates.add(new DeviceConnectionDelegate(bleAdapter, connectionStateStreamHandler));
Expand Down Expand Up @@ -192,4 +209,38 @@ private void cancelTransaction(MethodCall call, Result result) {
}
result.success(null);
}

// FlutterPlugin interface:

@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
init(binding.getApplicationContext(), binding.getBinaryMessenger(), null);
}

@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setMethodCallHandler(null);
}

// ActivityAware interface:

@Override
public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
activity = binding.getActivity();
}

@Override
public void onDetachedFromActivityForConfigChanges() {
activity = null;
}

@Override
public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
activity = binding.getActivity();
}

@Override
public void onDetachedFromActivity() {
activity = null;
}
}
4 changes: 3 additions & 1 deletion example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<application
android:name="io.flutter.app.FlutterApplication"
android:label="flutter_ble_lib_example"
android:icon="@mipmap/ic_launcher">
<activity
Expand All @@ -31,4 +30,7 @@
</intent-filter>
</activity>
</application>
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</manifest>
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package com.polidea.flutter_ble_lib_example;

import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.embedding.android.FlutterActivity;

public class MainActivity extends FlutterActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
}
}
public class MainActivity extends FlutterActivity {}
2 changes: 1 addition & 1 deletion example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,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.6.4-bin.zip
12 changes: 5 additions & 7 deletions example/lib/device_details/device_detail_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,12 @@ class DeviceDetailsViewState extends State<DeviceDetailsView> {
@override
Widget build(BuildContext context) {
final deviceDetailsBloc = _deviceDetailsBloc;
return WillPopScope(
onWillPop: () {
if (deviceDetailsBloc == null) {
return Future<bool>.value(true);
return PopScope(
canPop: deviceDetailsBloc == null,
onPopInvoked: (didPop) {
if (deviceDetailsBloc != null) {
deviceDetailsBloc.disconnect();
}
return deviceDetailsBloc.disconnect().then((_) {
return false;
});
},
child: DefaultTabController(
length: 2,
Expand Down
5 changes: 1 addition & 4 deletions example/lib/device_details/device_details_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import 'package:flutter_ble_lib_example/repository/device_repository.dart';
import 'package:flutter_ble_lib_example/test_scenarios/test_scenarios.dart';
import 'package:rxdart/rxdart.dart';

import '../model/ble_device.dart';
import '../repository/device_repository.dart';

class DeviceDetailsBloc {
final BleManager _bleManager;
final DeviceRepository _deviceRepository;
Expand All @@ -25,7 +22,7 @@ class DeviceDetailsBloc {

Stream<List<DebugLog>> get logs => _logsController.stream;

Stream<Null?> get disconnectedDevice => _deviceRepository.pickedDevice
Stream<Null> get disconnectedDevice => _deviceRepository.pickedDevice
.skipWhile((bleDevice) => bleDevice != null).cast<Null>();

List<DebugLog> _logs = [];
Expand Down
8 changes: 5 additions & 3 deletions example/lib/device_details/view/button_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ class ButtonView extends StatelessWidget {
return Expanded(
child: Padding(
padding: const EdgeInsets.all(2.0),
child: RaisedButton(
color: Colors.blue,
textColor: Colors.white,
child: ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.blue),
foregroundColor: MaterialStateProperty.all(Colors.white),
),
child: Text(_text),
onPressed: action,
),
Expand Down
2 changes: 1 addition & 1 deletion example/lib/devices_list/devices_list_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class DevicesList extends ListView {
padding: const EdgeInsets.all(8.0),
child: Image.asset('assets/ti_logo.png'),
),
backgroundColor: Theme.of(context).accentColor);
backgroundColor: Theme.of(context).colorScheme.secondary);
case DeviceCategory.hex:
return CircleAvatar(
child: CustomPaint(painter: HexPainter(), size: Size(20, 24)),
Expand Down
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MyApp extends StatelessWidget {
title: 'FlutterBleLib example',
theme: new ThemeData(
primaryColor: new Color(0xFF0A3D91),
accentColor: new Color(0xFFCC0000),
colorScheme: ColorScheme.fromSwatch().copyWith(secondary: new Color(0xFFCC0000)),
),
initialRoute: "/",
routes: <String, WidgetBuilder>{
Expand Down
Loading
Loading