Skip to content

Commit

Permalink
refactor: implement web platform
Browse files Browse the repository at this point in the history
  • Loading branch information
tnc1997 committed Nov 13, 2024
1 parent c2c8ebd commit cb67d67
Show file tree
Hide file tree
Showing 23 changed files with 1,839 additions and 185 deletions.
153 changes: 77 additions & 76 deletions README.md

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions packages/flutter_blue_plus/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

All notable changes to this project will be documented in this file. See [Conventional Commits](https://www.conventionalcommits.org) for commit guidelines.

## 1.2.0 (2024-11-13)

### Code Refactoring

* implement web platform

## 1.1.0 (2024-11-09)

### Code Refactoring
Expand Down
Binary file added packages/flutter_blue_plus/example/web/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions packages/flutter_blue_plus/example/web/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!DOCTYPE html>
<html>
<head>
<!--
If you are serving your web app in a path other than the root, change the
href value below to reflect the base path you are serving from.
The path provided below has to start and end with a slash "/" in order for
it to work correctly.
For more details:
* https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base
This is a placeholder for base href that will be replaced by the value of
the `--base-href` argument provided to `flutter build`.
-->
<base href="$FLUTTER_BASE_HREF">

<meta charset="UTF-8">
<meta content="IE=Edge" http-equiv="X-UA-Compatible">
<meta name="description" content="A new Flutter project.">

<!-- iOS meta tags & icons -->
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="apple-mobile-web-app-title" content="flutter_blue_plus_example">
<link rel="apple-touch-icon" href="icons/Icon-192.png">

<!-- Favicon -->
<link rel="icon" type="image/png" href="favicon.png"/>

<title>flutter_blue_plus_example</title>
<link rel="manifest" href="manifest.json">
</head>
<body>
<script src="flutter_bootstrap.js" async></script>
</body>
</html>
35 changes: 35 additions & 0 deletions packages/flutter_blue_plus/example/web/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "flutter_blue_plus_example",
"short_name": "flutter_blue_plus_example",
"start_url": ".",
"display": "standalone",
"background_color": "#0175C2",
"theme_color": "#0175C2",
"description": "A new Flutter project.",
"orientation": "portrait-primary",
"prefer_related_applications": false,
"icons": [
{
"src": "icons/Icon-192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "icons/Icon-512.png",
"sizes": "512x512",
"type": "image/png"
},
{
"src": "icons/Icon-maskable-192.png",
"sizes": "192x192",
"type": "image/png",
"purpose": "maskable"
},
{
"src": "icons/Icon-maskable-512.png",
"sizes": "512x512",
"type": "image/png",
"purpose": "maskable"
}
]
}
1 change: 1 addition & 0 deletions packages/flutter_blue_plus/lib/flutter_blue_plus.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ library flutter_blue_plus;
import 'dart:async';
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter_blue_plus_platform_interface/flutter_blue_plus_platform_interface.dart';

export 'package:flutter_blue_plus_platform_interface/flutter_blue_plus_platform_interface.dart' show DeviceIdentifier, Guid, LogLevel, PhySupport;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ class BluetoothCharacteristic {
}

