Skip to content

Commit

Permalink
Added getConnectedDevices on Android.
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldemarco authored and Paul DeMarco committed Jun 19, 2019
1 parent 1cd04d7 commit 3b49a24
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,18 @@ public void onMethodCall(MethodCall call, Result result) {
break;
}

case "getConnectedDevices":
{
List<BluetoothDevice> devices = mBluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
Protos.ConnectedDevicesResponse.Builder p = Protos.ConnectedDevicesResponse.newBuilder();
for(BluetoothDevice d : devices) {
p.addDevices(ProtoMaker.from(d));
}
result.success(p.build().toByteArray());
log(LogLevel.EMERGENCY, "mGattServers size: " + mGattServers.size());
break;
}

case "connect":
{
byte[] data = call.arguments();
Expand Down
75 changes: 56 additions & 19 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_blue/flutter_blue.dart';
import 'package:flutter_blue_example/widgets.dart';
Expand Down Expand Up @@ -71,25 +73,60 @@ class FindDevicesScreen extends StatelessWidget {
body: RefreshIndicator(
onRefresh: () =>
FlutterBlue.instance.startScan(timeout: Duration(seconds: 4)),
child: StreamBuilder<List<ScanResult>>(
stream: FlutterBlue.instance.scanResults,
initialData: [],
builder: (c, snapshot) {
return ListView(
children: snapshot.data
.map(
(r) => ScanResultTile(
result: r,
onTap: () => Navigator.of(context)
.push(MaterialPageRoute(builder: (context) {
r.device.connect();
return DeviceScreen(device: r.device);
})),
),
)
.toList(),
);
},
child: SingleChildScrollView(
child: Column(
children: <Widget>[
StreamBuilder<List<BluetoothDevice>>(
stream: Stream.periodic(Duration(seconds: 2))
.asyncMap((_) => FlutterBlue.instance.connectedDevices),
initialData: [],
builder: (c, snapshot) => Column(
children: snapshot.data
.map((d) => ListTile(
title: Text(d.name),
subtitle: Text(d.id.toString()),
trailing: StreamBuilder<BluetoothDeviceState>(
stream: d.state,
initialData:
BluetoothDeviceState.disconnected,
builder: (c, snapshot) {
if (snapshot.data ==
BluetoothDeviceState.connected) {
return RaisedButton(
child: Text('OPEN'),
onPressed: () => Navigator.of(context)
.push(MaterialPageRoute(
builder: (context) =>
DeviceScreen(device: d))),
);
}
return Text(snapshot.data.toString());
},
),
))
.toList(),
),
),
StreamBuilder<List<ScanResult>>(
stream: FlutterBlue.instance.scanResults,
initialData: [],
builder: (c, snapshot) => Column(
children: snapshot.data
.map(
(r) => ScanResultTile(
result: r,
onTap: () => Navigator.of(context).push(
MaterialPageRoute(builder: (context) {
r.device.connect();
return DeviceScreen(device: r.device);
})),
),
)
.toList(),
),
),
],
),
),
),
floatingActionButton: StreamBuilder<bool>(
Expand Down
14 changes: 14 additions & 0 deletions ios/gen/Flutterblue.pbobjc.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions ios/gen/Flutterblue.pbobjc.m

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions lib/gen/flutterblue.pb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -997,3 +997,26 @@ class DeviceStateResponse extends $pb.GeneratedMessage {
void clearState() => clearField(2);
}

class ConnectedDevicesResponse extends $pb.GeneratedMessage {
static final $pb.BuilderInfo _i = new $pb.BuilderInfo('ConnectedDevicesResponse')
..pp<BluetoothDevice>(1, 'devices', $pb.PbFieldType.PM, BluetoothDevice.$checkItem, BluetoothDevice.create)
..hasRequiredFields = false
;

ConnectedDevicesResponse() : super();
ConnectedDevicesResponse.fromBuffer(List<int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) : super.fromBuffer(i, r);
ConnectedDevicesResponse.fromJson(String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) : super.fromJson(i, r);
ConnectedDevicesResponse clone() => new ConnectedDevicesResponse()..mergeFromMessage(this);
ConnectedDevicesResponse copyWith(void Function(ConnectedDevicesResponse) updates) => super.copyWith((message) => updates(message as ConnectedDevicesResponse));
$pb.BuilderInfo get info_ => _i;
static ConnectedDevicesResponse create() => new ConnectedDevicesResponse();
static $pb.PbList<ConnectedDevicesResponse> createRepeated() => new $pb.PbList<ConnectedDevicesResponse>();
static ConnectedDevicesResponse getDefault() => _defaultInstance ??= create()..freeze();
static ConnectedDevicesResponse _defaultInstance;
static void $checkItem(ConnectedDevicesResponse v) {
if (v is! ConnectedDevicesResponse) $pb.checkItemFailed(v, _i.qualifiedMessageName);
}

List<BluetoothDevice> get devices => $_getList(0);
}

7 changes: 7 additions & 0 deletions lib/gen/flutterblue.pbjson.dart
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,10 @@ const DeviceStateResponse_BluetoothDeviceState$json = const {
],
};

const ConnectedDevicesResponse$json = const {
'1': 'ConnectedDevicesResponse',
'2': const [
const {'1': 'devices', '3': 1, '4': 3, '5': 11, '6': '.BluetoothDevice', '10': 'devices'},
],
};

16 changes: 12 additions & 4 deletions lib/src/flutter_blue.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ class FlutterBlue {
.map((s) => BluetoothState.values[s.state.value]);
}

Future<List<BluetoothDevice>> get connectedDevices {
return _channel
.invokeMethod('getConnectedDevices')
.then((buffer) => protos.ConnectedDevicesResponse.fromBuffer(buffer))
.then((p) => p.devices)
.then((p) => p.map((d) => BluetoothDevice.fromProto(d)).toList());
}

/// Starts a scan for Bluetooth Low Energy devices
/// Timeout closes the stream after a specified [Duration]
Stream<ScanResult> scan({
Expand Down Expand Up @@ -132,10 +140,10 @@ class FlutterBlue {
/// The list of connected peripherals can include those that are connected
/// by other apps and that will need to be connected locally using the
/// device.connect() method before they can be used.
Stream<List<BluetoothDevice>> connectedDevices({
List<Guid> withServices = const [],
}) =>
throw UnimplementedError();
// Stream<List<BluetoothDevice>> connectedDevices({
// List<Guid> withServices = const [],
// }) =>
// throw UnimplementedError();

/// Sets the log level of the FlutterBlue instance
/// Messages equal or below the log level specified are stored/forwarded,
Expand Down
4 changes: 4 additions & 0 deletions protos/flutterblue.proto
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,8 @@ message DeviceStateResponse {
}
string remote_id = 1;
BluetoothDeviceState state = 2;
}

message ConnectedDevicesResponse {
repeated BluetoothDevice devices = 1;
}

0 comments on commit 3b49a24

Please sign in to comment.