From bfec475086543830d3b9c03f4e8d759692bcc215 Mon Sep 17 00:00:00 2001 From: Julian Steenbakker Date: Tue, 7 Apr 2020 09:05:29 +0200 Subject: [PATCH 1/2] Merge branch 'master' of /Users/bever/Documents/test/old/flutter_ble_peripheral with conflicts. --- ios/Classes/FlutterBlePeripheralPlugin.h | 6 --- ios/Classes/FlutterBlePeripheralPlugin.m | 6 --- pubspec.yaml | 52 +++++++++++++++++++++--- 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/ios/Classes/FlutterBlePeripheralPlugin.h b/ios/Classes/FlutterBlePeripheralPlugin.h index 3082cbf..d92cb02 100644 --- a/ios/Classes/FlutterBlePeripheralPlugin.h +++ b/ios/Classes/FlutterBlePeripheralPlugin.h @@ -1,9 +1,3 @@ -/* -* Copyright (c) 2020. Julian Steenbakker. -* All rights reserved. Use of this source code is governed by a -* BSD-style license that can be found in the LICENSE file. -*/ - #import @interface FlutterBlePeripheralPlugin : NSObject diff --git a/ios/Classes/FlutterBlePeripheralPlugin.m b/ios/Classes/FlutterBlePeripheralPlugin.m index 3994558..9fb78ea 100644 --- a/ios/Classes/FlutterBlePeripheralPlugin.m +++ b/ios/Classes/FlutterBlePeripheralPlugin.m @@ -1,9 +1,3 @@ -/* -* Copyright (c) 2020. Julian Steenbakker. -* All rights reserved. Use of this source code is governed by a -* BSD-style license that can be found in the LICENSE file. -*/ - #import "FlutterBlePeripheralPlugin.h" #if __has_include() #import diff --git a/pubspec.yaml b/pubspec.yaml index 831fd44..dcb8928 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,12 +1,13 @@ name: flutter_ble_peripheral -description: Flutter plugin for advertising in peripheral mode -version: 0.0.1 -author: Julian Steenbakker +description: This plugin enables a device to be set into peripheral mode, and advertise custom + services and characteristics. +version: 0.0.2 homepage: https://github.com/juliansteenbakker/flutter_ble_peripheral + environment: - sdk: ">=2.1.0 <3.0.0" - flutter: ">=1.12.0 <2.0.0" + sdk: ">=2.7.0 <3.0.0" + flutter: ">=1.10.0" dependencies: flutter: @@ -16,11 +17,50 @@ dev_dependencies: flutter_test: sdk: flutter +# For information on the generic Dart part of this file, see the +# following page: https://dart.dev/tools/pub/pubspec + +# The following section is specific to Flutter. flutter: + # This section identifies this Flutter project as a plugin project. + # The 'pluginClass' and Android 'package' identifiers should not ordinarily + # be modified. They are used by the tooling to maintain consistency when + # adding or updating assets for this project. plugin: - platform: + platforms: android: package: dev.steenbakker.flutter_ble_peripheral pluginClass: FlutterBlePeripheralPlugin ios: pluginClass: FlutterBlePeripheralPlugin + + # To add assets to your plugin package, add an assets section, like this: + # assets: + # - images/a_dot_burr.jpeg + # - images/a_dot_ham.jpeg + # + # For details regarding assets in packages, see + # https://flutter.dev/assets-and-images/#from-packages + # + # An image asset can refer to one or more resolution-specific "variants", see + # https://flutter.dev/assets-and-images/#resolution-aware. + + # To add custom fonts to your plugin package, add a fonts section here, + # in this "flutter" section. Each entry in this list should have a + # "family" key with the font family name, and a "fonts" key with a + # list giving the asset and other descriptors for the font. For + # example: + # fonts: + # - family: Schyler + # fonts: + # - asset: fonts/Schyler-Regular.ttf + # - asset: fonts/Schyler-Italic.ttf + # style: italic + # - family: Trajan Pro + # fonts: + # - asset: fonts/TrajanPro.ttf + # - asset: fonts/TrajanPro_Bold.ttf + # weight: 700 + # + # For details regarding fonts in packages, see + # https://flutter.dev/custom-fonts/#from-packages From b455038bebc1cf06edf42df1e1d1a24cafc6fcd0 Mon Sep 17 00:00:00 2001 From: Julian Steenbakker Date: Tue, 7 Apr 2020 12:15:28 +0200 Subject: [PATCH 2/2] Fixed callback on Android --- CHANGELOG.md | 4 ++ android/src/main/AndroidManifest.xml | 5 +- .../FlutterBlePeripheralPlugin.kt | 15 ++--- .../flutter_ble_peripheral/Peripheral.kt | 66 +++++++------------ example/android/gradle.properties | 1 + example/ios/Runner.xcodeproj/project.pbxproj | 11 ++-- example/lib/main.dart | 66 ++++++++++--------- example/pubspec.yaml | 52 +-------------- ios/flutter_ble_peripheral.podspec | 10 +-- pubspec.yaml | 47 ++----------- 10 files changed, 87 insertions(+), 190 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d97bec..078a94a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.0.3 + +Fixed callback on Android + ## 0.0.2 Fixed flutter v2 embedding diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index ddc271c..3e21587 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -1,2 +1,5 @@ - + + + diff --git a/android/src/main/kotlin/dev/steenbakker/flutter_ble_peripheral/FlutterBlePeripheralPlugin.kt b/android/src/main/kotlin/dev/steenbakker/flutter_ble_peripheral/FlutterBlePeripheralPlugin.kt index b91ae36..d057bb0 100644 --- a/android/src/main/kotlin/dev/steenbakker/flutter_ble_peripheral/FlutterBlePeripheralPlugin.kt +++ b/android/src/main/kotlin/dev/steenbakker/flutter_ble_peripheral/FlutterBlePeripheralPlugin.kt @@ -18,9 +18,6 @@ class FlutterBlePeripheralPlugin: FlutterPlugin, MethodChannel.MethodCallHandler private var eventChannel: EventChannel? = null private var peripheral: Peripheral? = null private var eventSink: EventChannel.EventSink? = null - private var advertiseCallback: (Boolean) -> Unit = { isAdvertising -> - eventSink?.success(isAdvertising) - } /** Plugin registration embedding v1 */ companion object { @@ -58,13 +55,8 @@ class FlutterBlePeripheralPlugin: FlutterPlugin, MethodChannel.MethodCallHandler // TODO: Add permission check override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: MethodChannel.Result) { when (call.method) { - "start" -> { - print("Start advertising") - startPeripheral(call, result) - } - "stop" -> { - stopPeripheral(result) - } + "start" -> startPeripheral(call, result) + "stop" -> stopPeripheral(result) "isAdvertising" -> result.success(peripheral!!.isAdvertising()) // "isTransmissionSupported" -> isTransmissionSupported(result) else -> result.notImplemented() @@ -85,7 +77,7 @@ class FlutterBlePeripheralPlugin: FlutterPlugin, MethodChannel.MethodCallHandler // arguments["manufacturerId"] as Int? ) - peripheral!!.start(beaconData, advertiseCallback) + peripheral!!.start(beaconData) result.success(null) } @@ -94,6 +86,7 @@ class FlutterBlePeripheralPlugin: FlutterPlugin, MethodChannel.MethodCallHandler result.success(null) } + // TODO: Fix listeners override fun onListen(event: Any?, eventSink: EventChannel.EventSink) { this.eventSink = eventSink } diff --git a/android/src/main/kotlin/dev/steenbakker/flutter_ble_peripheral/Peripheral.kt b/android/src/main/kotlin/dev/steenbakker/flutter_ble_peripheral/Peripheral.kt index d0ac855..4308aa2 100644 --- a/android/src/main/kotlin/dev/steenbakker/flutter_ble_peripheral/Peripheral.kt +++ b/android/src/main/kotlin/dev/steenbakker/flutter_ble_peripheral/Peripheral.kt @@ -23,65 +23,46 @@ class Peripheral { private var mBluetoothLeAdvertiser: BluetoothLeAdvertiser? = null private var advertiseCallback: AdvertiseCallback? = null private val tag = "FlutterBlePeripheral" + + private val mAdvertiseCallback = object : AdvertiseCallback() { + override fun onStartSuccess(settingsInEffect: AdvertiseSettings) { + super.onStartSuccess(settingsInEffect) + Log.i(tag, "LE Advertise Started.") + //advertisingCallback(true) + isAdvertising = true + } + override fun onStartFailure(errorCode: Int) { + super.onStartFailure(errorCode) + Log.e(tag, "ERROR while starting advertising: $errorCode") + //advertisingCallback(false) + isAdvertising = false + } + } + fun init(context: Context) { if (mBluetoothLeAdvertiser == null) { - val mBluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager? - if (mBluetoothManager != null) { - val mBluetoothAdapter = mBluetoothManager.adapter - if (mBluetoothAdapter != null) { - mBluetoothLeAdvertiser = mBluetoothAdapter.bluetoothLeAdvertiser - } - } + mBluetoothLeAdvertiser = (context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager).adapter.bluetoothLeAdvertiser } } - - fun start(data: Data, advertisingCallback: ((Boolean) -> Unit)) { + + fun start(data: Data) { val settings = buildAdvertiseSettings() val advertiseData = buildAdvertiseData(data.uuid, false) - - advertiseCallback = object : AdvertiseCallback() { - override fun onStartSuccess(settingsInEffect: AdvertiseSettings?) { - super.onStartSuccess(settingsInEffect) - Log.d(tag, "Started advertising") - advertisingCallback(true) - isAdvertising = true - } - - override fun onStartFailure(errorCode: Int) { - super.onStartFailure(errorCode) - Log.d(tag, "ERROR advertising: $errorCode") - advertisingCallback(false) - isAdvertising = false - } - } - - mBluetoothLeAdvertiser?.startAdvertising(settings, advertiseData, advertiseCallback) + mBluetoothLeAdvertiser!!.startAdvertising(settings, advertiseData, mAdvertiseCallback) } fun isAdvertising(): Boolean { return isAdvertising } + // TODO: Fix transmission supported type // fun isTransmissionSupported(): Int { // return checkTransmissionSupported(context) // } fun stop() { - Log.d(tag, "Stopped advertising") - - mBluetoothLeAdvertiser!!.stopAdvertising(object : AdvertiseCallback() { - override fun onStartSuccess(settingsInEffect: AdvertiseSettings?) { - super.onStartSuccess(settingsInEffect) - advertiseCallback = null - isAdvertising = false - } - - override fun onStartFailure(errorCode: Int) { - super.onStartFailure(errorCode) - Log.d(tag, "ERROR advertising: $errorCode") - } - }) + mBluetoothLeAdvertiser!!.stopAdvertising(mAdvertiseCallback) advertiseCallback = null isAdvertising = false } @@ -110,4 +91,5 @@ class Peripheral { settingsBuilder.setTimeout(0) return settingsBuilder.build() } -} \ No newline at end of file +} + diff --git a/example/android/gradle.properties b/example/android/gradle.properties index 94adc3a..a673820 100644 --- a/example/android/gradle.properties +++ b/example/android/gradle.properties @@ -1,3 +1,4 @@ org.gradle.jvmargs=-Xmx1536M android.useAndroidX=true android.enableJetifier=true +android.enableR8=true diff --git a/example/ios/Runner.xcodeproj/project.pbxproj b/example/ios/Runner.xcodeproj/project.pbxproj index 3aa4c34..5e1676b 100644 --- a/example/ios/Runner.xcodeproj/project.pbxproj +++ b/example/ios/Runner.xcodeproj/project.pbxproj @@ -210,15 +210,12 @@ buildActionMask = 2147483647; files = ( ); - inputPaths = ( - "${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks.sh", - "${PODS_ROOT}/../Flutter/Flutter.framework", - "${BUILT_PRODUCTS_DIR}/flutter_ble_peripheral/flutter_ble_peripheral.framework", + inputFileListPaths = ( + "${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks-${CONFIGURATION}-input-files.xcfilelist", ); name = "[CP] Embed Pods Frameworks"; - outputPaths = ( - "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Flutter.framework", - "${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/flutter_ble_peripheral.framework", + outputFileListPaths = ( + "${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-frameworks-${CONFIGURATION}-output-files.xcfilelist", ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; diff --git a/example/lib/main.dart b/example/lib/main.dart index f21d9db..5c36a6e 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -7,7 +7,6 @@ import 'package:flutter/material.dart'; import 'dart:async'; -import 'package:flutter/services.dart'; import 'package:flutter_ble_peripheral/flutter_ble_peripheral.dart'; void main() => runApp(MyApp()); @@ -18,7 +17,9 @@ class MyApp extends StatefulWidget { } class _MyAppState extends State { - String _platformVersion = 'Unknown'; + FlutterBlePeripheral blePeripheral = FlutterBlePeripheral(); + String _uuid = 'bf27730d-860a-4e09-889c-2d8b6a9e0fe7'; + bool _isBroadcasting = false; @override void initState() { @@ -26,31 +27,25 @@ class _MyAppState extends State { initPlatformState(); } - // Platform messages are asynchronous, so we initialize in an async method. Future initPlatformState() async { - String platformVersion; - // Platform messages may fail, so we use a try/catch PlatformException. -// try { -// platformVersion = await FlutterBlePeripheral.platformVersion; -// } on PlatformException { -// platformVersion = 'Failed to get platform version.'; -// } - - // If the widget was removed from the tree while the asynchronous platform - // message was in flight, we want to discard the reply rather than calling - // setState to update our non-existent appearance. - if (!mounted) return; - + bool isAdvertising = await blePeripheral.isAdvertising(); setState(() { - _platformVersion = platformVersion; + _isBroadcasting = isAdvertising; }); } - FlutterBlePeripheral blePeripheral = FlutterBlePeripheral(); - - void startBroadcast() { - String uuid = 'bf27730d-860a-4e09-889c-2d8b6a9e0fe7'; - blePeripheral.start(uuid); + void _toggleAdvertise() async { + if (await blePeripheral.isAdvertising()) { + blePeripheral.stop(); + setState(() { + _isBroadcasting = false; + }); + } else { + blePeripheral.start(_uuid); + setState(() { + _isBroadcasting = true; + }); + } } @override @@ -58,18 +53,25 @@ class _MyAppState extends State { return MaterialApp( home: Scaffold( appBar: AppBar( - title: const Text('FlutterBlePeripheral'), + title: const Text('Flutter BLE Peripheral'), ), body: Center( - child: FlatButton( - onPressed: () => startBroadcast(), - child: Text( - 'start broadcasting', - style: Theme.of(context) - .primaryTextTheme - .button - .copyWith(color: Colors.blue), - )) + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + Text('Is advertising: ' + _isBroadcasting.toString()), + Text('Current uuid is ' + _uuid), + FlatButton( + onPressed: () => _toggleAdvertise(), + child: Text( + 'Toggle advertising', + style: Theme.of(context) + .primaryTextTheme + .button + .copyWith(color: Colors.blue), + )), + ]) ), ), ); diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 87f75a8..18394f2 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -1,9 +1,7 @@ name: flutter_ble_peripheral_example description: Demonstrates how to use the flutter_ble_peripheral plugin. -# The following line prevents the package from being accidentally published to -# pub.dev using `pub publish`. This is preferred for private packages. -publish_to: 'none' # Remove this line if you wish to publish to pub.dev +publish_to: 'none' environment: sdk: ">=2.7.0 <3.0.0" @@ -13,59 +11,13 @@ dependencies: sdk: flutter flutter_ble_peripheral: - # When depending on this package from a real application you should use: - # flutter_ble_peripheral: ^x.y.z - # See https://dart.dev/tools/pub/dependencies#version-constraints - # The example app is bundled with the plugin so we use a path dependency on - # the parent directory to use the current plugin's version. path: ../ - # The following adds the Cupertino Icons font to your application. - # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^0.1.3 dev_dependencies: flutter_test: sdk: flutter -# For information on the generic Dart part of this file, see the -# following page: https://dart.dev/tools/pub/pubspec - -# The following section is specific to Flutter. flutter: - - # The following line ensures that the Material Icons font is - # included with your application, so that you can use the icons in - # the material Icons class. - uses-material-design: true - - # To add assets to your application, add an assets section, like this: - # assets: - # - images/a_dot_burr.jpeg - # - images/a_dot_ham.jpeg - - # An image asset can refer to one or more resolution-specific "variants", see - # https://flutter.dev/assets-and-images/#resolution-aware. - - # For details regarding adding assets from package dependencies, see - # https://flutter.dev/assets-and-images/#from-packages - - # To add custom fonts to your application, add a fonts section here, - # in this "flutter" section. Each entry in this list should have a - # "family" key with the font family name, and a "fonts" key with a - # list giving the asset and other descriptors for the font. For - # example: - # fonts: - # - family: Schyler - # fonts: - # - asset: fonts/Schyler-Regular.ttf - # - asset: fonts/Schyler-Italic.ttf - # style: italic - # - family: Trajan Pro - # fonts: - # - asset: fonts/TrajanPro.ttf - # - asset: fonts/TrajanPro_Bold.ttf - # weight: 700 - # - # For details regarding fonts from package dependencies, - # see https://flutter.dev/custom-fonts/#from-packages + uses-material-design: true \ No newline at end of file diff --git a/ios/flutter_ble_peripheral.podspec b/ios/flutter_ble_peripheral.podspec index 3d58db7..b2f0581 100644 --- a/ios/flutter_ble_peripheral.podspec +++ b/ios/flutter_ble_peripheral.podspec @@ -4,12 +4,14 @@ # Pod::Spec.new do |s| s.name = 'flutter_ble_peripheral' - s.version = '0.0.1' - s.summary = 'A new flutter plugin project.' + s.version = '0.0.3' + s.summary = 'This plugin enables a device to be set into peripheral mode, and advertise custom + services and characteristics.' s.description = <<-DESC -A new flutter plugin project. +This plugin enables a device to be set into peripheral mode, and advertise custom + services and characteristics. DESC - s.homepage = 'http://example.com' + s.homepage = 'https://steenbakker.dev' s.license = { :file => '../LICENSE' } s.author = { 'Your Company' => 'email@example.com' } s.source = { :path => '.' } diff --git a/pubspec.yaml b/pubspec.yaml index 8bed805..3f38509 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,9 +1,9 @@ name: flutter_ble_peripheral -description: Flutter plugin for advertising in peripheral mode -version: 0.0.2 +description: This plugin enables a device to be set into peripheral mode, and advertise custom + services and characteristics. +version: 0.0.3 homepage: https://github.com/juliansteenbakker/flutter_ble_peripheral - environment: sdk: ">=2.7.0 <3.0.0" flutter: ">=1.10.0" @@ -16,50 +16,11 @@ dev_dependencies: flutter_test: sdk: flutter -# For information on the generic Dart part of this file, see the -# following page: https://dart.dev/tools/pub/pubspec - -# The following section is specific to Flutter. flutter: - # This section identifies this Flutter project as a plugin project. - # The 'pluginClass' and Android 'package' identifiers should not ordinarily - # be modified. They are used by the tooling to maintain consistency when - # adding or updating assets for this project. plugin: platforms: android: package: dev.steenbakker.flutter_ble_peripheral pluginClass: FlutterBlePeripheralPlugin ios: - pluginClass: FlutterBlePeripheralPlugin - - # To add assets to your plugin package, add an assets section, like this: - # assets: - # - images/a_dot_burr.jpeg - # - images/a_dot_ham.jpeg - # - # For details regarding assets in packages, see - # https://flutter.dev/assets-and-images/#from-packages - # - # An image asset can refer to one or more resolution-specific "variants", see - # https://flutter.dev/assets-and-images/#resolution-aware. - - # To add custom fonts to your plugin package, add a fonts section here, - # in this "flutter" section. Each entry in this list should have a - # "family" key with the font family name, and a "fonts" key with a - # list giving the asset and other descriptors for the font. For - # example: - # fonts: - # - family: Schyler - # fonts: - # - asset: fonts/Schyler-Regular.ttf - # - asset: fonts/Schyler-Italic.ttf - # style: italic - # - family: Trajan Pro - # fonts: - # - asset: fonts/TrajanPro.ttf - # - asset: fonts/TrajanPro_Bold.ttf - # weight: 700 - # - # For details regarding fonts in packages, see - # https://flutter.dev/custom-fonts/#from-packages + pluginClass: FlutterBlePeripheralPlugin \ No newline at end of file