// check
if (Platform.isMacOS || Platform.isIOS) {
assert(forceIndications == false, "iOS & macOS do not support forcing indications");
if (!kIsWeb && !Platform.isAndroid) {
assert(forceIndications == false, "Only Android supports forcing indications");
}

// Only allow a single ble operation to be underway at a time
Expand Down
26 changes: 13 additions & 13 deletions packages/flutter_blue_plus/lib/src/bluetooth_device.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class BluetoothDevice {
Future<BmConnectionStateResponse> futureState = responseStream.first;

// record connection time
if (Platform.isAndroid) {
if (!kIsWeb && Platform.isAndroid) {
FlutterBluePlus._connectTimestamp[remoteId] = DateTime.now();
}

Expand Down Expand Up @@ -171,7 +171,7 @@ class BluetoothDevice {
}

// request larger mtu
if (Platform.isAndroid && isConnected && mtu != null) {
if (!kIsWeb && Platform.isAndroid && isConnected && mtu != null) {
await requestMtu(mtu);
}
}
Expand Down Expand Up @@ -224,7 +224,7 @@ class BluetoothDevice {
await futureState.fbpEnsureAdapterIsOn("disconnect").fbpTimeout(timeout, "disconnect");
}

if (Platform.isAndroid) {
if (!kIsWeb && Platform.isAndroid) {
// Disconnected, remove connect timestamp
FlutterBluePlus._connectTimestamp.remove(remoteId);
}
Expand Down Expand Up @@ -279,10 +279,10 @@ class BluetoothDevice {
mtx.give();
}

// in order to match iOS behavior on all platforms,
// in order to match iOS behavior on all platforms (except web),
// we always listen to the Services Changed characteristic if it exists.
if (subscribeToServicesChanged) {
if (Platform.isIOS == false && Platform.isMacOS == false) {
if (!kIsWeb && !Platform.isIOS && !Platform.isMacOS) {
BluetoothCharacteristic? c = _servicesChangedCharacteristic;
if (c != null && (c.properties.notify || c.properties.indicate) && c.isNotifying == false) {
await c.setNotifyValue(true);
Expand Down Expand Up @@ -391,7 +391,7 @@ class BluetoothDevice {
/// - [predelay] adds delay to avoid race conditions on some peripherals. see comments below.
Future<int> requestMtu(int desiredMtu, {double predelay = 0.35, int timeout = 15}) async {
// check android
if (Platform.isAndroid == false) {
if (kIsWeb || !Platform.isAndroid) {
throw FlutterBluePlusException(ErrorPlatform.fbp, "requestMtu", FbpErrorCode.androidOnly.index, "android-only");
}

Expand Down Expand Up @@ -459,7 +459,7 @@ class BluetoothDevice {
/// Request connection priority update (Android only)
Future<void> requestConnectionPriority({required ConnectionPriority connectionPriorityRequest}) async {
// check android
if (Platform.isAndroid == false) {
if (kIsWeb || !Platform.isAndroid) {
throw FlutterBluePlusException(
ErrorPlatform.fbp, "requestConnectionPriority", FbpErrorCode.androidOnly.index, "android-only");
}
Expand Down Expand Up @@ -490,7 +490,7 @@ class BluetoothDevice {
required PhyCoding option,
}) async {
// check android
if (Platform.isAndroid == false) {
if (kIsWeb || !Platform.isAndroid) {
throw FlutterBluePlusException(
ErrorPlatform.fbp, "setPreferredPhy", FbpErrorCode.androidOnly.index, "android-only");
}
Expand All @@ -516,7 +516,7 @@ class BluetoothDevice {
/// Note! calling this is usually not necessary!! The platform does it automatically.
Future<void> createBond({int timeout = 90}) async {
// check android
if (Platform.isAndroid == false) {
if (kIsWeb || !Platform.isAndroid) {
throw FlutterBluePlusException(ErrorPlatform.fbp, "createBond", FbpErrorCode.androidOnly.index, "android-only");
}

Expand Down Expand Up @@ -563,7 +563,7 @@ class BluetoothDevice {
/// Remove bond (Android Only)
Future<void> removeBond({int timeout = 30}) async {
// check android
if (Platform.isAndroid == false) {
if (kIsWeb || !Platform.isAndroid) {
throw FlutterBluePlusException(ErrorPlatform.fbp, "removeBond", FbpErrorCode.androidOnly.index, "android-only");
}

Expand Down Expand Up @@ -604,7 +604,7 @@ class BluetoothDevice {
/// Refresh ble services & characteristics (Android Only)
Future<void> clearGattCache() async {
// check android
if (Platform.isAndroid == false) {
if (kIsWeb || !Platform.isAndroid) {
throw FlutterBluePlusException(
ErrorPlatform.fbp, "clearGattCache", FbpErrorCode.androidOnly.index, "android-only");
}
Expand All @@ -622,7 +622,7 @@ class BluetoothDevice {
/// Get the current bondState of the device (Android Only)
Stream<BluetoothBondState> get bondState async* {
// check android
if (Platform.isAndroid == false) {
if (kIsWeb || !Platform.isAndroid) {
throw FlutterBluePlusException(ErrorPlatform.fbp, "bondState", FbpErrorCode.androidOnly.index, "android-only");
}

Expand Down Expand Up @@ -661,7 +661,7 @@ class BluetoothDevice {
/// so FBP will have no idea this connection is active. Adding a delay fixes this issue.
/// https://issuetracker.google.com/issues/37121040
Future<void> _ensureAndroidDisconnectionDelay(int androidDelay) async {
if (Platform.isAndroid) {
if (!kIsWeb && Platform.isAndroid) {
if (FlutterBluePlus._connectTimestamp.containsKey(remoteId)) {
Duration minGap = Duration(milliseconds: androidDelay);
Duration elapsed = DateTime.now().difference(FlutterBluePlus._connectTimestamp[remoteId]!);
Expand Down
Loading

0 comments on commit cb67d67

Please sign in to comment.