<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Hussein Aladeen on Medium]]></title>
        <description><![CDATA[Stories by Hussein Aladeen on Medium]]></description>
        <link>https://medium.com/@Hussein_Ala?source=rss-126fdd895027------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*10-Sk5UAR1CF1ddO.jpeg</url>
            <title>Stories by Hussein Aladeen on Medium</title>
            <link>https://medium.com/@Hussein_Ala?source=rss-126fdd895027------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 16:51:56 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@Hussein_Ala/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[FlowR — Making Android Fragments Easy]]></title>
            <link>https://medium.com/fueled-engineering/flowr-making-android-fragments-easy-5ba2ea56d62d?source=rss-126fdd895027------2</link>
            <guid isPermaLink="false">https://medium.com/p/5ba2ea56d62d</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[androiddev]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[fragments]]></category>
            <dc:creator><![CDATA[Hussein Aladeen]]></dc:creator>
            <pubDate>Tue, 04 Jul 2017 10:11:22 GMT</pubDate>
            <atom:updated>2017-07-04T10:11:22.060Z</atom:updated>
            <content:encoded><![CDATA[<p>Fragments are a very big and useful part of the Android platform, but as many Android developers know it’s always a nightmare to deal with. So when we had decided to go with a single activity multiple fragments navigation pattern we knew we had to create a good solution to help us deal with fragments more easily, and thus came about <a href="https://github.com/Fueled/flowr">Flowr</a>.</p><p><a href="https://github.com/Fueled/flowr">Flowr</a> is a small library that simplifies Fragment navigation by providing a wrapper class around the <strong>FragmentManager</strong>. It’s mainly used to navigate between different fragments easily while providing a wide range of functionality. When building Flowr we had three main points we wanted to achieve with the library:</p><ol><li>Provide the ability to easily navigate between different fragments.</li><li>Provide the ability to open fragments for result.</li><li>Automatically keeping the state of the activity correctly synced depending on the custom values specified by the fragment currently visible such as the screen orientation and navigation bar color.</li></ol><p>After the initial release of Flowr, we decided to expand the functionality to also include deep link handling.</p><h3>Adding FlowR to your Project</h3><p>To add Flowr in your project you can simply follow the instruction provided on the <a href="http://jitpack.io/">JitPack</a> website.</p><p><a href="https://jitpack.io/#Fueled/flowr/howto">JitPack | Publish JVM and Android libraries</a></p><h3>Getting Started With FlowR</h3><p>With Flowr each new activity will have its own Flowr instance, the Flowr lifecycle should be bound to the activity, meaning it should be destroyed and created with the activity. For the activity to work with Flowr, the activity has to implement the interface <strong>FlowrScreen</strong>.</p><p>While creating a new Flowr instance these are the required parameters:</p><ul><li><strong>containerId</strong>: this is the id for the layout that the Flowr will use to display the fragments inside.</li><li><strong>FlowrScreen</strong>: in most cases this will be your activity with the interface <strong>FlowrScreen </strong>implemented, this will provide Flowr with access to the activity’s <strong>FragmentManager</strong> and the ability to set values such as the screen orientation and navigation bar color depending on the fragment currently being displayed.</li><li><strong>FragmentsResultPublisher</strong>: this is used to publish results from fragments that were opened for results.</li></ul><p>And some of the optional values are:</p><ul><li><strong>ToolbarHandler</strong>: this is only needed if a single <strong>Toolbar</strong> is going to be used for every fragment, in most cases this will be your activity with the interface <strong>ToolbarHandler</strong> implemented. The <strong>ToolbarHandler</strong> provides extra functionality to the fragments displayed to define the toolbar navigation icon, and to toggle the toolbar visibility.</li><li><strong>tagPrefix</strong>: a custom prefix for the tags to be used for fragments that will be added to the backstack, the default tag used is <strong>#id-</strong>.</li><li><strong>DrawerHandler</strong>: this is only needed if the activity contains a side drawer, again in most cases this will be your activity with the <strong>DrawerHandler </strong>interface implemented. The <strong>DrawerHandler</strong> provides the ability to enable/disable the drawer and open/close the drawer.</li></ul><p>Creating a new Flowr instance for an activity that does not have a toolbar and a drawer:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/996ec36eda04b327c9700a0c76c2d0df/href">https://medium.com/media/996ec36eda04b327c9700a0c76c2d0df/href</a></iframe><p>Creating a new Flowr instance for an activity that does have a toolbar and a drawer:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b7f4bdbcf3cb274d75343a5987c2b9b3/href">https://medium.com/media/b7f4bdbcf3cb274d75343a5987c2b9b3/href</a></iframe><h3>Displaying a Fragment</h3><p>When displaying a new fragment there are multiple parameters that can be specified. Displaying a Fragment is done through a builder pattern which allows us to only specify the parameters that we require easily. these parameters are listed and explained below:</p><ul><li><strong>fragmentClass</strong>: The class of the fragment to be displayed, this is the only parameter that would always be required when displaying a new fragment.</li><li><strong>data</strong>: a <strong>Bundle</strong> containing the arguments the fragment might need. The default value for this is <strong>null</strong>.</li><li><strong>skipBackStack</strong>: specify whether this fragment will be added to the <strong>FragmentManager</strong> back stack or not. The default value for this is <strong>false</strong>.</li><li><strong>clearBackStack</strong>: specify whether the <strong>FragmentManager</strong> backstack should be cleared before displaying this fragment. The default value used for this is <strong>false</strong>.</li><li><strong>replaceCurrentFragment</strong>: specify whether this fragment should replace the fragment currently displayed inside the container or just be added over it. The default value used for this is <strong>false</strong>.</li><li><strong>enterAnim</strong>,<strong> </strong><strong>exitAnim</strong>,<strong> </strong><strong>popEnterAnim</strong>,<strong> </strong><strong>popExitAnim </strong>: animation resource ID for the enter, exit, pop enter, and pop exit fragment animation. The default values used for these animations is <strong>FragmentTransaction.TRANSIT_NONE</strong>.</li></ul><p>finally after we have specified all the parameters we need, we can simply call <strong>displayFragment()</strong> to display the specified fragment.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d86364397904500e45eff7bd7b4c975c/href">https://medium.com/media/d86364397904500e45eff7bd7b4c975c/href</a></iframe><h3>Displaying a Fragment for Results</h3><p>When displaying a fragment for results, we will have access to all the parameters described in the previous section with the only difference being that <strong>displayFragmentForResults</strong> should be called rather than <strong>displayFragment</strong> at the end with the following parameters:</p><ul><li><strong>fragmentId</strong>: a unique ID that each new instance of <strong>AbstractFlowrFragment</strong> is assigned at the start and it can simply be accessed by calling <strong>getFragmentId()</strong> from inside your fragment. This ID is used to deliver the results to the correct fragment instance at the end.</li><li><strong>requestCode</strong>: this code will be returned in <strong>onFragmentResults()</strong> when the fragment is closed, and it can be used to identify the request from which the results were returned.</li></ul><pre><strong>Flowr.open(RequestFragment.class)<br>      .displayFragmentForResults(getFragmentId(), REQUEST_CODE);</strong></pre><p>To handle the results all you need to do is simply just override the <strong>onFragmentResults()</strong> method in your fragment:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/584e651da5a29ec24e02169707d711fd/href">https://medium.com/media/584e651da5a29ec24e02169707d711fd/href</a></iframe><p>Finally when closing a fragment that was opened for results you will need to call the Flowr method <strong>closeWithResults()</strong>:</p><pre><strong>Flowr.closeWithResults(getResultsResponse(resultCode, resultData));</strong></pre><h3>State Sync and Fragment Dependent Customization</h3><p>Flowr provides the ability to set custom attributes for values such as the screen orientation and navigation bar color for each fragment. These values are kept in sync automatically with the parent activity when navigating between different fragments through Flowr <strong>syncScreenState()</strong> method. The <strong>syncScreenState()</strong> method is only required to be invoked manually when a change to one of these values is made after the fragment has already been displayed.</p><p>The following custom values are what is currently supported by the Flowr:</p><h4>Screen Orientation</h4><p>Each fragment can specify its own preferred screen orientation by simply overriding the method <strong>getScreenOrientation()</strong> from <strong>AbstractFragment</strong> and returning one of the following supported values listed <a href="https://developer.android.com/reference/android/content/pm/ActivityInfo.html#screenOrientation">here</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ff9f13d16e818cf00ff93298a211fcd7/href">https://medium.com/media/ff9f13d16e818cf00ff93298a211fcd7/href</a></iframe><h4>Navigation Bar Color</h4><p>Fragments can also specify a preferred color to be used for the navigation bar, this can be done by simply overriding <strong>getNavigationBarColor()</strong> and returning the integer value for the color to be used.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c48fabf5c9a36fd1127957876d4ea507/href">https://medium.com/media/c48fabf5c9a36fd1127957876d4ea507/href</a></iframe><h4>Toolbar Options</h4><p>These values are only used if the parent activity has a toolbar and Flowr was provided a <strong>ToolbarHandler</strong> instance.</p><p>1.<strong> Toolbar Visibility</strong>: The <strong>boolean</strong> value specified here by overriding the <strong>isToolbarVisible()</strong> method in your fragment, will be used to determine if the toolbar should be visible or not. By default the toolbar is visible.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bdb8f4b51ce8af54ce6001807a2af834/href">https://medium.com/media/bdb8f4b51ce8af54ce6001807a2af834/href</a></iframe><p>2.<strong> Navigation Icon Type</strong>: This value is used to determine the type of the navigation icon to be used by the toolbar. This is done simply by overriding the <strong>getNavigationIconType()</strong> method in your fragment, and returning one of the following values <strong>HIDDEN</strong>, <strong>HAMBURGER</strong>, <strong>BACK</strong>, or <strong>CUSTOM</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6304c5c7d41973795a596cea06cd482c/href">https://medium.com/media/6304c5c7d41973795a596cea06cd482c/href</a></iframe><p>3. <strong>Navigation Icon Drawable</strong>: This value is only used if the navigation icon type returned previously was <strong>CUSTOM</strong>. The drawable returned here will be used as the toolbar navigation icon, which can be simply done by just overriding the <strong>getNavigationIcon()</strong> in you fragment.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e1d21d519b5afe0cef27da60781f5de/href">https://medium.com/media/4e1d21d519b5afe0cef27da60781f5de/href</a></iframe><h4>Drawer Options</h4><p>Flowr also provides the ability to specify whether the drawer should be enabled or not for the current fragment, this option is also only available if the fragment has a drawer and Flowr was provided with a <strong>DrawerHandler</strong> instance. To specify this you will have to override the <strong>isDrawerEnabled()</strong> method in your fragment and return either <strong>true</strong> or <strong>false</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/06c97f604dfa6457cde1de4e8057149c/href">https://medium.com/media/06c97f604dfa6457cde1de4e8057149c/href</a></iframe><h3>Deep Linking</h3><p>Deep link handling was also recently added to Flowr. Below are the steps you will need to follow to implement deep link handling.</p><p>First, setup the deep link as explained in the <a href="https://developer.android.com/training/app-indexing/deep-linking.html#adding-filters">documentation</a>. Then annotate your fragment with a <strong>@DeepLink</strong> annotation and provide it with path value to map the fragment to. That value will be the relative path...</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/083e6741e78a8f76b53a7c8bf5f6474e/href">https://medium.com/media/083e6741e78a8f76b53a7c8bf5f6474e/href</a></iframe><p>… or two.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/63bc552838d793207f0bcf3385ac3b85/href">https://medium.com/media/63bc552838d793207f0bcf3385ac3b85/href</a></iframe><p>You can also insert variable:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b066d879ae0c5c6282d1fc78ac2ae019/href">https://medium.com/media/b066d879ae0c5c6282d1fc78ac2ae019/href</a></iframe><h4>Deep Linking Setup</h4><p>To generate your deep link handler you will need to annotate at least one class with the <strong>@DeepLinkHandler</strong> annotation. The name of the class annotated with the <strong>@DeepLinkHandler</strong> annotation would then be used as the name of the generated handler class with &quot;Impl&quot; appended at the end.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0a3ac3c1b41b847c58c1ae70675fccb8/href">https://medium.com/media/0a3ac3c1b41b847c58c1ae70675fccb8/href</a></iframe><p>However it is also possible to specify a custom name for the generated class by passing the desired class name as a string argument to the <strong>@DeepLinkHandler</strong> annotation.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0c359b465f40f69b963e33980b5243fa/href">https://medium.com/media/0c359b465f40f69b963e33980b5243fa/href</a></iframe><p>If you have fragments across multiple modules, you will need to add the <strong>@DeepLinkHandler</strong> annotation to at least one class in each module.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/18af3ffb528f93786c3f98e585395ee4/href">https://medium.com/media/18af3ffb528f93786c3f98e585395ee4/href</a></iframe><p>Then you will need to provide the list of generated deep link handlers to your flowr instance.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa50b4f79470ce92ec853617a1eb2d7f/href">https://medium.com/media/aa50b4f79470ce92ec853617a1eb2d7f/href</a></iframe><p>Finally to trigger the deep linking handling, simply call <strong>open(Intent, Fragment))</strong> from your <strong>Activity#onCreate(Bundle)</strong> method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d2d08eb4a49616a6e32e5ace1ce483c/href">https://medium.com/media/8d2d08eb4a49616a6e32e5ace1ce483c/href</a></iframe><p>Additionally you can access a Fragment via the link attached to it:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/503a3deb4f0535feab8d8c3971c1ddf5/href">https://medium.com/media/503a3deb4f0535feab8d8c3971c1ddf5/href</a></iframe><p>variables work too:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/07fffaae348b57e1e5d9b4d66d7cc70f/href">https://medium.com/media/07fffaae348b57e1e5d9b4d66d7cc70f/href</a></iframe><h3>Upcoming features</h3><p>With the upcoming new version of the Android support library (<strong>26.0.0</strong>), one feature we are really looking forward to incorporating into Flowr is the ability to easily synthesize a new back stack history. This feature will be possible due to the addition of the new <a href="https://developer.android.com/reference/android/support/v4/app/FragmentTransaction.html#setReorderingAllowed(boolean)"><strong>setReorderingAllowed(boolean)</strong></a> Fragment Transaction API, as it will remove redundant operations, by eliminating operations that cancel.</p><p>Finally we are always looking forward to getting feedback from the community and are happy to incorporate new features requested by the community, so if you have any feature in mind you would like to see added in Flowr please feel free to create a new <a href="https://github.com/Fueled/flowr">GitHub issue</a> with the details of the required feature.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5ba2ea56d62d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/fueled-engineering/flowr-making-android-fragments-easy-5ba2ea56d62d">FlowR — Making Android Fragments Easy</a> was originally published in <a href="https://medium.com/fueled-engineering">Fueled Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Google Fit API Integration]]></title>
            <link>https://medium.com/fueled-engineering/google-fit-api-integration-6eea356c3a9?source=rss-126fdd895027------2</link>
            <guid isPermaLink="false">https://medium.com/p/6eea356c3a9</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Hussein Aladeen]]></dc:creator>
            <pubDate>Mon, 24 Apr 2017 15:27:16 GMT</pubDate>
            <atom:updated>2017-04-24T15:27:16.420Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6WZlID-hgh64dD0Hi_sUmg.png" /></figure><h3><strong>Background</strong></h3><p>While working on a project that involved wearables one of the features we were tasked with implementing was step tracking. After some research we discovered that the best way for us to implement this particular feature was to Google Fit API.</p><p>Using the Google Fit API helped us save a large amount of time on having to work on our own back end implementation for steps. Using the Fit API also provides the users access to all the useful features provided by the Google Fit APP.</p><p>This article will cover the logic for integrating the Google Fit API with a BLE device to handle steps data sync and use the Google Fit API as a cloud storage for the steps data.</p><h3>The Architecture</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*Ql4WkjUIXA-1XSkwrNcQUQ.png" /></figure><p>The simple idea behind the architecture shown in the diagram above was to have the following three main components:</p><ul><li><strong>BleManager</strong>: the BLE manager is responsible for all the BLE communication with the wearable device and to provide a simple RxJava backed API to send and receive data from the device.</li><li><strong>FitManager</strong>: the fit manager is responsible for handling all the communication with the Google Fit API, it also provides a simple RxJava backed APIs to read and write steps data to the Fit API.</li><li><strong>StepsSyncManager</strong>: the steps sync manager is responsible for connecting the <strong>BleManager</strong> and the <strong>FitManager</strong> together by retrieving all the steps data from the <strong>BleManager</strong> and passing them to the <strong>FitManager</strong> to be stored using the Google Fit History API.</li></ul><h3>The Implementation</h3><h3>FitManager</h3><p>When implementing the Google Fit API we wanted to use RxJava to simplify some of the complicated APIs that would be commonly used. This led to the creation of <strong>FitManager</strong>.</p><h4><strong>Connecting to the Google Fit API</strong></h4><p>To start using the Google Fit API we first have to create a new Google API client, this can be done by using the Google Fit API client builder as shown below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/084ce3658b4a3dcf245b014bcf960eae/href">https://medium.com/media/084ce3658b4a3dcf245b014bcf960eae/href</a></iframe><p>As shown above, to build the API client we have to specify a couple of values.</p><ul><li>The first value is the API we want to use, in our case we wanted to use the Google Fit history API which can be specified using the <a href="https://developers.google.com/android/reference/com/google/android/gms/fitness/Fitness.html#HISTORY_API"><strong>Fitness.HISTORY_API</strong></a> constant.</li><li>We also have to specify the access scope we want to that particular API, in our case we need both read and write access to the history API to be able to save new steps data and read them again later. This is specified using the constant value <a href="https://developers.google.com/android/reference/com/google/android/gms/common/Scopes.html#FITNESS_ACTIVITY_READ_WRITE"><strong>Scopes.FITNESS_ACTIVITY_READ_WRITE</strong></a>.</li><li>Finally we need to provide a connection callbacks listener and connection failed listener. In the example above we are taking the results from these two listeners and then passing them to an RxJava <a href="http://reactivex.io/RxJava/javadoc/rx/subjects/PublishSubject.html"><strong>PublishSubject</strong></a>. This gives us the ability to subscribe to these updates anywhere in the app where information about the connection state is required.</li></ul><h4>Retrieving total steps recorded between two timestamps</h4><p>After connecting to the Google Fit API client we can start retrieving the steps data previously recorded. The method below is used to retrieve the total steps recorded between two timestamps and returning them as an RxJava observable.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0d491e93085d25db2a2d23933a4a82ee/href">https://medium.com/media/0d491e93085d25db2a2d23933a4a82ee/href</a></iframe><h4>Retrieving total steps recorded for the day</h4><p>Sometimes we also want to retrieve the total steps recorded for the current data, thankfully the Fit API provides a simple way to retrieve the daily total as shown below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f84742a026bf954921200d4f55752c12/href">https://medium.com/media/f84742a026bf954921200d4f55752c12/href</a></iframe><h4>Inserting new steps</h4><p>Finally we need the ability to insert new steps data, the methods below allow us to insert steps data for either a single data point or multiple data points. Each data point consist of the following:</p><ul><li><strong>startTime</strong>: the start timestamp for when these steps were recorded.</li><li><strong>endTime</strong>: the end timestamp for when these steps were recorded.</li><li><strong>steps</strong>: the total steps recorded steps between the specified timestamps.</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f43a921786a0e65c3c6ee210405d39b8/href">https://medium.com/media/f43a921786a0e65c3c6ee210405d39b8/href</a></iframe><h3>BleManager</h3><p>The <strong>BleManager</strong> is responsible for providing the ability to easily connect to the BLE device, send commands, and retrieve data from the device. To Simplify our <strong>BleManager</strong> implementation we looked for a good library that wraps around the core Android BLE APIs and provides RxJava support. After some research we decided to go with <a href="https://github.com/Polidea/RxAndroidBle">RxAndroidBLE</a>, a really powerful library that removes a lot of the headache that comes with using the core Android BLE APIs.</p><h4>Sending commands</h4><p>To send commands to the BLE device we first need to retrieve the BLE characteristic we want to send the commands to. After retrieving the characteristic we have to specify the following two values:</p><ul><li>Write Type: the write type of the command being sent, it can be one of: <a href="https://developer.android.com/reference/android/bluetooth/BluetoothGattCharacteristic.html#WRITE_TYPE_DEFAULT"><strong>WRITE_TYPE_DEFAULT</strong></a>, <a href="https://developer.android.com/reference/android/bluetooth/BluetoothGattCharacteristic.html#WRITE_TYPE_NO_RESPONSE"><strong>WRITE_TYPE_NO_RESPONSE</strong></a><strong> </strong>or <a href="https://developer.android.com/reference/android/bluetooth/BluetoothGattCharacteristic.html#WRITE_TYPE_SIGNED"><strong>WRITE_TYPE_SIGNED</strong></a>.</li><li>Value: the value for the command we want to send.</li></ul><p>Finally after specifying the write type and command value we can send back the characteristic to the BLE device as shown below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/603a7a8fe105b291232c6ca21d98f6fa/href">https://medium.com/media/603a7a8fe105b291232c6ca21d98f6fa/href</a></iframe><h4>Receiving data from BLE notifications</h4><p>Data are sent from the BLE device using GATT notifications, so when we want to receive data from the BLE device we would setup a notification to a specific BLE characteristic and listen to its value changes. Due to the BLE limitation around the size of the data allowed to be transferred at a time, some responses have to be split into multiple parts with a header and trailer used to indicate the start and end of the transfer.</p><p>The example below shows the code for setting up notifications that will listen to steps data responses for a specific date. These responses come in multiple parts so they are being combined into a single object and then published using an RxJava <a href="http://reactivex.io/RxJava/javadoc/rx/subjects/PublishSubject.html"><strong>PublishSubject</strong></a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/44e6e85b4362ff799b517c0caff11abb/href">https://medium.com/media/44e6e85b4362ff799b517c0caff11abb/href</a></iframe><h3>StepsSyncManager</h3><p>Finally the <strong>StepsSyncManager </strong>is responsible for connecting the <strong>BleManager</strong> and <strong>FitManager</strong> together, by retrieving steps data from our <strong>BleManager</strong> and pushing them to the <strong>FitManager</strong>. The <strong>StepsSyncManager</strong> is started through a sticky service and kept running in the background all the time. This insures steps data are kept up to data with the Google Fit API, and if the users were to open the Google Fit App at any moment they would be represented with an accurate count of their steps for the day.</p><p>Steps syncing is divided into two parts: the first is retrieving the steps data from our BLE device and the second pushing that data to the Google Fit API.</p><h4>Retrieving steps data from BLE</h4><p>To regularly receive step data updates from our BLE device, we had to setup a new BLE notification on the characteristic responsible for publishing steps data updates.</p><p>Once we were subscribed to the steps data notifications we had to slow down the amount of updates we were receiving, as our BLE device pushes multiple updates a second and we wanted to avoid hammering the Google Fit API with hundreds of requests every couple of minutes. To achieve that we used the RxJava <a href="http://reactivex.io/documentation/operators/sample.html"><strong>Sample</strong></a> operator, which allowed us to only receive the last published update every 30 minutes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f33a1eeaa48840aeb2af58a0c068d8a4/href">https://medium.com/media/f33a1eeaa48840aeb2af58a0c068d8a4/href</a></iframe><h4>Pushing steps data to Google Fit API</h4><p>After we’ve received new steps data update , we need to push these updates to the Google Fit API. As our steps data updates contain the total steps count for the day and we only need to push the number of the steps since our last update, we first need to retrieve the total steps count since our last update from the Google Fit API which then we can use to work out the steps count delta. Finally after we have worked out the amount of steps walked since our last update we can insert a new steps record using our <strong>FitManager</strong> as shown below.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/639793b318d1e24585975be84171da45/href">https://medium.com/media/639793b318d1e24585975be84171da45/href</a></iframe><h3>Conclusion</h3><p>The Google Fit API provides great value at a very low cost for both the developer and user. It removes all the complications that come with implementing your own backend to store all the steps data, while providing the user with a tremendous set of tools to interact and better analyze the collected steps data using the Google Fit App.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6eea356c3a9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/fueled-engineering/google-fit-api-integration-6eea356c3a9">Google Fit API Integration</a> was originally published in <a href="https://medium.com/fueled-engineering">Fueled Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>