You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: app/src/main/java/com/bluetooth/communicator/BluetoothCommunicator.java
+56-25Lines changed: 56 additions & 25 deletions
Original file line number
Diff line number
Diff line change
@@ -49,19 +49,21 @@
49
49
* It also automatically implements (they are active by default) reconnection in case of temporary connection loss, reliable message sending,
50
50
* splitting and rebuilding of long messages, sending raw data in addition to text messages and a message queue in order to always send the messages (and always in the right order)
51
51
* even in case of connection problems (they will be sent as soon as the connection is restored)
52
-
*
52
+
* <br /><br />
53
53
* First create a bluetooth communicator object, it is the object that handles all operations of bluetooth low energy library, if you want to manage
54
54
* the bluetooth connections in multiple activities I suggest you to save this object as an attribute of a custom class that extends Application and
55
55
* create a getter so you can access to bluetoothCommunicator from any activity or service with:
56
-
*
56
+
* <pre>{@code
57
57
* ((custom class name) getApplication()).getBluetoothCommunicator();
58
+
* }</pre>
58
59
* Next step is to initialize bluetoothCommunicator, the parameters are: a context, the name by which the other devices will see us (limited to 18 characters
59
60
* and can be only characters listed in BluetoothTools.getSupportedUTFCharacters(context) because the number of bytes for advertising beacon is limited) and the strategy
60
61
* (for now the only supported strategy is BluetoothCommunicator.STRATEGY_P2P_WITH_RECONNECTION)
61
-
*
62
+
* <pre>{@code
62
63
* bluetoothCommunicator = new BluetoothCommunicator(this, "device name", BluetoothCommunicator.STRATEGY_P2P_WITH_RECONNECTION);
64
+
* }</pre>
63
65
* Then add the bluetooth communicator callback, the callback will listen for all events of bluetooth communicator:
@@ -278,6 +284,7 @@ public class BluetoothCommunicator {
278
284
279
285
/**
280
286
* Constructor of BluetoothCommunicator
287
+
*
281
288
* @param context
282
289
* @param name the name by which the other devices will see us (limited to 18 characters
283
290
* and can be only characters listed in BluetoothTools.getSupportedUTFCharacters(context) because the number of bytes for advertising beacon is limited)
@@ -429,7 +436,8 @@ public void onScanFailed(int errorCode) {
429
436
430
437
/**
431
438
* Constructor of BluetoothCommunicator that adds a callback (it can also be added with addCallback
432
-
* @param context
439
+
*
440
+
* @param context context
433
441
* @param name the name by which the other devices will see us (limited to 18 characters
434
442
* and can be only characters listed in BluetoothTools.getSupportedUTFCharacters(context) because the number of bytes for advertising beacon is limited)
435
443
* @param strategy (for now the only supported strategy is BluetoothCommunicator.STRATEGY_P2P_WITH_RECONNECTION)
@@ -572,7 +580,7 @@ public void onDisconnectionFailed() {
572
580
/**
573
581
* This method start advertising, so this device can be discovered by other devices that use this library and can receive a connection request,
574
582
* this method will eventually turn on bluetooth if it is off (BluetoothCommunicator will restore bluetooth status when both advertising and discovery are stopped)
575
-
*
583
+
* <br /><br />
576
584
* This method must always be done from the main thread or it will return NOT_MAIN_THREAD without doing anything.
577
585
*
578
586
* @return SUCCESS if everithing is OK, ALREADY_STARTED if BluetoothCommunicator is already advertising, NOT_MAIN_THREAD if this method is not called
@@ -650,7 +658,7 @@ private int executeStartAdvertising() {
650
658
651
659
/**
652
660
* This method stop advertising, this method will eventually turn off bluetooth if BluetoothCommunicator turned on before and if discovery is off
653
-
*
661
+
* <br /><br />
654
662
* This method must always be done from the main thread or it will return NOT_MAIN_THREAD without doing anything.
655
663
*
656
664
* @return SUCCESS if everithing is OK, ALREADY_STOPPED if BluetoothCommunicator is not advertising, NOT_MAIN_THREAD if this method is not called
@@ -712,6 +720,7 @@ private int executeStopAdvertising() {
712
720
713
721
/**
714
722
* This method will check if BluetoothLe is supported by the device (rarely it can indicate a general bluetooth error, not an incompatibility with bluetooth le)
723
+
*
715
724
* @return SUCCESS if bluetooth le is supported, BLUETOOTH_LE_NOT_SUPPORTED if not (or rarely if we had a generic bluetooth problem)
716
725
*/
717
726
publicintisBluetoothLeSupported() {
@@ -725,7 +734,7 @@ public int isBluetoothLeSupported() {
725
734
/**
726
735
* This method start discovery so BluetoothCommunicator can discover advertising devices and notify them with onPeerFound,
727
736
* this method will eventually turn on bluetooth if it is off (BluetoothCommunicator will restore bluetooth status when both advertising and discovery are stopped)
728
-
*
737
+
* <br /><br />
729
738
* This method must always be done from the main thread or it will return NOT_MAIN_THREAD without doing anything.
730
739
*
731
740
* @return SUCCESS if everithing is OK, ALREADY_STARTED if BluetoothCommunicator is already advertising, NOT_MAIN_THREAD if this method is not called
@@ -799,7 +808,7 @@ private int executeStartDiscovery() {
799
808
800
809
/**
801
810
* This method stop discovery, this method will eventually turn off bluetooth if BluetoothCommunicator turned on before and if advertising is off
802
-
*
811
+
* <br /><br />
803
812
* This method must always be done from the main thread or it will return NOT_MAIN_THREAD without doing anything.
804
813
*
805
814
* @return SUCCESS if everithing is OK, ALREADY_STOPPED if BluetoothCommunicator is not discovering, NOT_MAIN_THREAD if this method is not called
@@ -858,6 +867,7 @@ private int executeStopDiscovery() {
858
867
/**
859
868
* This method will send the text contained in message to the peer contained in the receiver attribute of the message (only if that peer is connected), if receiver is not set
860
869
* the message will be sent to all the connected peers
870
+
*
861
871
* @param message message to be sent
862
872
*/
863
873
publicvoidsendMessage(finalMessagemessage) {
@@ -897,6 +907,7 @@ public void onMessageSent() { // means that we have sent the message to all th
897
907
/**
898
908
* This method will send the data contained in message to the peer contained in the receiver attribute of the message (only if that peer is connected), if receiver is not set
899
909
* the message will be sent to all the connected peers
910
+
*
900
911
* @param data message to be sent
901
912
*/
902
913
publicvoidsendData(finalMessagedata) {
@@ -937,9 +948,10 @@ public void onMessageSent() { // means that we have sent the message to all th
937
948
* this method set the name with which you would be seen by other devices, the name must be limited to 18 characters
938
949
* and can be only characters listed in BluetoothTools.getSupportedUTFCharacters(context) because the number of bytes for advertising beacon is limited,
939
950
* there is no controls for this so if you not follow these restrictions BluetoothCommunicator will not work correctly.
940
-
*
951
+
* <br /><br />
941
952
* To the name will be added 4 random symbols in a completely transparent way (this 4 symbols will exixts only inside BluetoothCommunicator, which removes them before the name gets on the outside),
942
953
* this allows to have a unique identification (for BluetoothCommunicator, not for the user) even for peers with the same name
954
+
*
943
955
* @param name
944
956
* @return SUCCESS if bluetooth le is supported by the device or BLUETOOTH_LE_NOT_SUPPORTED if not (or rarely if we had a generic bluetooth problem)
945
957
*/
@@ -981,6 +993,7 @@ public ArrayList<Peer> getConnectedPeersList() {
981
993
/**
982
994
* This method must be used after you have received a connection request to accept it and complete the connection (the connection is complete when
983
995
* onConnectionSuccess is called)
996
+
*
984
997
* @param peer the peer that has sent the connection request that we want to accept
985
998
* @return SUCCESS if bluetooth le is supported by the device or BLUETOOTH_LE_NOT_SUPPORTED if not (or rarely if we had a generic bluetooth problem)
986
999
*/
@@ -994,7 +1007,8 @@ public int acceptConnection(Peer peer) {
994
1007
}
995
1008
996
1009
/**
997
-
* This method must be used after you have received a connection request to reject it and cancel the connection
1010
+
* This method must be used after you have received a connection request to reject it and cancel the connection.
1011
+
*
998
1012
* @param peer the peer that has sent the connection request that you want to accept
999
1013
* @return SUCCESS if bluetooth le is supported by the device or BLUETOOTH_LE_NOT_SUPPORTED if not (or rarely if we had a generic bluetooth problem)
1000
1014
*/
@@ -1009,6 +1023,7 @@ public int rejectConnection(Peer peer) {
1009
1023
1010
1024
/**
1011
1025
* This method is used to know if BluetoothCommunicator is advertising
1026
+
*
1012
1027
* @return advertising
1013
1028
*/
1014
1029
publicbooleanisAdvertising() {
@@ -1026,7 +1041,8 @@ public boolean isDiscovering() {
1026
1041
/**
1027
1042
* This method must be used to send a connection request to a founded peer, successfully you can know if it has accepted or rejected the connection listening
1028
1043
* onConnectionSuccess and onConnectionFailed
1029
-
* @param peer
1044
+
*
1045
+
* @param peer found peer you want to connect with
1030
1046
* @return SUCCESS if everything is gone OK, BLUETOOTH_LE_NOT_SUPPORTED if bluetooth le is not supported (or rarely if we had a generic bluetooth problem)
1031
1047
* or DESTROYING if destroy() is called before
1032
1048
*/
@@ -1055,6 +1071,7 @@ public int readPhy(Peer peer) {
1055
1071
1056
1072
/**
1057
1073
* This method return the bluetooth adapter used by BluetoothCommunicator
1074
+
*
1058
1075
* @return bluetoothAdapter
1059
1076
*/
1060
1077
publicBluetoothAdaptergetBluetoothAdapter() {
@@ -1066,8 +1083,9 @@ public BluetoothAdapter getBluetoothAdapter() {
1066
1083
* when onDisconnected is called with that peer as argument, in case the disconnection fails onDisconnectionFailed is called but if you not
1067
1084
* override it or leave the call to super BluetoothCommunicator will turn off and on bluetooth to force the disconnection and onDisconnection will be
1068
1085
* called.
1069
-
* @param peer
1070
-
* @return
1086
+
*
1087
+
* @param peer connected peer you want to disconnect from
1088
+
* @return SUCCESS if bluetooth le is supported by the device or BLUETOOTH_LE_NOT_SUPPORTED if not (or rarely if we had a generic bluetooth problem)
1071
1089
*/
1072
1090
publicintdisconnect(finalPeerpeer) {
1073
1091
synchronized (bluetoothLock) {
@@ -1089,6 +1107,7 @@ public int disconnect(final Peer peer) {
1089
1107
1090
1108
/**
1091
1109
* This method will call disconnect for all the connected peers
1110
+
*
1092
1111
* @return SUCCESS if bluetooth le is supported by the device or BLUETOOTH_LE_NOT_SUPPORTED if not (or rarely if we had a generic bluetooth problem)
1093
1112
*/
1094
1113
publicintdisconnectFromAll() {
@@ -1124,10 +1143,19 @@ public interface DestroyCallback {
1124
1143
}
1125
1144
1126
1145
1146
+
/**
1147
+
* With this method you can add the callback for listening all the events of BluetoothCommunicator
1148
+
*
1149
+
* @param callback callback for listening all the events of BluetoothCommunicator
1150
+
*/
1127
1151
publicvoidaddCallback(Callbackcallback) {
1128
1152
clientCallbacks.add(callback);
1129
1153
}
1130
1154
1155
+
/**
1156
+
* This remote will remove the callback you pass as argument from the list of callback of this class, so this callback will not receive method call anymore
1157
+
* @param callback callback for listening all the events of BluetoothCommunicator
1158
+
*/
1131
1159
publicvoidremoveCallback(Callbackcallback) {
1132
1160
clientCallbacks.remove(callback);
1133
1161
}
@@ -1320,28 +1348,31 @@ public void onDiscoveryStopped() {
1320
1348
1321
1349
/**
1322
1350
* Notify that a Peer is found with the discovery.
1323
-
*
1351
+
* <br /><br />
1324
1352
* Here for example you can save peer in a list or anywhere you want and when the user
1325
1353
* choose a peer you can call bluetoothCommunicator.connect(peer founded) but if you want to
1326
1354
* use a peer for connect you have to have peer updated (see onPeerUpdated or onPeerLost), if you use a
1327
1355
* non updated peer the connection might fail
1328
-
* instead if you want to immediate connect where peer is found you can call bluetoothCommunicator.connect(peer) here
1356
+
* instead if you want to immediate connect where peer is found you can call bluetoothCommunicator.connect(peer) here.
1357
+
*
1329
1358
* @param peer founded peer
1330
1359
*/
1331
1360
publicvoidonPeerFound(Peerpeer) {
1332
1361
}
1333
1362
1334
1363
/**
1335
1364
* Notify that a peer is out of range or has interrupted the advertise.
1365
+
* <br /><br />
1366
+
* Here you can delete the peer lost from a eventual collection of founded peers.
1336
1367
*
1337
-
* Here you can delete the peer lost from a eventual collection of founded peers
1338
1368
* @param peer
1339
1369
*/
1340
1370
publicvoidonPeerLost(Peerpeer) {
1341
1371
}
1342
1372
1343
1373
/**
1344
1374
* Notify that a peer is disconnected, peersLeft indicate the number of connected peers remained
0 commit comments