Skip to content

Commit

Permalink
Moved guid to its own lib
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldemarco committed Aug 29, 2017
1 parent 25002f3 commit 480b3fc
Show file tree
Hide file tree
Showing 43 changed files with 211 additions and 375 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ android {
// Required for local unit tests (JUnit 4 framework)
testCompile 'junit:junit:4.12'

compile "com.polidea.rxandroidble:rxandroidble:1.3.2"
compile "com.polidea.rxandroidble:rxandroidble:1.3.3"
//compile files('/home/paul/flutter/bin/cache/artifacts/engine/android-arm/flutter.jar')
}
}
Expand Down
2 changes: 1 addition & 1 deletion android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
android:versionCode="1"
android:versionName="0.0.1">

<uses-sdk android:minSdkVersion="18" android:targetSdkVersion="21" />
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.BLUETOOTH" />
</manifest>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.pauldemarco.flutterblue.concrete;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.util.Log;

import com.pauldemarco.flutterblue.Adapter;
Expand All @@ -14,10 +15,7 @@
import com.polidea.rxandroidble.scan.ScanResult;
import com.polidea.rxandroidble.scan.ScanSettings;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;

import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodCall;
Expand Down Expand Up @@ -137,7 +135,12 @@ public Completable connectToKnownDevice(Guid deviceGuid) {

@Override
public List<Device> getSystemConnectedOrPairedDevices(Set<Guid> services) {
return null;
List<Device> devices = new ArrayList<>();
for(RxBleDevice d : rxBleClient.getBondedDevices()) {
Device device = new DeviceImpl(registrar, Guid.fromMac(d.getMacAddress()), d.getName(), d, 0, new byte[]{});
devices.add(device);
}
return devices;
}

@Override
Expand Down Expand Up @@ -192,6 +195,13 @@ public void onMethodCall(MethodCall call, Result result) {
} else {
result.success("Device not found in Set");
}
} else if(call.method.equals("getSystemConnectedOrPairedDevices")) {
List<Device> devices = getSystemConnectedOrPairedDevices(null);
List<Map> mapsOfDevices = new ArrayList<>();
for(Device d : devices) {
mapsOfDevices.add(d.toMap());
}
result.success(mapsOfDevices);
} else {
result.notImplemented();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public Single<byte[]> read() {
public Completable write(byte[] data) {
return connectionObservable
.flatMap(rxBleConnection -> rxBleConnection.writeCharacteristic(guid.toUUID(), data))
.doOnNext(valueStream::onNext)
//.doOnNext(valueStream::onNext)
.toCompletable();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.pauldemarco.flutterblue.concrete;

import android.bluetooth.BluetoothDevice;
import android.util.Log;

import com.pauldemarco.flutterblue.ChannelPaths;
Expand Down Expand Up @@ -166,7 +167,7 @@ public Single<Service> getService(Guid id) {
.map(RxBleDeviceServices::getBluetoothGattServices)
.flatMapIterable(services -> services)
.map(service -> (Service)new ServiceImpl(registrar, service, this, connectionObservable))
.filter(service -> service.getGuid() == id)
.filter(service -> service.getGuid().equals(id))
.toSingle();

} else {
Expand Down Expand Up @@ -253,6 +254,18 @@ public void onMethodCall(MethodCall call, Result result) {
);
} else if (call.method.equals("getState")) {
result.success(toState(nativeDevice.getConnectionState()).ordinal());
} else if (call.method.equals("createBond")) {
if(nativeDevice.getBluetoothDevice().createBond()) {
result.success(true);
} else {
result.error("CREATE_BOND_STATE FAILED", "", "");
}
} else if (call.method.equals("isBonded")) {
if(nativeDevice.getBluetoothDevice().getBondState() == BluetoothDevice.BOND_BONDED) {
result.success(true);
} else {
result.success(false);
}
} else {
result.notImplemented();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public ServiceImpl(Registrar registrar, Guid guid, Device device, boolean isPrim
super(guid, isPrimary, device);
this.registrar = registrar;
this.methodChannel = new MethodChannel(registrar.messenger(), ChannelPaths.getServiceMethodsPath(device.getGuid().toString(), guid.toString()));
this.methodChannel.setMethodCallHandler(this);
this.connectionObservable = connectionObservable;
}

Expand Down Expand Up @@ -109,6 +110,13 @@ public void onMethodCall(MethodCall call, Result result) {
} else if (call.method.equals("getCharacteristic")) {
String id = (String)call.arguments;
Guid guid = new Guid(id);
for(Characteristic c : characteristics) {
if(guid.equals(c.getGuid())) {
result.success(c.toMap());
return;
}
}
result.error("GET_CHARACTERISTIC_ERROR", "Characteristic not found with id " + guid.toString(), null);
} else {
result.notImplemented();
}
Expand Down
2 changes: 1 addition & 1 deletion example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
android:versionCode="1"
android:versionName="0.0.1">

<uses-sdk android:minSdkVersion="18" android:targetSdkVersion="21" />
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="21" />

<!-- The INTERNET permission is required for development. Specifically,
flutter needs it to communicate with the running application
Expand Down
41 changes: 26 additions & 15 deletions example/lib/app_strings.dart
Original file line number Diff line number Diff line change
@@ -1,42 +1,53 @@
import 'dart:async';

import 'package:flutter_blue_example/i18n/app_messages_all.dart';
import 'package:intl/intl.dart';
import 'package:flutter/widgets.dart';

// Wrappers for strings that are shown in the UI. The strings can be
// translated for different locales using the Dart intl package.
//
// Locale-specific values for the strings live in the i18n/*.arb files.
//
// To generate the app_messages_*.dart files from the ARB files, run:
// flutter packages pub run intl_translation:generate_from_arb --output-dir=lib/i18n --generated-file-prefix=app_ --no-use-deferred-loading lib/*.dart lib/i18n/*.arb
// Information about how this file relates to i18n/stock_messages_all.dart and how the i18n files
// were generated can be found in i18n/regenerate.md.

class AppStrings extends LocaleQueryData {
static AppStrings of(BuildContext context) {
return LocaleQuery.of(context);
class AppStrings {
AppStrings(Locale locale) : _localeName = locale.toString();

final String _localeName;

static Future<AppStrings> load(Locale locale) {
return initializeMessages(locale.toString())
.then((Null _) {
return new AppStrings(locale);
});
}

static final AppStrings instance = new AppStrings();
static AppStrings of(BuildContext context) {
return Localizations.of<AppStrings>(context, AppStrings);
}

String title() => Intl.message(
'Stocks',
name: 'title',
desc: 'Title for the Stocks application'
desc: 'Title for the Stocks application',
locale: _localeName,
);

String devices() => Intl.message(
'Devices',
name: 'devices',
desc: 'Title for the Devices application'
desc: 'Title for the Devices application',
locale: _localeName,
);

String scanner() => Intl.message(
'SCANNER',
name: 'scanner',
desc: 'Label for the Scanner tab'
desc: 'Label for the Scanner tab',
locale: _localeName,
);

String bonded() => Intl.message(
'BONDED',
name: 'bonded',
desc: 'Label for the Bonded tab'
desc: 'Label for the Bonded tab',
locale: _localeName,
);
}
21 changes: 15 additions & 6 deletions example/lib/characteristic_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ class CharacteristicTile extends StatefulWidget {
}

class _CharacteristicTileState extends State<CharacteristicTile> {
String id;
String title;
Uint8List value = new Uint8List(1);
bool isUpdating = false;
StreamSubscription valueStream;

@override
void initState() {
id = widget.characteristic.id.toString();
title = widget.characteristic.name;
widget.characteristic.getDescriptors().then((d) => print('Descriptor List! $d'));
valueStream = widget.characteristic.valueUpdated()
.listen((d) {
setState(() {
Expand Down Expand Up @@ -108,12 +111,18 @@ class _CharacteristicTileState extends State<CharacteristicTile> {
@override
Widget build(BuildContext context) {
return new ListTile(
title: new Text(title),
subtitle: new Text(value.toString()),
onTap: _startUpdates,
trailing: new Row(
children: _buildIconsList()
),
dense: true,
title: new Text(title),
subtitle: new Column(
children: <Widget>[
new Text(id),
new Text(value.toString())
],
),
onTap: _startUpdates,
trailing: new Row(
children: _buildIconsList()
),
);
}
}
3 changes: 2 additions & 1 deletion example/lib/i18n/app_messages_all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import 'package:intl/src/intl_helpers.dart';
import 'app_messages_en.dart' as messages_en;
import 'app_messages_es.dart' as messages_es;

Map<String, Function> _deferredLibraries = {
typedef Future<dynamic> LibraryLoader();
Map<String, LibraryLoader> _deferredLibraries = {
'en': () => new Future.value(null),
'es': () => new Future.value(null),
};
Expand Down
5 changes: 4 additions & 1 deletion example/lib/i18n/app_messages_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class MessageLookup extends MessageLookupByLibrary {

final messages = _notInlinedMessages(_notInlinedMessages);
static _notInlinedMessages(_) => {

"bonded" : MessageLookupByLibrary.simpleMessage("BONDED"),
"devices" : MessageLookupByLibrary.simpleMessage("Devices"),
"scanner" : MessageLookupByLibrary.simpleMessage("SCANNER"),
"title" : MessageLookupByLibrary.simpleMessage("FlutterBlue")
};
}
5 changes: 4 additions & 1 deletion example/lib/i18n/app_messages_es.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class MessageLookup extends MessageLookupByLibrary {

final messages = _notInlinedMessages(_notInlinedMessages);
static _notInlinedMessages(_) => {

"bonded" : MessageLookupByLibrary.simpleMessage("GARANTIZADO"),
"devices" : MessageLookupByLibrary.simpleMessage("Dispositivos"),
"scanner" : MessageLookupByLibrary.simpleMessage("ESCANER"),
"title" : MessageLookupByLibrary.simpleMessage("FlutterBlue")
};
}
1 change: 1 addition & 0 deletions example/lib/i18n/intl_messages.arb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"title":"Stocks","@title":{"description":"Title for the Stocks application","type":"text","placeholders":{}},"devices":"Devices","@devices":{"description":"Title for the Devices application","type":"text","placeholders":{}},"scanner":"SCANNER","@scanner":{"description":"Label for the Scanner tab","type":"text","placeholders":{}},"bonded":"BONDED","@bonded":{"description":"Label for the Bonded tab","type":"text","placeholders":{}}}
32 changes: 30 additions & 2 deletions example/lib/i18n/regenerate.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
To rebuild the i18n files:
## Regenerating the i18n files

The files in this directory are based on ../lib/stock_strings.dart
which defines all of the localizable strings used by the stocks
app. The stocks app uses
the [Dart `intl` package](https://github.com/dart-lang/intl).

Rebuilding everything requires two steps.

With the `examples/stocks` as the current directory, generate
`intl_messages.arb` from `lib/stock_strings.dart`:
```
flutter packages pub run intl_translation:generate_from_arb --output-dir=lib/i18n --generated-file-prefix=app_ --no-use-deferred-loading lib/*.dart lib/i18n/*.arb
flutter pub pub run intl_translation:extract_to_arb --output-dir=lib/i18n lib/stock_strings.dart
```
The `intl_messages.arb` file is a JSON format map with one entry for
each `Intl.message()` function defined in `stock_strings.dart`. This
file was used to create the English and Spanish localizations,
`stocks_en.arb` and `stocks_es.arb`. The `intl_messages.arb` wasn't
checked into the repository, since it only serves as a template for
the other `.arb` files.


With the `examples/stocks` as the current directory, generate a
`stock_messages_<locale>.dart` for each `stocks_<locale>.arb` file and
`stock_messages_all.dart`, which imports all of the messages files:
```
flutter pub pub run intl_translation:generate_from_arb --output-dir=lib/i18n \
--generated-file-prefix=stock_ --no-use-deferred-loading lib/*.dart lib/i18n/stocks_*.arb
```

The `StockStrings` class uses the generated `initializeMessages()`
function (`stock_messages_all.dart`) to load the localized messages
and `Intl.message()` to look them up.
21 changes: 11 additions & 10 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import 'package:flutter_blue_example/app_home.dart';
import 'package:flutter_blue_example/app_settings.dart';
import 'package:flutter_blue_example/app_strings.dart';
import 'package:flutter_blue_example/device_page.dart';
import 'package:intl/intl.dart';
import 'i18n/app_messages_all.dart';

class FlutterBlueApp extends StatefulWidget {
@override
Expand Down Expand Up @@ -92,13 +90,6 @@ class FlutterBlueAppState extends State<FlutterBlueApp> {
return null;
}

Future<LocaleQueryData> _onLocaleChanged(Locale locale) async {
final String localeString = locale.toString();
await initializeMessages(localeString);
Intl.defaultLocale = localeString;
return AppStrings.instance;
}

@override
Widget build(BuildContext context) {
assert(() {
Expand All @@ -112,6 +103,9 @@ class FlutterBlueAppState extends State<FlutterBlueApp> {
return new MaterialApp(
title: 'FlutterBlue',
theme: theme,
localizationsDelegates: <_AppLocalizationsDelegate>[
new _AppLocalizationsDelegate(),
],
debugShowMaterialGrid: _configuration.debugShowGrid,
showPerformanceOverlay: _configuration.showPerformanceOverlay,
showSemanticsDebugger: _configuration.showSemanticsDebugger,
Expand All @@ -120,11 +114,18 @@ class FlutterBlueAppState extends State<FlutterBlueApp> {
'/settings': (BuildContext context) => new AppSettings(_configuration, configurationUpdater)
},
onGenerateRoute: _getRoute,
onLocaleChanged: _onLocaleChanged
);
}
}

class _AppLocalizationsDelegate extends LocalizationsDelegate<AppStrings> {
@override
Future<AppStrings> load(Locale locale) => AppStrings.load(locale);

@override
bool shouldReload(_AppLocalizationsDelegate old) => false;
}

void main() {
runApp(new FlutterBlueApp());
}
2 changes: 1 addition & 1 deletion example/lib/scan_devices_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:flutter_blue/abstractions/contracts/i_device.dart';
import 'package:flutter_blue/abstractions/device_state.dart';
import 'package:flutter_blue/concrete/device.dart';
import 'package:flutter_blue/flutter_blue.dart';
import 'package:flutter_blue/utils/guid.dart';
import 'package:guid/guid.dart';

import 'device_tile.dart';

Expand Down
Loading

0 comments on commit 480b3fc

Please sign in to comment.