Skip to content

Commit

Permalink
Changed ScanResult to include the BluetoothDevice
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldemarco committed Sep 6, 2017
1 parent 0f34553 commit 19d8110
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 49 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.2.2
* BREAKING CHANGE: `ScanResult` now returns a `BluetoothDevice`

## 0.2.1
* BREAKING CHANGE: removed `stopScan` from API, use `scanSubscription.cancel()` instead
* Automatically stops scan when `startScan` subscription is canceled (thanks to @brianegan)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ scanSubscription.cancel();

### Connect to a device
```dart
BluetoothDevice device = await flutterBlue.connect(scanResult.identifier);
BluetoothDevice device = await flutterBlue.connect(scanResult.device.id);
```

### Discover services
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,15 +592,9 @@ private ScanCallback getScanCallback21() {
@Override
public void onScanResult(int callbackType, ScanResult result) {
super.onScanResult(callbackType, result);
Protos.ScanResult.Builder scanResult = Protos.ScanResult.newBuilder()
.setRemoteId(result.getDevice().getAddress())
.setName(result.getDevice().getName())
.setRssi(result.getRssi());
if(result.getScanRecord() != null) {
scanResult.setAdvertisementData(AdvertisementParser.parse(result.getScanRecord().getBytes()));
}
if(scanResultsSink != null) {
scanResultsSink.success(scanResult.build().toByteArray());
Protos.ScanResult scanResult = ProtoMaker.from(result.getDevice(), result.getScanRecord().getBytes(), result.getRssi());
scanResultsSink.success(scanResult.toByteArray());
}
}

Expand Down Expand Up @@ -637,13 +631,8 @@ private BluetoothAdapter.LeScanCallback getScanCallback18() {
@Override
public void onLeScan(final BluetoothDevice bluetoothDevice, int rssi,
byte[] scanRecord) {
Protos.ScanResult scanResult = Protos.ScanResult.newBuilder()
.setRemoteId(bluetoothDevice.getAddress())
.setName(bluetoothDevice.getName())
.setRssi(rssi)
.setAdvertisementData(AdvertisementParser.parse(scanRecord))
.build();
if(scanResultsSink != null) {
Protos.ScanResult scanResult = ProtoMaker.from(bluetoothDevice, scanRecord, rssi);
scanResultsSink.success(scanResult.toByteArray());
}
}
Expand Down
30 changes: 30 additions & 0 deletions android/src/main/java/com/pauldemarco/flutterblue/ProtoMaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,36 @@ public class ProtoMaker {

private static final UUID CCCD_UUID = UUID.fromString("000002902-0000-1000-8000-00805f9b34fb");

static Protos.ScanResult from(BluetoothDevice device, byte[] advertisementData, int rssi) {
Protos.ScanResult.Builder p = Protos.ScanResult.newBuilder();
p.setDevice(from(device));
if(advertisementData != null && advertisementData.length > 0)
p.setAdvertisementData(AdvertisementParser.parse(advertisementData));
p.setRssi(rssi);
return p.build();
}

static Protos.BluetoothDevice from(BluetoothDevice device) {
Protos.BluetoothDevice.Builder p = Protos.BluetoothDevice.newBuilder();
p.setRemoteId(device.getAddress());
p.setName(device.getName());
switch(device.getType()){
case BluetoothDevice.DEVICE_TYPE_LE:
p.setType(Protos.BluetoothDevice.Type.LE);
break;
case BluetoothDevice.DEVICE_TYPE_CLASSIC:
p.setType(Protos.BluetoothDevice.Type.CLASSIC);
break;
case BluetoothDevice.DEVICE_TYPE_DUAL:
p.setType(Protos.BluetoothDevice.Type.DUAL);
break;
default:
p.setType(Protos.BluetoothDevice.Type.UNKNOWN);
break;
}
return p.build();
}

static Protos.BluetoothService from(BluetoothDevice device, BluetoothGattService service, BluetoothGatt gatt) {
Protos.BluetoothService.Builder p = Protos.BluetoothService.newBuilder();
p.setRemoteId(device.getAddress());
Expand Down
8 changes: 4 additions & 4 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class _FlutterBlueAppState extends State<FlutterBlueApp> {
.startScan(timeout: const Duration(seconds: 5))
.listen((scanResult) {
setState(() {
scanResults[scanResult.identifier] = scanResult;
scanResults[scanResult.device.id] = scanResult;
});
}, onDone: _stopScan);

Expand All @@ -89,7 +89,7 @@ class _FlutterBlueAppState extends State<FlutterBlueApp> {

_connect(ScanResult r) async {
// Connect to device
BluetoothDevice d = await _flutterBlue.connect(r.identifier);
BluetoothDevice d = await _flutterBlue.connect(r.device.id);
setState(() {
device = d;
});
Expand Down Expand Up @@ -183,8 +183,8 @@ class _FlutterBlueAppState extends State<FlutterBlueApp> {
_buildScanResultTiles(BuildContext context) {
return scanResults.values
.map((s) => new ListTile(
title: new Text(s.name),
subtitle: new Text(s.identifier.toString()),
title: new Text(s.device.name),
subtitle: new Text(s.device.id.toString()),
leading: new Text(s.rssi.toString()),
onTap: () => _connect(s),
))
Expand Down
26 changes: 10 additions & 16 deletions lib/gen/flutterblue.pb.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,9 @@ class _ReadonlyScanSettings extends ScanSettings with ReadonlyMessageMixin {}

class ScanResult extends GeneratedMessage {
static final BuilderInfo _i = new BuilderInfo('ScanResult')
..a<String>(1, 'remoteId', PbFieldType.OS)
..a<String>(2, 'name', PbFieldType.OS)
..a<BluetoothDevice>(1, 'device', PbFieldType.OM, BluetoothDevice.getDefault, BluetoothDevice.create)
..a<AdvertisementData>(2, 'advertisementData', PbFieldType.OM, AdvertisementData.getDefault, AdvertisementData.create)
..a<int>(3, 'rssi', PbFieldType.O3)
..a<AdvertisementData>(4, 'advertisementData', PbFieldType.OM, AdvertisementData.getDefault, AdvertisementData.create)
..hasRequiredFields = false
;

Expand All @@ -189,25 +188,20 @@ class ScanResult extends GeneratedMessage {
if (v is! ScanResult) checkItemFailed(v, 'ScanResult');
}

String get remoteId => $_get(0, 1, '');
set remoteId(String v) { $_setString(0, 1, v); }
bool hasRemoteId() => $_has(0, 1);
void clearRemoteId() => clearField(1);
BluetoothDevice get device => $_get(0, 1, null);
set device(BluetoothDevice v) { setField(1, v); }
bool hasDevice() => $_has(0, 1);
void clearDevice() => clearField(1);

String get name => $_get(1, 2, '');
set name(String v) { $_setString(1, 2, v); }
bool hasName() => $_has(1, 2);
void clearName() => clearField(2);
AdvertisementData get advertisementData => $_get(1, 2, null);
set advertisementData(AdvertisementData v) { setField(2, v); }
bool hasAdvertisementData() => $_has(1, 2);
void clearAdvertisementData() => clearField(2);

int get rssi => $_get(2, 3, 0);
set rssi(int v) { $_setUnsignedInt32(2, 3, v); }
bool hasRssi() => $_has(2, 3);
void clearRssi() => clearField(3);

AdvertisementData get advertisementData => $_get(3, 4, null);
set advertisementData(AdvertisementData v) { setField(4, v); }
bool hasAdvertisementData() => $_has(3, 4);
void clearAdvertisementData() => clearField(4);
}

class _ReadonlyScanResult extends ScanResult with ReadonlyMessageMixin {}
Expand Down
5 changes: 2 additions & 3 deletions lib/gen/flutterblue.pbjson.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ const ScanSettings$json = const {
const ScanResult$json = const {
'1': 'ScanResult',
'2': const [
const {'1': 'remote_id', '3': 1, '4': 1, '5': 9, '10': 'remoteId'},
const {'1': 'name', '3': 2, '4': 1, '5': 9, '10': 'name'},
const {'1': 'device', '3': 1, '4': 1, '5': 11, '6': '.BluetoothDevice', '10': 'device'},
const {'1': 'advertisement_data', '3': 2, '4': 1, '5': 11, '6': '.AdvertisementData', '10': 'advertisementData'},
const {'1': 'rssi', '3': 3, '4': 1, '5': 5, '10': 'rssi'},
const {'1': 'advertisement_data', '3': 4, '4': 1, '5': 11, '6': '.AdvertisementData', '10': 'advertisementData'},
],
};

Expand Down
12 changes: 5 additions & 7 deletions lib/src/flutter_blue.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,14 @@ class DeviceIdentifier {

class ScanResult {
const ScanResult(
{this.name, this.identifier, this.advertisementData, this.rssi});
{this.device, this.advertisementData, this.rssi});

ScanResult.fromProto(protos.ScanResult p)
: name = p.name,
identifier = new DeviceIdentifier(p.remoteId),
rssi = p.rssi,
advertisementData = new AdvertisementData.fromProto(p.advertisementData);
: device = new BluetoothDevice.fromProto(p.device),
advertisementData = new AdvertisementData.fromProto(p.advertisementData),
rssi = p.rssi;

final String name;
final DeviceIdentifier identifier;
final BluetoothDevice device;
final AdvertisementData advertisementData;
final int rssi;
}
Expand Down
5 changes: 2 additions & 3 deletions protos/flutterblue.proto
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ message ScanSettings {
}

message ScanResult {
string remote_id = 1; // The received peer's ID.
string name = 2; // The peer's name.
BluetoothDevice device = 1; // The received peer's ID.
AdvertisementData advertisement_data = 2;
int32 rssi = 3;
AdvertisementData advertisement_data = 4;
}

message ConnectOptions {
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
description: Bluetooth plugin for Flutter
version: 0.2.1
version: 0.2.2
author: Paul DeMarco <paulmdemarco@gmail.com>
homepage: https://github.com/pauldemarco/flutter_blue

Expand Down

0 comments on commit 19d8110

Please sign in to comment.