Skip to content

Commit

Permalink
Merge branch 'boskokg-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
MrCsabaToth committed Jul 24, 2022
2 parents 76aed89 + 9361909 commit 79eb0da
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 22 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 1.2.0
* connect timeout fixed (thanks to crazy-rodney, sophisticode, SkuggaEdward, MousyBusiness and cthurston)
* Add timestamp field to ScanResult class #59 (thanks to simon-iversen)
* Add FlutterBlue.name to get the human readable device name #93 (thanks to mvo5)
* Fix bug where if there were multiple subscribers to FlutterBlue.state and one cancelled it would accidentally cancel all subscribers (thank to MacMalainey and MrCsabaToth)

## 1.1.3
* Read RSSI from a connected BLE device #1 (thanks to sophisticode)
* Fixed a crash on Android OS 12 (added check for BLUETOOTH_CONNECT permission) (fixed by dspells)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,15 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
break;
}

case "name":
{
String name = mBluetoothAdapter.getName();
if (name == null)
name = "";
result.success(name);
break;
}

case "turnOn":
{
if (!mBluetoothAdapter.isEnabled()) {
Expand Down Expand Up @@ -350,6 +359,15 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
break;
}

case "pair":
{
String deviceId = (String)call.arguments;
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(deviceId);
device.createBond();
result.success(null);
break;
}

case "disconnect":
{
String deviceId = (String)call.arguments;
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.1.3"
version: "1.2.0"
flutter_lints:
dependency: "direct dev"
description:
Expand Down
2 changes: 2 additions & 0 deletions ios/Classes/FlutterBluePlusPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
} else {
result(@(NO));
}
} else if([@"name" isEqualToString:call.method]) {
result([[UIDevice currentDevice] name]);
} else if([@"startScan" isEqualToString:call.method]) {
// Clear any existing scan results
[self.scannedPeripherals removeAllObjects];
Expand Down
1 change: 1 addition & 0 deletions lib/flutter_blue_plus.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
library flutter_blue_plus;

import 'dart:async';
import 'dart:io';

import 'package:collection/collection.dart';
import 'package:convert/convert.dart';
Expand Down
37 changes: 21 additions & 16 deletions lib/src/bluetooth_device.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,35 @@ class BluetoothDevice {
Duration? timeout,
bool autoConnect = true,
}) async {
final completer = Completer<void>();
var request = protos.ConnectRequest.create()
..remoteId = id.toString()
..androidAutoConnect = autoConnect;

Timer? timer;
if (timeout != null) {
timer = Timer(timeout, () {
disconnect();
completer.completeError(
TimeoutException('Failed to connect in time.', timeout));
});
}

await FlutterBluePlus.instance._channel
.invokeMethod('connect', request.writeToBuffer());

await state.firstWhere((s) => s == BluetoothDeviceState.connected);

timer?.cancel();

completer.complete();
if (timeout == null) {
await state.firstWhere((s) => s == BluetoothDeviceState.connected);
} else {
await state
.firstWhere((s) => s == BluetoothDeviceState.connected)
.timeout(
timeout,
onTimeout: () {
disconnect();
throw TimeoutException('Failed to connect in time.', timeout);
},
);
}
}

return completer.future;
/// Send a pairing request to the device.
/// Currently only implemented on Android.
Future<void> pair() async {
if (Platform.isAndroid) {
return FlutterBluePlus.instance._channel
.invokeMethod('pair', id.toString());
}
}

/// Cancels connection to the Bluetooth Device
Expand Down
14 changes: 10 additions & 4 deletions lib/src/flutter_blue_plus.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class FlutterBluePlus {
Stream<MethodCall> get _methodStream => _methodStreamController
.stream; // Used internally to dispatch methods from platform.

/// Fix for issue https://github.com/pauldemarco/flutter_blue/issues/608
/// Cached broadcast stream for FlutterBlue.state events
/// Caching this stream allows for more than one listener to subscribe
/// and unsubscribe apart from each other,
Expand All @@ -41,6 +40,10 @@ class FlutterBluePlus {
Future<bool> get isAvailable =>
_channel.invokeMethod('isAvailable').then<bool>((d) => d);

/// Return the friendly Bluetooth name of the local Bluetooth adapter
Future<String> get name =>
_channel.invokeMethod('name').then<String>((d) => d);

/// Checks if Bluetooth functionality is turned on
Future<bool> get isOn => _channel.invokeMethod('isOn').then<bool>((d) => d);

Expand Down Expand Up @@ -93,7 +96,8 @@ class FlutterBluePlus {
_stateStream ??= _stateChannel
.receiveBroadcastStream()
.map((buffer) => protos.BluetoothState.fromBuffer(buffer))
.map((s) => BluetoothState.values[s.state.value]);
.map((s) => BluetoothState.values[s.state.value])
.doOnCancel(() => _stateStream = null);

yield* _stateStream!;
}
Expand Down Expand Up @@ -289,11 +293,13 @@ class ScanResult {
ScanResult.fromProto(protos.ScanResult p)
: device = BluetoothDevice.fromProto(p.device),
advertisementData = AdvertisementData.fromProto(p.advertisementData),
rssi = p.rssi;
rssi = p.rssi,
timeStamp = DateTime.now();

final BluetoothDevice device;
final AdvertisementData advertisementData;
final int rssi;
final DateTime timeStamp;

@override
bool operator ==(Object other) =>
Expand All @@ -307,7 +313,7 @@ class ScanResult {

@override
String toString() {
return 'ScanResult{device: $device, advertisementData: $advertisementData, rssi: $rssi}';
return 'ScanResult{device: $device, advertisementData: $advertisementData, rssi: $rssi, timeStamp: $timeStamp}';
}
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_blue_plus
description: Flutter plugin for connecting and communicationg with Bluetooth Low Energy devices, on Android and iOS
version: 1.1.3
version: 1.2.0
homepage: https://github.com/boskokg/flutter_blue_plus

environment:
Expand Down

0 comments on commit 79eb0da

Please sign in to comment.