Skip to content

Commit 3b49a24

Browse files
pauldemarcoPaul DeMarco
authored and
Paul DeMarco
committed
Added getConnectedDevices on Android.
1 parent 1cd04d7 commit 3b49a24

File tree

8 files changed

+171
-23
lines changed

8 files changed

+171
-23
lines changed

android/src/main/java/com/pauldemarco/flutterblue/FlutterBluePlugin.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,18 @@ public void onMethodCall(MethodCall call, Result result) {
170170
break;
171171
}
172172

173+
case "getConnectedDevices":
174+
{
175+
List<BluetoothDevice> devices = mBluetoothManager.getConnectedDevices(BluetoothProfile.GATT);
176+
Protos.ConnectedDevicesResponse.Builder p = Protos.ConnectedDevicesResponse.newBuilder();
177+
for(BluetoothDevice d : devices) {
178+
p.addDevices(ProtoMaker.from(d));
179+
}
180+
result.success(p.build().toByteArray());
181+
log(LogLevel.EMERGENCY, "mGattServers size: " + mGattServers.size());
182+
break;
183+
}
184+
173185
case "connect":
174186
{
175187
byte[] data = call.arguments();

example/lib/main.dart

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:async';
6+
57
import 'package:flutter/material.dart';
68
import 'package:flutter_blue/flutter_blue.dart';
79
import 'package:flutter_blue_example/widgets.dart';
@@ -71,25 +73,60 @@ class FindDevicesScreen extends StatelessWidget {
7173
body: RefreshIndicator(
7274
onRefresh: () =>
7375
FlutterBlue.instance.startScan(timeout: Duration(seconds: 4)),
74-
child: StreamBuilder<List<ScanResult>>(
75-
stream: FlutterBlue.instance.scanResults,
76-
initialData: [],
77-
builder: (c, snapshot) {
78-
return ListView(
79-
children: snapshot.data
80-
.map(
81-
(r) => ScanResultTile(
82-
result: r,
83-
onTap: () => Navigator.of(context)
84-
.push(MaterialPageRoute(builder: (context) {
85-
r.device.connect();
86-
return DeviceScreen(device: r.device);
87-
})),
88-
),
89-
)
90-
.toList(),
91-
);
92-
},
76+
child: SingleChildScrollView(
77+
child: Column(
78+
children: <Widget>[
79+
StreamBuilder<List<BluetoothDevice>>(
80+
stream: Stream.periodic(Duration(seconds: 2))
81+
.asyncMap((_) => FlutterBlue.instance.connectedDevices),
82+
initialData: [],
83+
builder: (c, snapshot) => Column(
84+
children: snapshot.data
85+
.map((d) => ListTile(
86+
title: Text(d.name),
87+
subtitle: Text(d.id.toString()),
88+
trailing: StreamBuilder<BluetoothDeviceState>(
89+
stream: d.state,
90+
initialData:
91+
BluetoothDeviceState.disconnected,
92+
builder: (c, snapshot) {
93+
if (snapshot.data ==
94+
BluetoothDeviceState.connected) {
95+
return RaisedButton(
96+
child: Text('OPEN'),
97+
onPressed: () => Navigator.of(context)
98+
.push(MaterialPageRoute(
99+
builder: (context) =>
100+
DeviceScreen(device: d))),
101+
);
102+
}
103+
return Text(snapshot.data.toString());
104+
},
105+
),
106+
))
107+
.toList(),
108+
),
109+
),
110+
StreamBuilder<List<ScanResult>>(
111+
stream: FlutterBlue.instance.scanResults,
112+
initialData: [],
113+
builder: (c, snapshot) => Column(
114+
children: snapshot.data
115+
.map(
116+
(r) => ScanResultTile(
117+
result: r,
118+
onTap: () => Navigator.of(context).push(
119+
MaterialPageRoute(builder: (context) {
120+
r.device.connect();
121+
return DeviceScreen(device: r.device);
122+
})),
123+
),
124+
)
125+
.toList(),
126+
),
127+
),
128+
],
129+
),
93130
),
94131
),
95132
floatingActionButton: StreamBuilder<bool>(

ios/gen/Flutterblue.pbobjc.h

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ios/gen/Flutterblue.pbobjc.m

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/gen/flutterblue.pb.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,3 +997,26 @@ class DeviceStateResponse extends $pb.GeneratedMessage {
997997
void clearState() => clearField(2);
998998
}
999999

1000+
class ConnectedDevicesResponse extends $pb.GeneratedMessage {
1001+
static final $pb.BuilderInfo _i = new $pb.BuilderInfo('ConnectedDevicesResponse')
1002+
..pp<BluetoothDevice>(1, 'devices', $pb.PbFieldType.PM, BluetoothDevice.$checkItem, BluetoothDevice.create)
1003+
..hasRequiredFields = false
1004+
;
1005+
1006+
ConnectedDevicesResponse() : super();
1007+
ConnectedDevicesResponse.fromBuffer(List<int> i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) : super.fromBuffer(i, r);
1008+
ConnectedDevicesResponse.fromJson(String i, [$pb.ExtensionRegistry r = $pb.ExtensionRegistry.EMPTY]) : super.fromJson(i, r);
1009+
ConnectedDevicesResponse clone() => new ConnectedDevicesResponse()..mergeFromMessage(this);
1010+
ConnectedDevicesResponse copyWith(void Function(ConnectedDevicesResponse) updates) => super.copyWith((message) => updates(message as ConnectedDevicesResponse));
1011+
$pb.BuilderInfo get info_ => _i;
1012+
static ConnectedDevicesResponse create() => new ConnectedDevicesResponse();
1013+
static $pb.PbList<ConnectedDevicesResponse> createRepeated() => new $pb.PbList<ConnectedDevicesResponse>();
1014+
static ConnectedDevicesResponse getDefault() => _defaultInstance ??= create()..freeze();
1015+
static ConnectedDevicesResponse _defaultInstance;
1016+
static void $checkItem(ConnectedDevicesResponse v) {
1017+
if (v is! ConnectedDevicesResponse) $pb.checkItemFailed(v, _i.qualifiedMessageName);
1018+
}
1019+
1020+
List<BluetoothDevice> get devices => $_getList(0);
1021+
}
1022+

lib/gen/flutterblue.pbjson.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,10 @@ const DeviceStateResponse_BluetoothDeviceState$json = const {
300300
],
301301
};
302302

303+
const ConnectedDevicesResponse$json = const {
304+
'1': 'ConnectedDevicesResponse',
305+
'2': const [
306+
const {'1': 'devices', '3': 1, '4': 3, '5': 11, '6': '.BluetoothDevice', '10': 'devices'},
307+
],
308+
};
309+

lib/src/flutter_blue.dart

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ class FlutterBlue {
5757
.map((s) => BluetoothState.values[s.state.value]);
5858
}
5959

60+
Future<List<BluetoothDevice>> get connectedDevices {
61+
return _channel
62+
.invokeMethod('getConnectedDevices')
63+
.then((buffer) => protos.ConnectedDevicesResponse.fromBuffer(buffer))
64+
.then((p) => p.devices)
65+
.then((p) => p.map((d) => BluetoothDevice.fromProto(d)).toList());
66+
}
67+
6068
/// Starts a scan for Bluetooth Low Energy devices
6169
/// Timeout closes the stream after a specified [Duration]
6270
Stream<ScanResult> scan({
@@ -132,10 +140,10 @@ class FlutterBlue {
132140
/// The list of connected peripherals can include those that are connected
133141
/// by other apps and that will need to be connected locally using the
134142
/// device.connect() method before they can be used.
135-
Stream<List<BluetoothDevice>> connectedDevices({
136-
List<Guid> withServices = const [],
137-
}) =>
138-
throw UnimplementedError();
143+
// Stream<List<BluetoothDevice>> connectedDevices({
144+
// List<Guid> withServices = const [],
145+
// }) =>
146+
// throw UnimplementedError();
139147

140148
/// Sets the log level of the FlutterBlue instance
141149
/// Messages equal or below the log level specified are stored/forwarded,

protos/flutterblue.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,8 @@ message DeviceStateResponse {
196196
}
197197
string remote_id = 1;
198198
BluetoothDeviceState state = 2;
199+
}
200+
201+
message ConnectedDevicesResponse {
202+
repeated BluetoothDevice devices = 1;
199203
}

0 commit comments

Comments
 (0)