@@ -110,7 +110,7 @@ public class DeviceStateFragment extends Fragment
110110 * @param device that was connected.
111111 */
112112 @Override
113- public void onDeviceConnected (@NonNull final AbstractBleDevice device ){
113+ public void onDeviceConnected (@NonNull final BleDevice device ){
114114 // Do something.
115115 }
116116
@@ -120,7 +120,7 @@ public class DeviceStateFragment extends Fragment
120120 * @param device that was disconnected.
121121 */
122122 @Override
123- public void onDeviceDisconnected (@NonNull final AbstractBleDevice device ){
123+ public void onDeviceDisconnected (@NonNull final BleDevice device ){
124124 // Do something.
125125 }
126126
@@ -130,50 +130,54 @@ public class DeviceStateFragment extends Fragment
130130 * @param device that was discovered.
131131 */
132132 @Override
133- public void onDeviceDiscovered (@NonNull final AbstractBleDevice device ){
133+ public void onDeviceDiscovered (@NonNull final BleDevice device ){
134134 // Do something.
135135 }
136136
137137 /**
138138 * This method is called when all the device services are discovered.
139139 */
140140 @Override
141- public void onDeviceAllServicesDiscovered (@NonNull final AbstractBleDevice device ){
141+ public void onDeviceAllServicesDiscovered (@NonNull final BleDevice device ){
142142 // Do something.
143143 }
144144}
145145```
146146
147147### STEP 3: Connect to a BLE device (* AbstractBleDevice* )
148148
149- ##### STEP 3.1: Scan for * AbstractBleDevices *
149+ ##### STEP 3.1: Scan for * BleDevices *
150150
151151###### OPTION 1: Scan for all devices
152152``` java
153153BleManager . getInstance(). startScanning()
154154```
155155
156156###### OPTION 2: Scan for devices with specific UUIDs
157+
157158``` java
158- private void scanForBleDevicesUsingUUIDs (@NonNull final UUID [] validDeviceUUIDs) {
159- BleManager . getInstance(). startScanning(validDeviceUUIDs)
160- }
159+ // Scans for a default period of time. (10 seconds)
160+ BleManager . getInstance(). startScanning(deviceUUIDs)
161+ ```
162+ or
163+ ``` java
164+ BleManager . getInstance(). startScanning(scanDurationMilliseconds, deviceUUIDs)
161165```
162166
163167##### STEP 3.2: Retrieve discovered devices
164168
165169###### OPTION 1: Retrieve all discovered devices
166170``` java
167171@NonNull
168- public Iterable < ? extends AbstractBleDevice > getDiscoveredBleDevices() {
172+ public Iterable < ? extends BleDevice > getDiscoveredBleDevices() {
169173 return BleManager . getInstance(). getDiscoveredBleDevices();
170174}
171175```
172176
173177###### OPTION 2: Retrieve discovered devices with specific advertise names
174178``` java
175179@NonNull
176- public Iterable<? extends AbstractBleDevice > getDiscoveredBleDevices(){
180+ public Iterable<? extends BleDevice > getDiscoveredBleDevices(){
177181 final List acceptedAdvertiseNames = new LinkedList ();
178182 acceptedAdvertiseNames. add(" SHTC1 SmartGadget" );
179183 acceptedAdvertiseNames. add(" Smart Humigadget" );
@@ -185,7 +189,7 @@ public Iterable<? extends AbstractBleDevice> getDiscoveredBleDevices(){
185189
186190###### OPTION 1: Connect using the * AbstractBleDevice* object
187191``` java
188- public static void connectDevice (@NonNull AbstractBleDevice device){
192+ public static void connectDevice (@NonNull BleDevice device){
189193 device. connect();
190194}
191195```
@@ -206,6 +210,7 @@ registerDeviceListener(deviceAddress, NotificationListener) methods.
206210``` java
207211import com.sensirion.libble.BleManager ;
208212import com.sensirion.libble.listeners.services.HumidityListener ;
213+ import com.sensirion.libble.utils.HumidityUnit ;
209214
210215/**
211216 * Example fragment that listens for humidity notifications.
@@ -229,30 +234,32 @@ public class HumidityListenerFragment extends Fragment implements HumidityListen
229234 /**
230235 * Advices the listeners that a new humidity value was obtained.
231236 *
232- * @param device {@link AbstractBleDevice } that send the humidity data.
237+ * @param device {@link BleDevice } that send the humidity data.
233238 * @param humidity {@link float} with the humidity value.
234239 * @param sensorName {@link String} with the sensor name.
240+ * @param unit {@link HumidityUnit} with the humidity unit.
235241 */
236242 public void onNewHumidity (@NonNull final AbstractBleDevice device ,
237243 final float humidity ,
238244 @NonNull final String sensorName ,
239- @NonNull final String unit ){
245+ @NonNull final HumidityUnit unit ){
240246 // Do something
241247 }
242248
243249 /**
244250 * Sends to the user the latest historical humidity.
245251 *
246- * @param device that send the humidity historical value.
247- * @param relativeHumidity from a moment in the past.
248- * @param timestamp in milliseconds that determine when the humidity was obtained.
249- * @param sensorName that send the humidity.
252+ * @param device {@link BleDevice} that send the humidity data.
253+ * @param relativeHumidity {@link float} from a moment in the past.
254+ * @param timestampMillisUTC {@link long} when the humidity was obtained.
255+ * @param sensorName {@link String} with the sensorName.
256+ * @param unit {@link HumidityUnit} with the humidity unit.
250257 */
251258 public void onNewHistoricalHumidity (@NonNull final AbstractBleDevice device ,
252259 final float relativeHumidity ,
253260 final long timestamp ,
254261 @NonNull final String sensorName ,
255- @NonNull final String unit ){
262+ @NonNull final HumidityUnit unit ){
256263 // Do something
257264 }
258265}
@@ -268,17 +275,22 @@ BleManager.getInstance().release(context);
268275```
269276## Add new services to the library:
270277
271- * Create for every new service a new interface in order
272- to be able to listen for notifications of the new service.
273- The new interface must extend
274- com.sensirion.libble.listeners.NotificationListener.
275-
276278###### Notification interface example:
279+
280+ If we want to receive notifications from a service, we may want
281+ to receive customized notifications. In order to listen for them
282+ we can create a new
283+ com.sensirion.libble.listeners.NotificationListener interface.
284+
285+ For example, if we want to create a service that listens for the
286+ number of steps notifications from an external Bluetooth gadget,
287+ we can implement the following interface:
288+
277289``` java
278290import com.sensirion.libble.services.NotificationListener ;
279291
280292public interface StepListener extends NotificationListener {
281- void onNewStep (@NonNull AbstractBleDevice device , int numberSteps );
293+ void onNewStep (@NonNull BleDevice device , int numberSteps );
282294}
283295```
284296After the interface is created, a new service can be built whose
@@ -287,16 +299,6 @@ to extend com.sensirion.libble.services.AbstractBleService. It is possible
287299to generify the AbstractBleService to manage the listener registration
288300automatically.
289301
290- ###### Service registration:
291-
292- ``` java
293- /**
294- * The following command needs to be called before the device connection.
295- * If a device with this service is connected, the factory will create
296- * instances of this service class automatically.
297- */
298- BleServiceFactory . registerServiceImplementation(SERVICE_UUID , StepService . class);
299- ```
300302###### Service example:
301303
302304``` java
@@ -305,17 +307,21 @@ import android.bluetooth.BluetoothGattService;
305307import com.sensirion.libble.devices.Peripheral ;
306308import static android.bluetooth.BluetoothGattCharacteristic.FORMAT_UINT32 ;
307309
308- public class StepService extends AbstractBleService<StepListener > {
310+ /**
311+ * This is an example on how to implement a service that listens for step
312+ * notifications from an external Bluetooth device.
313+ */
314+ public class StepListener extends AbstractBleService<StepListener > {
309315
310316 // SERVICE UUIDs
311317 public static final String SERVICE_UUID = " 0000352f-0000-1000-8000-00805f9b34fb" ;
312- private static final String CHARACTERISTIC_UUID = " 00002539-0000-1000-8000-00805f9b34fb " ;
318+ private static final String FOUR_BYTE_CHARACTERISTIC_UUID = " 2539 " ;
313319 private final BluetoothGattCharacteristic mStepCharacteristic;
314320
315321 public StepService (@NonNull final Peripheral parent ,
316322 @NonNull final BluetoothGattService bluetoothGattService ) {
317323 super (parent, bluetoothGattService);
318- mStepCharacteristic = getCharacteristic(CHARACTERISTIC_UUID );
324+ mStepCharacteristic = getCharacteristic(FOUR_BYTE_CHARACTERISTIC_UUID );
319325 parent. readCharacteristic(mStepCharacteristic);
320326 }
321327
@@ -330,17 +336,17 @@ public class StepService extends AbstractBleService<StepListener> {
330336 /**
331337 * Method called when a characteristic is read.
332338 *
333- * @param characteristic that was updated.
339+ * @param char updated { @link BluetoothGattCharacteristic}
334340 * @return <code >true</code> if the characteristic was read correctly.
335341 */
336342 @Override
337- public boolean onCharacteristicUpdate (@NonNull BluetoothGattCharacteristic characteristic ) {
338- if (mStepCharacteristic. equals(updatedCharacteristic )) {
339- final int numberSteps = characteristic . getIntValue(FORMAT_UINT32 , 0 );
343+ public boolean onCharacteristicUpdate (@NonNull BluetoothGattCharacteristic char ) {
344+ if (mStepCharacteristic. equals(char )) {
345+ final int numberSteps = char . getIntValue(FORMAT_UINT32 , 0 );
340346 notifyListeners(numberSteps);
341347 return true ;
342348 }
343- return super . onCharacteristicUpdate(updatedCharacteristic );
349+ return super . onCharacteristicUpdate(char );
344350 }
345351
346352 private void notifyListeners (final int numberSteps ) {
@@ -356,7 +362,7 @@ public class StepService extends AbstractBleService<StepListener> {
356362 }
357363
358364 /**
359- * Checks if a service is ready to use.
365+ * Checks if the service is ready to use.
360366 *
361367 * @return <code >true</code> if the service is synchronized.
362368 */
@@ -366,12 +372,24 @@ public class StepService extends AbstractBleService<StepListener> {
366372 }
367373
368374 /**
369- * Tries to synchronize a service in case some of its data is missing.
375+ * This method tries to obtain all the data the service needs and
376+ * it registers for all the missing notitifications.
370377 */
371378 @Override
372379 public abstract void synchronizeService () {
373380 // If notifications are registered, it won't do anything.
374381 registerDeviceCharacteristicNotifications();
375382 }
376383}
384+ ```
385+
386+ ###### Service registration:
387+
388+ ``` java
389+ /**
390+ * The following command needs to be called before the device connection.
391+ * If a device with this service is connected, the factory will create
392+ * instances of this service class automatically.
393+ */
394+ BleServiceFactory . registerServiceImplementation(SERVICE_UUID , ExampleService . class);
377395```
0 commit comments