Skip to content
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

Upgrade to flutter_blue_plus 1.29 API & published as 3.1.0 #839

Merged
merged 1 commit into from
Nov 26, 2023
Merged
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
7 changes: 6 additions & 1 deletion packages/movisens_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 3.1.0

- Upgrade to flutter version 3.2.0
- Upgrade to `flutter_blue_plus: ^1.29.0`

## 3.0.0

- Upgrade to flutter version 3.0.0
Expand All @@ -20,7 +25,7 @@

## 1.0.0

- Reimplentation of package to use direct bluetooth communication using `flutter_blue_plus` instead of Movisens libraries
- Re-implementation of package to use direct Bluetooth communication using `flutter_blue_plus` instead of Movisens libraries

## 0.2.0

Expand Down
32 changes: 23 additions & 9 deletions packages/movisens_flutter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,40 @@ A plugin for connecting and collecting data from a Movisens sensor. Works for bo

## Install

Add `movisens_flutter` as a dependency in `pubspec.yaml`.
For help on adding as a dependency, view the [documentation](https://flutter.io/using-packages/).
Add `movisens_flutter` as a dependency in `pubspec.yaml`. For help on adding as a dependency, view the [documentation](https://flutter.io/using-packages/).

This plugin uses the [flutter_blue_plus](https://pub.dev/packages/flutter_blue_plus) plugin and apps using this plugin should follow the [setup guide for flutter_blue](https://pub.dev/packages/flutter_blue_plus#getting-started).

### Android

Add the following to your `android/app/src/main/AndroidManifest.xml` :

```dart
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
```xml
<!-- Tell Google Play Store that your app uses Bluetooth LE
Set android:required="true" if bluetooth is necessary -->
<uses-feature android:name="android.hardware.bluetooth_le" android:required="false" />

<!-- New Bluetooth permissions in Android 12
https://developer.android.com/about/versions/12/features/bluetooth-permissions -->
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

<!-- legacy for Android 11 or lower -->
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30"/>

<!-- legacy for Android 9 or lower -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="28" />
```

Update the `android/app/build.gradle` to `minSdkVersion` at least 19
Update the `android/app/build.gradle` to `minSdkVersion` at least 21.

```gradle
android {
defaultConfig {
...
minSdkVersion 19
minSdkVersion 21
...
}
}
Expand Down Expand Up @@ -61,7 +75,7 @@ As illustrated below, each service has a 1 or more `MovisensBluetoothCharacteris

### Undocumented API

4 services and 9 characterisics are not documented in Movisens' documentation as they are part of the general Bluetooth GATT specifications but have been added to this plugin.
4 services and 9 characteristics are not documented in Movisens' documentation as they are part of the general Bluetooth GATT specifications but have been added to this plugin.
Below is a list of their names and UUIDs. The battery service, heart rate service and user data service have been added to the movisens specific implementations for simplicity of the plugin API.

- SERVICE: 0000180f-0000-1000-8000-00805f9b34fb // **_BATTERY SERVICE_** (added to the Battery Service)
Expand Down
2 changes: 1 addition & 1 deletion packages/movisens_flutter/example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ android {
applicationId "com.example.example"
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration.
minSdkVersion 19
minSdkVersion 21
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.example">
<uses-permission android:name="android.permission.BLUETOOTH" />

<!-- Tell Google Play Store that your app uses Bluetooth LE
Set android:required="true" if bluetooth is necessary -->
<uses-feature android:name="android.hardware.bluetooth_le" android:required="false" />

<!-- New Bluetooth permissions in Android 12
https://developer.android.com/about/versions/12/features/bluetooth-permissions -->
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

<!-- legacy for Android 11 or lower -->
<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" android:maxSdkVersion="30"/>

<!-- legacy for Android 9 or lower -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" android:maxSdkVersion="28" />


<!-- <uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> -->

<application android:label="Movisens Example App"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher">
<activity android:name=".MainActivity"
<activity android:name="io.flutter.embedding.android.FlutterActivity"
android:exported="true"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
Expand Down

This file was deleted.

26 changes: 13 additions & 13 deletions packages/movisens_flutter/lib/movisens_device.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class MovisensDevice {
///
/// Returns null if the device is not connected or being connected
/// using the [connect] method.
Stream<BluetoothDeviceState>? get state => _bluetoothDevice?.state;
Stream<BluetoothConnectionState>? get state =>
_bluetoothDevice?.connectionState;

/// Get the [AmbientService] if the device supports it.
/// Is null if not supported / discovered on device.
Expand Down Expand Up @@ -99,13 +100,11 @@ class MovisensDevice {
/// Automatically discovers services on device and stores them.
Future<void> connect() async {
_log.info("Connecting to movisens device using name: [$name]");
FlutterBluePlus flutterBluePlus = FlutterBluePlus.instance;
// Checking if already connected - skips the rest of connect if true
// TODO: If already connected - can `device.connect()` be avoided?
List<BluetoothDevice> connectedDevices =
await flutterBluePlus.connectedDevices;
List<BluetoothDevice> connectedDevices = FlutterBluePlus.connectedDevices;
for (BluetoothDevice device in connectedDevices) {
if (device.name == name) {
if (device.platformName == name) {
await device.connect();
_bluetoothDevice = device;
// Discover services
Expand All @@ -116,9 +115,9 @@ class MovisensDevice {

// (For android) Check if the device is bonded
if (Platform.isAndroid) {
List<BluetoothDevice> bondedDevices = await flutterBluePlus.bondedDevices;
List<BluetoothDevice> bondedDevices = await FlutterBluePlus.bondedDevices;
for (BluetoothDevice device in bondedDevices) {
if (device.name == name) {
if (device.platformName == name) {
await device.connect();
_bluetoothDevice = device;
// Discover services
Expand All @@ -129,17 +128,18 @@ class MovisensDevice {
}

// Scan for devices
flutterBluePlus.startScan(timeout: const Duration(seconds: 10));
FlutterBluePlus.startScan(timeout: const Duration(seconds: 10));
late StreamSubscription subscription;
subscription = flutterBluePlus.scanResults.listen((scanResults) async {
subscription = FlutterBluePlus.scanResults.listen((scanResults) async {
// Select only 1 device to connect to
ScanResult? scanResult =
(scanResults.any((element) => element.device.name == name))
? scanResults.firstWhere((element) => element.device.name == name)
(scanResults.any((element) => element.device.platformName == name))
? scanResults
.firstWhere((element) => element.device.platformName == name)
: null;
// connect, stop scanning and clean streams
if (scanResult != null) {
await flutterBluePlus.stopScan();
await FlutterBluePlus.stopScan();
await scanResult.device.connect();
_bluetoothDevice = scanResult.device;
await _discoverAndSetup();
Expand All @@ -150,7 +150,7 @@ class MovisensDevice {

// Discovers services on device and instantiates them
Future<void> _discoverAndSetup() async {
id = _bluetoothDevice!.id.id;
id = _bluetoothDevice!.remoteId.str;
_log.info("Stored ID [$id] from Movisens device [$name]");
_log.info("Discovering services on Movisens device [$id]");
// Discover services
Expand Down
10 changes: 5 additions & 5 deletions packages/movisens_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
name: movisens_flutter
description: A Flutter plugin for Movisens devices, which connects to the device and listens for incoming data.
version: 3.0.0
version: 3.1.0
homepage: https://github.com/cph-cachet/flutter-plugins/tree/master/packages/movisens_flutter

environment:
sdk: ">=2.18.2 <4.0.0"
flutter: ">=3.0.0"

sdk: ">=3.2.0 <4.0.0"
flutter: ">=3.6.0"
dependencies:
flutter:
sdk: flutter
path_provider: ^2.0.2
flutter_blue_plus: ^1.3.1
flutter_blue_plus: ^1.29.1
rxdart: ^0.27.6
logging: ^1.1.0

Expand Down