@@ -69,6 +69,7 @@ public class FlutterBluePlugin implements MethodCallHandler, RequestPermissionsR
69
69
private final BluetoothManager mBluetoothManager ;
70
70
private BluetoothAdapter mBluetoothAdapter ;
71
71
private final Map <String , BluetoothGatt > mGattServers = new HashMap <>();
72
+ private LogLevel logLevel = LogLevel .EMERGENCY ;
72
73
73
74
// Pending call and result for startScan, in the case where permissions are needed
74
75
private MethodCall pendingCall ;
@@ -108,6 +109,14 @@ public void onMethodCall(MethodCall call, Result result) {
108
109
}
109
110
110
111
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
+
111
120
case "state" :
112
121
{
113
122
Protos .BluetoothState .Builder p = Protos .BluetoothState .newBuilder ();
@@ -744,13 +753,13 @@ public void onCancel(Object o) {
744
753
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback () {
745
754
@ Override
746
755
public void onConnectionStateChange (BluetoothGatt gatt , int status , int newState ) {
747
- Log . d ( TAG , "onConnectionStateChange: " + newState );
756
+ log ( LogLevel . DEBUG , "[ onConnectionStateChange] status: " + status + " newState : " + newState );
748
757
channel .invokeMethod ("DeviceState" , ProtoMaker .from (gatt .getDevice (), newState ).toByteArray ());
749
758
}
750
759
751
760
@ Override
752
761
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 );
754
763
if (servicesDiscoveredSink != null ) {
755
764
Protos .DiscoverServicesResult .Builder p = Protos .DiscoverServicesResult .newBuilder ();
756
765
p .setRemoteId (gatt .getDevice ().getAddress ());
@@ -763,7 +772,7 @@ public void onServicesDiscovered(BluetoothGatt gatt, int status) {
763
772
764
773
@ Override
765
774
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 );
767
776
if (characteristicReadSink != null ) {
768
777
Protos .ReadCharacteristicResponse .Builder p = Protos .ReadCharacteristicResponse .newBuilder ();
769
778
p .setRemoteId (gatt .getDevice ().getAddress ());
@@ -774,7 +783,7 @@ public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic
774
783
775
784
@ Override
776
785
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 );
778
787
Protos .WriteCharacteristicRequest .Builder request = Protos .WriteCharacteristicRequest .newBuilder ();
779
788
request .setRemoteId (gatt .getDevice ().getAddress ());
780
789
request .setCharacteristicUuid (characteristic .getUuid ().toString ());
@@ -787,7 +796,7 @@ public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristi
787
796
788
797
@ Override
789
798
public void onCharacteristicChanged (BluetoothGatt gatt , BluetoothGattCharacteristic characteristic ) {
790
- Log . d ( TAG , "onCharacteristicChanged: " + characteristic .getUuid ().toString ());
799
+ log ( LogLevel . DEBUG , "[ onCharacteristicChanged] uuid : " + characteristic .getUuid ().toString ());
791
800
Protos .OnNotificationResponse .Builder p = Protos .OnNotificationResponse .newBuilder ();
792
801
p .setRemoteId (gatt .getDevice ().getAddress ());
793
802
p .setCharacteristic (ProtoMaker .from (characteristic , gatt ));
@@ -796,7 +805,7 @@ public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteris
796
805
797
806
@ Override
798
807
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 );
800
809
if (descriptorReadSink != null ) {
801
810
// Rebuild the ReadAttributeRequest and send back along with response
802
811
Protos .ReadDescriptorRequest .Builder q = Protos .ReadDescriptorRequest .newBuilder ();
@@ -827,7 +836,7 @@ public void onDescriptorRead(BluetoothGatt gatt, BluetoothGattDescriptor descrip
827
836
828
837
@ Override
829
838
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 );
831
840
Protos .WriteDescriptorRequest .Builder request = Protos .WriteDescriptorRequest .newBuilder ();
832
841
request .setRemoteId (gatt .getDevice ().getAddress ());
833
842
request .setDescriptorUuid (descriptor .getUuid ().toString ());
@@ -849,18 +858,29 @@ public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descri
849
858
850
859
@ Override
851
860
public void onReliableWriteCompleted (BluetoothGatt gatt , int status ) {
852
- Log . d ( TAG , "onReliableWriteCompleted: " );
861
+ log ( LogLevel . DEBUG , "[ onReliableWriteCompleted] status : " + status );
853
862
}
854
863
855
864
@ Override
856
865
public void onReadRemoteRssi (BluetoothGatt gatt , int rssi , int status ) {
857
- Log . d ( TAG , "onReadRemoteRssi: " );
866
+ log ( LogLevel . DEBUG , "[ onReadRemoteRssi] rssi : " + rssi + " status: " + status );
858
867
}
859
868
860
869
@ Override
861
870
public void onMtuChanged (BluetoothGatt gatt , int mtu , int status ) {
862
- Log . d ( TAG , "onMtuChanged: " );
871
+ log ( LogLevel . DEBUG , "[ onMtuChanged] mtu : " + mtu + " status: " + status );
863
872
}
864
873
};
865
874
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
+
866
886
}
0 commit comments