Skip to content

Commit

Permalink
Add isDiscoverable to detect is discoverable mode active
Browse files Browse the repository at this point in the history
- Remove `getDiscoverableTimeout` since it was not working properly (almost always returning last set timeout)
+ Add example with countdown for discoverable mode timeout
  • Loading branch information
AgainPsychoX committed Jun 27, 2019
1 parent 8736bde commit 1365778
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -359,18 +359,9 @@ public void onResult(boolean granted) {
result.success(null);
break;

case "getDiscoverableTimeout": {
try {
java.lang.reflect.Method method;
method = bluetoothAdapter.getClass().getMethod("getDiscoverableTimeout");
int value = (int) method.invoke(bluetoothAdapter);
result.success(value);
}
catch (Exception ex) {
result.error("bond_error", "error while unbonding", exceptionToString(ex));
}
case "isDiscoverable":
result.success(bluetoothAdapter.getScanMode() == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE);
break;
}

case "requestDiscoverable": {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
Expand Down
30 changes: 28 additions & 2 deletions example/lib/MainPage.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';
import 'package:scoped_model/scoped_model.dart';
Expand All @@ -18,6 +19,9 @@ class MainPage extends StatefulWidget {
class _MainPage extends State<MainPage> {
BluetoothState _bluetoothState = BluetoothState.UNKNOWN;

Timer _discoverableTimeoutTimer;
int _discoverableTimeoutSecondsLeft = 0;

BackgroundCollectingTask _collectingTask;

@override
Expand All @@ -31,13 +35,20 @@ class _MainPage extends State<MainPage> {

// Listen for futher state changes
FlutterBluetoothSerial.instance.onStateChanged().listen((BluetoothState state) {
setState(() { _bluetoothState = state; });
setState(() {
_bluetoothState = state;

// Discoverable mode is disabled when Bluetooth gets disabled
_discoverableTimeoutTimer = null;
_discoverableTimeoutSecondsLeft = 0;
});
});
}

@override
void dispose() {
_collectingTask?.dispose();
_discoverableTimeoutTimer?.cancel();
super.dispose();
}

Expand Down Expand Up @@ -81,7 +92,7 @@ class _MainPage extends State<MainPage> {
),
),
ListTile(
title: Text("Discoverable"),
title: _discoverableTimeoutSecondsLeft == 0 ? const Text("Discoverable") : Text("Discoverable for $_discoverableTimeoutSecondsLeft seconds"),
subtitle: const Text("PsychoX-Luna"),
trailing: Row(
mainAxisSize: MainAxisSize.min,
Expand All @@ -101,6 +112,21 @@ class _MainPage extends State<MainPage> {
else {
print('Discoverable mode acquired for $timeout seconds');
}
setState(() {
_discoverableTimeoutTimer?.cancel();
_discoverableTimeoutSecondsLeft = timeout;
_discoverableTimeoutTimer = Timer.periodic(Duration(seconds: 1), (Timer timer) {
setState(() {
if (_discoverableTimeoutSecondsLeft <= 0) {
timer.cancel();
_discoverableTimeoutSecondsLeft = 0;
}
else {
_discoverableTimeoutSecondsLeft -= 1;
}
});
});
});
},
)
]
Expand Down
5 changes: 2 additions & 3 deletions lib/FlutterBluetoothSerial.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,8 @@ class FlutterBluetoothSerial {

// @TODO . add `discoverableName` (get/set)

/// Timeout in seconds left in discoverable mode. Negative values means
/// out of discoverable mode or error (such as adapter disabled).
Future<int> get discoverableTimeout => _methodChannel.invokeMethod("getDiscoverableTimeout");
/// Describes is the local device in discoverable mode.
Future<bool> get isDiscoverable => _methodChannel.invokeMethod("isDiscoverable");

/// Asks for discoverable mode (probably always prompt for user interaction in fact).
/// Returns number of seconds acquired or zero if canceled or failed gracefully.
Expand Down

0 comments on commit 1365778

Please sign in to comment.