Skip to content

Commit

Permalink
Merge pull request #5 from juliansteenbakker/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
juliansteenbakker authored Apr 7, 2020
2 parents 1224778 + 3ed316b commit 1251d5b
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 202 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.0.3

Fixed callback on Android

## 0.0.2

Fixed flutter v2 embedding
Expand Down
5 changes: 4 additions & 1 deletion android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
<manifest package="dev.steenbakker.flutter_ble_peripheral">
<manifest package="dev.steenbakker.flutter_ble_peripheral"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand All @@ -85,7 +77,7 @@ class FlutterBlePeripheralPlugin: FlutterPlugin, MethodChannel.MethodCallHandler
// arguments["manufacturerId"] as Int?
)

peripheral!!.start(beaconData, advertiseCallback)
peripheral!!.start(beaconData)
result.success(null)
}

Expand All @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -110,4 +91,5 @@ class Peripheral {
settingsBuilder.setTimeout(0)
return settingsBuilder.build()
}
}
}

1 change: 1 addition & 0 deletions example/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true
android.enableR8=true
11 changes: 4 additions & 7 deletions example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
66 changes: 34 additions & 32 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -18,58 +17,61 @@ class MyApp extends StatefulWidget {
}

class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
FlutterBlePeripheral blePeripheral = FlutterBlePeripheral();
String _uuid = 'bf27730d-860a-4e09-889c-2d8b6a9e0fe7';
bool _isBroadcasting = false;

@override
void initState() {
super.initState();
initPlatformState();
}

// Platform messages are asynchronous, so we initialize in an async method.
Future<void> 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
Widget build(BuildContext context) {
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: <Widget>[
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),
)),
])
),
),
);
Expand Down
52 changes: 2 additions & 50 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
6 changes: 0 additions & 6 deletions ios/Classes/FlutterBlePeripheralPlugin.h
Original file line number Diff line number Diff line change
@@ -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 <Flutter/Flutter.h>

@interface FlutterBlePeripheralPlugin : NSObject<FlutterPlugin>
Expand Down
6 changes: 0 additions & 6 deletions ios/Classes/FlutterBlePeripheralPlugin.m
Original file line number Diff line number Diff line change
@@ -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(<flutter_ble_peripheral/flutter_ble_peripheral-Swift.h>)
#import <flutter_ble_peripheral/flutter_ble_peripheral-Swift.h>
Expand Down
10 changes: 6 additions & 4 deletions ios/flutter_ble_peripheral.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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 protected]' }
s.source = { :path => '.' }
Expand Down
Loading

0 comments on commit 1251d5b

Please sign in to comment.