Skip to content

Commit caef917

Browse files
committed
Fixed documentation
1 parent 3255e45 commit caef917

11 files changed

+125
-80
lines changed

app/src/main/java/com/bluetooth/communicator/BluetoothCommunicator.java

Lines changed: 56 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,21 @@
4949
* It also automatically implements (they are active by default) reconnection in case of temporary connection loss, reliable message sending,
5050
* 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)
5151
* even in case of connection problems (they will be sent as soon as the connection is restored)
52-
*
52+
* <br /><br />
5353
* First create a bluetooth communicator object, it is the object that handles all operations of bluetooth low energy library, if you want to manage
5454
* the bluetooth connections in multiple activities I suggest you to save this object as an attribute of a custom class that extends Application and
5555
* create a getter so you can access to bluetoothCommunicator from any activity or service with:
56-
*
56+
* <pre>{@code
5757
* ((custom class name) getApplication()).getBluetoothCommunicator();
58+
* }</pre>
5859
* 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
5960
* and can be only characters listed in BluetoothTools.getSupportedUTFCharacters(context) because the number of bytes for advertising beacon is limited) and the strategy
6061
* (for now the only supported strategy is BluetoothCommunicator.STRATEGY_P2P_WITH_RECONNECTION)
61-
*
62+
* <pre>{@code
6263
* bluetoothCommunicator = new BluetoothCommunicator(this, "device name", BluetoothCommunicator.STRATEGY_P2P_WITH_RECONNECTION);
64+
* }</pre>
6365
* Then add the bluetooth communicator callback, the callback will listen for all events of bluetooth communicator:
64-
*
66+
* <pre>{@code
6567
* bluetoothCommunicator.addCallback(new BluetoothCommunicator.Callback() {
6668
* @Override
6769
* public void onBluetoothLeNotSupported() {
@@ -149,11 +151,12 @@
149151
* device has accepted your connection request and the connection is complete, from now on you
150152
* can send messages or data (or disconnection request) to this peer until onDisconnected
151153
*
152-
To send messages to all connected peers you need to create a message with a context, a header, represented by a single character string
153-
* (you can use a header to distinguish between different types of messages, or you can ignore it and use a random
154-
* character), the text of the message, or a series of bytes if you want to send any kind of data and the peer you want to send the message to
155-
* (must be connected to avoid errors), example: new Message(context,"a","hello world",peer);
156-
* If you want to send message to a specific peer you have to set the sender of the message with the corresponding peer.
154+
* To send messages to all connected peers you need to create a message with a context, a header, represented by a
155+
* single character string (you can use a header to distinguish between different types of messages, or you can ignore
156+
* it and use a random character), the text of the message, or a series of bytes if you want to send any kind of data
157+
* and the peer you want to send the message to (must be connected to avoid errors), example:
158+
* new Message(context,"a","hello world",peer); If you want to send message to a specific peer you have to set
159+
* the sender of the message with the corresponding peer.
157160
*
158161
* To send disconnection request to connected peer you need to call bluetoothCommunicator.disconnect(peer);
159162
* }
@@ -218,17 +221,20 @@
218221
* (however the disconnection will be notified in onDisconnection)
219222
* }
220223
* });
224+
* }</pre>
221225
* Finally you can start discovery and/or advertising:
222-
*
226+
* <pre>{@code
223227
* bluetoothCommunicator.startAdvertising();
224228
* bluetoothCommunicator.startDiscovery();
229+
* }</pre>
225230
* All other actions that can be done are explained with the comments in the code of callback I wrote before.
226-
*
231+
* <br /><br />
227232
* To use this library add these permissions to your manifest:
228-
*
233+
* <pre>{@code
229234
* <uses-permission android:name="android.permission.BLUETOOTH"/>
230235
* <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
231236
* <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
237+
* }</pre>
232238
*/
233239
public class BluetoothCommunicator {
234240
// constants
@@ -278,6 +284,7 @@ public class BluetoothCommunicator {
278284

279285
/**
280286
* Constructor of BluetoothCommunicator
287+
*
281288
* @param context
282289
* @param name the name by which the other devices will see us (limited to 18 characters
283290
* 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) {
429436

430437
/**
431438
* Constructor of BluetoothCommunicator that adds a callback (it can also be added with addCallback
432-
* @param context
439+
*
440+
* @param context context
433441
* @param name the name by which the other devices will see us (limited to 18 characters
434442
* and can be only characters listed in BluetoothTools.getSupportedUTFCharacters(context) because the number of bytes for advertising beacon is limited)
435443
* @param strategy (for now the only supported strategy is BluetoothCommunicator.STRATEGY_P2P_WITH_RECONNECTION)
@@ -572,7 +580,7 @@ public void onDisconnectionFailed() {
572580
/**
573581
* This method start advertising, so this device can be discovered by other devices that use this library and can receive a connection request,
574582
* 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 />
576584
* This method must always be done from the main thread or it will return NOT_MAIN_THREAD without doing anything.
577585
*
578586
* @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() {
650658

651659
/**
652660
* 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 />
654662
* This method must always be done from the main thread or it will return NOT_MAIN_THREAD without doing anything.
655663
*
656664
* @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() {
712720

713721
/**
714722
* 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+
*
715724
* @return SUCCESS if bluetooth le is supported, BLUETOOTH_LE_NOT_SUPPORTED if not (or rarely if we had a generic bluetooth problem)
716725
*/
717726
public int isBluetoothLeSupported() {
@@ -725,7 +734,7 @@ public int isBluetoothLeSupported() {
725734
/**
726735
* This method start discovery so BluetoothCommunicator can discover advertising devices and notify them with onPeerFound,
727736
* 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 />
729738
* This method must always be done from the main thread or it will return NOT_MAIN_THREAD without doing anything.
730739
*
731740
* @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() {
799808

800809
/**
801810
* 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 />
803812
* This method must always be done from the main thread or it will return NOT_MAIN_THREAD without doing anything.
804813
*
805814
* @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() {
858867
/**
859868
* 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
860869
* the message will be sent to all the connected peers
870+
*
861871
* @param message message to be sent
862872
*/
863873
public void sendMessage(final Message message) {
@@ -897,6 +907,7 @@ public void onMessageSent() { // means that we have sent the message to all th
897907
/**
898908
* 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
899909
* the message will be sent to all the connected peers
910+
*
900911
* @param data message to be sent
901912
*/
902913
public void sendData(final Message data) {
@@ -937,9 +948,10 @@ public void onMessageSent() { // means that we have sent the message to all th
937948
* this method set the name with which you would be seen by other devices, the name must be limited to 18 characters
938949
* and can be only characters listed in BluetoothTools.getSupportedUTFCharacters(context) because the number of bytes for advertising beacon is limited,
939950
* there is no controls for this so if you not follow these restrictions BluetoothCommunicator will not work correctly.
940-
*
951+
* <br /><br />
941952
* 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),
942953
* this allows to have a unique identification (for BluetoothCommunicator, not for the user) even for peers with the same name
954+
*
943955
* @param name
944956
* @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)
945957
*/
@@ -981,6 +993,7 @@ public ArrayList<Peer> getConnectedPeersList() {
981993
/**
982994
* This method must be used after you have received a connection request to accept it and complete the connection (the connection is complete when
983995
* onConnectionSuccess is called)
996+
*
984997
* @param peer the peer that has sent the connection request that we want to accept
985998
* @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)
986999
*/
@@ -994,7 +1007,8 @@ public int acceptConnection(Peer peer) {
9941007
}
9951008

9961009
/**
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+
*
9981012
* @param peer the peer that has sent the connection request that you want to accept
9991013
* @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)
10001014
*/
@@ -1009,6 +1023,7 @@ public int rejectConnection(Peer peer) {
10091023

10101024
/**
10111025
* This method is used to know if BluetoothCommunicator is advertising
1026+
*
10121027
* @return advertising
10131028
*/
10141029
public boolean isAdvertising() {
@@ -1026,7 +1041,8 @@ public boolean isDiscovering() {
10261041
/**
10271042
* 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
10281043
* onConnectionSuccess and onConnectionFailed
1029-
* @param peer
1044+
*
1045+
* @param peer found peer you want to connect with
10301046
* @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)
10311047
* or DESTROYING if destroy() is called before
10321048
*/
@@ -1055,6 +1071,7 @@ public int readPhy(Peer peer) {
10551071

10561072
/**
10571073
* This method return the bluetooth adapter used by BluetoothCommunicator
1074+
*
10581075
* @return bluetoothAdapter
10591076
*/
10601077
public BluetoothAdapter getBluetoothAdapter() {
@@ -1066,8 +1083,9 @@ public BluetoothAdapter getBluetoothAdapter() {
10661083
* when onDisconnected is called with that peer as argument, in case the disconnection fails onDisconnectionFailed is called but if you not
10671084
* override it or leave the call to super BluetoothCommunicator will turn off and on bluetooth to force the disconnection and onDisconnection will be
10681085
* 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)
10711089
*/
10721090
public int disconnect(final Peer peer) {
10731091
synchronized (bluetoothLock) {
@@ -1089,6 +1107,7 @@ public int disconnect(final Peer peer) {
10891107

10901108
/**
10911109
* This method will call disconnect for all the connected peers
1110+
*
10921111
* @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)
10931112
*/
10941113
public int disconnectFromAll() {
@@ -1124,10 +1143,19 @@ public interface DestroyCallback {
11241143
}
11251144

11261145

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+
*/
11271151
public void addCallback(Callback callback) {
11281152
clientCallbacks.add(callback);
11291153
}
11301154

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+
*/
11311159
public void removeCallback(Callback callback) {
11321160
clientCallbacks.remove(callback);
11331161
}
@@ -1320,28 +1348,31 @@ public void onDiscoveryStopped() {
13201348

13211349
/**
13221350
* Notify that a Peer is found with the discovery.
1323-
*
1351+
* <br /><br />
13241352
* Here for example you can save peer in a list or anywhere you want and when the user
13251353
* choose a peer you can call bluetoothCommunicator.connect(peer founded) but if you want to
13261354
* use a peer for connect you have to have peer updated (see onPeerUpdated or onPeerLost), if you use a
13271355
* 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+
*
13291358
* @param peer founded peer
13301359
*/
13311360
public void onPeerFound(Peer peer) {
13321361
}
13331362

13341363
/**
13351364
* 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.
13361367
*
1337-
* Here you can delete the peer lost from a eventual collection of founded peers
13381368
* @param peer
13391369
*/
13401370
public void onPeerLost(Peer peer) {
13411371
}
13421372

13431373
/**
13441374
* Notify that a peer is disconnected, peersLeft indicate the number of connected peers remained
1375+
*
13451376
* @param peer disconnected peer
13461377
* @param peersLeft remaining peers connected
13471378
*/

0 commit comments

Comments
 (0)