Skip to content

Commit 09fa011

Browse files
committed
[Dart] [Android] Standardize logging across plugin, iOS still needed. #45
1 parent 8bc647c commit 09fa011

File tree

3 files changed

+65
-13
lines changed

3 files changed

+65
-13
lines changed

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

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public class FlutterBluePlugin implements MethodCallHandler, RequestPermissionsR
6969
private final BluetoothManager mBluetoothManager;
7070
private BluetoothAdapter mBluetoothAdapter;
7171
private final Map<String, BluetoothGatt> mGattServers = new HashMap<>();
72+
private LogLevel logLevel = LogLevel.EMERGENCY;
7273

7374
// Pending call and result for startScan, in the case where permissions are needed
7475
private MethodCall pendingCall;
@@ -108,6 +109,14 @@ public void onMethodCall(MethodCall call, Result result) {
108109
}
109110

110111
switch (call.method) {
112+
case "setLogLevel":
113+
{
114+
int logLevelIndex = (int)call.arguments;
115+
logLevel = LogLevel.values()[logLevelIndex];
116+
result.success(null);
117+
break;
118+
}
119+
111120
case "state":
112121
{
113122
Protos.BluetoothState.Builder p = Protos.BluetoothState.newBuilder();
@@ -744,13 +753,13 @@ public void onCancel(Object o) {
744753
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
745754
@Override
746755
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
747-
Log.d(TAG, "onConnectionStateChange: " + newState);
756+
log(LogLevel.DEBUG, "[onConnectionStateChange] status: " + status + " newState: " + newState);
748757
channel.invokeMethod("DeviceState", ProtoMaker.from(gatt.getDevice(), newState).toByteArray());
749758
}
750759

751760
@Override
752761
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
753-
Log.d(TAG, "onServicesDiscovered: " + gatt.getServices().size() + " sink:" + servicesDiscoveredSink);
762+
log(LogLevel.DEBUG, "[onServicesDiscovered] count: " + gatt.getServices().size() + " status: " + status);
754763
if(servicesDiscoveredSink != null) {
755764
Protos.DiscoverServicesResult.Builder p = Protos.DiscoverServicesResult.newBuilder();
756765
p.setRemoteId(gatt.getDevice().getAddress());
@@ -763,7 +772,7 @@ public void onServicesDiscovered(BluetoothGatt gatt, int status) {
763772

764773
@Override
765774
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
766-
Log.d(TAG, "onCharacteristicRead: ");
775+
log(LogLevel.DEBUG, "[onCharacteristicRead] uuid: " + characteristic.getUuid().toString() + " status: " + status);
767776
if(characteristicReadSink != null) {
768777
Protos.ReadCharacteristicResponse.Builder p = Protos.ReadCharacteristicResponse.newBuilder();
769778
p.setRemoteId(gatt.getDevice().getAddress());
@@ -774,7 +783,7 @@ public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic
774783

775784
@Override
776785
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
777-
Log.d(TAG, "onCharacteristicWrite: ");
786+
log(LogLevel.DEBUG, "[onCharacteristicWrite] uuid: " + characteristic.getUuid().toString() + " status: " + status);
778787
Protos.WriteCharacteristicRequest.Builder request = Protos.WriteCharacteristicRequest.newBuilder();
779788
request.setRemoteId(gatt.getDevice().getAddress());
780789
request.setCharacteristicUuid(characteristic.getUuid().toString());
@@ -787,7 +796,7 @@ public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristi
787796

788797
@Override
789798
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
790-
Log.d(TAG, "onCharacteristicChanged: " + characteristic.getUuid().toString());
799+
log(LogLevel.DEBUG, "[onCharacteristicChanged] uuid: " + characteristic.getUuid().toString());
791800
Protos.OnNotificationResponse.Builder p = Protos.OnNotificationResponse.newBuilder();
792801
p.setRemoteId(gatt.getDevice().getAddress());
793802
p.setCharacteristic(ProtoMaker.from(characteristic, gatt));
@@ -796,7 +805,7 @@ public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteris
796805

797806
@Override
798807
public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
799-
Log.d(TAG, "onDescriptorRead: " + descriptor.getUuid().toString() + " status: " + status);
808+
log(LogLevel.DEBUG, "[onDescriptorRead] uuid: " + descriptor.getUuid().toString() + " status: " + status);
800809
if(descriptorReadSink != null) {
801810
// Rebuild the ReadAttributeRequest and send back along with response
802811
Protos.ReadDescriptorRequest.Builder q = Protos.ReadDescriptorRequest.newBuilder();
@@ -827,7 +836,7 @@ public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descrip
827836

828837
@Override
829838
public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
830-
Log.d(TAG, "onDescriptorWrite: " + descriptor.getUuid().toString() + " status: " + status);
839+
log(LogLevel.DEBUG, "[onDescriptorWrite] uuid: " + descriptor.getUuid().toString() + " status: " + status);
831840
Protos.WriteDescriptorRequest.Builder request = Protos.WriteDescriptorRequest.newBuilder();
832841
request.setRemoteId(gatt.getDevice().getAddress());
833842
request.setDescriptorUuid(descriptor.getUuid().toString());
@@ -849,18 +858,29 @@ public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descri
849858

850859
@Override
851860
public void onReliableWriteCompleted(BluetoothGatt gatt, int status) {
852-
Log.d(TAG, "onReliableWriteCompleted: ");
861+
log(LogLevel.DEBUG, "[onReliableWriteCompleted] status: " + status);
853862
}
854863

855864
@Override
856865
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
857-
Log.d(TAG, "onReadRemoteRssi: ");
866+
log(LogLevel.DEBUG, "[onReadRemoteRssi] rssi: " + rssi + " status: " + status);
858867
}
859868

860869
@Override
861870
public void onMtuChanged(BluetoothGatt gatt, int mtu, int status) {
862-
Log.d(TAG, "onMtuChanged: ");
871+
log(LogLevel.DEBUG, "[onMtuChanged] mtu: " + mtu + " status: " + status);
863872
}
864873
};
865874

875+
enum LogLevel
876+
{
877+
EMERGENCY, ALERT, CRITICAL, ERROR, WARNING, NOTICE, INFO, DEBUG;
878+
}
879+
880+
private void log(LogLevel level, String message) {
881+
if(level.ordinal() <= logLevel.ordinal()) {
882+
Log.d(TAG, message);
883+
}
884+
}
885+
866886
}

lib/src/bluetooth_device.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ class BluetoothDevice {
5050
..remoteId = id.toString()
5151
..characteristicUuid = characteristic.uuid.toString()
5252
..serviceUuid = characteristic.serviceUuid.toString();
53-
54-
print(
53+
FlutterBlue.instance._log(LogLevel.info,
5554
'remoteId: ${id.toString()} characteristicUuid: ${characteristic.uuid.toString()} serviceUuid: ${characteristic.serviceUuid.toString()}');
5655

5756
await FlutterBlue.instance._channel

lib/src/flutter_blue.dart

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,17 @@ class FlutterBlue {
2525
_channel.setMethodCallHandler((MethodCall call) {
2626
_methodStreamController.add(call);
2727
});
28+
29+
// Send the log level to the underlying platforms.
30+
setLogLevel(logLevel);
2831
}
2932
static FlutterBlue _instance = new FlutterBlue._();
3033
static FlutterBlue get instance => _instance;
3134

35+
/// Log level of the instance, default is all messages (debug).
36+
LogLevel _logLevel = LogLevel.debug;
37+
LogLevel get logLevel => _logLevel;
38+
3239
/// Checks whether the device supports Bluetooth
3340
Future<bool> get isAvailable =>
3441
_channel.invokeMethod('isAvailable').then<bool>((d) => d);
@@ -126,7 +133,7 @@ class FlutterBlue {
126133
subscription = device.onStateChanged().listen(
127134
(data) {
128135
if (data == BluetoothDeviceState.connected) {
129-
print('connected!');
136+
_log(LogLevel.info, 'connected!');
130137
connected = true;
131138
}
132139
controller.add(data);
@@ -141,6 +148,32 @@ class FlutterBlue {
141148
/// Cancels connection to the Bluetooth Device
142149
Future _cancelConnection(BluetoothDevice device) =>
143150
_channel.invokeMethod('disconnect', device.id.toString());
151+
152+
/// Sets the log level of the FlutterBlue instance
153+
/// Messages equal or below the log level specified are stored/forwarded,
154+
/// messages above are dropped.
155+
void setLogLevel(LogLevel level) async {
156+
await _channel.invokeMethod('setLogLevel', level.index);
157+
_logLevel = level;
158+
}
159+
160+
void _log(LogLevel level, String message) {
161+
if (level.index <= _logLevel.index) {
162+
print(message);
163+
}
164+
}
165+
}
166+
167+
/// Log levels for FlutterBlue
168+
enum LogLevel {
169+
emergency,
170+
alert,
171+
critical,
172+
error,
173+
warning,
174+
notice,
175+
info,
176+
debug,
144177
}
145178

146179
/// State of the bluetooth adapter.

0 commit comments

Comments
 (0)