<?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 Elanur Yoktan on Medium]]></title>
        <description><![CDATA[Stories by Elanur Yoktan on Medium]]></description>
        <link>https://medium.com/@elanuryoktan?source=rss-b52498a77394------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Ugxvg3m3O2x-DA7Y.</url>
            <title>Stories by Elanur Yoktan on Medium</title>
            <link>https://medium.com/@elanuryoktan?source=rss-b52498a77394------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 14:03:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@elanuryoktan/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[iOS Smartwatch App using CoreBluetooth and RxSwift]]></title>
            <link>https://medium.com/deliveryherotechhub/ios-smartwatch-app-using-corebluetooth-and-rxswift-23f322ae1474?source=rss-b52498a77394------2</link>
            <guid isPermaLink="false">https://medium.com/p/23f322ae1474</guid>
            <category><![CDATA[corebluetooth]]></category>
            <category><![CDATA[rxswift]]></category>
            <category><![CDATA[ble]]></category>
            <category><![CDATA[smartwatch]]></category>
            <dc:creator><![CDATA[Elanur Yoktan]]></dc:creator>
            <pubDate>Wed, 23 Mar 2022 10:34:40 GMT</pubDate>
            <atom:updated>2022-03-23T10:34:40.888Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/891/1*Wk_lT8lP4S67JE8ijRlCew.png" /></figure><p>This article will help you to get a general understanding of data transfer procedures and formats in Bluetooth Low Energy (BLE) connection and how to build an application for your BLE device with CoreBluetooth and RxSwift. Also, you will find a little bit of reverse engineering thanks to the PacketLogger. I hope you enjoy it!</p><p>I have a smartwatch (Everest W311 Fit Mate) that I bought years ago and haven’t used for a long time. I have recently decided to use it again, but its application was outdated and had problems with connection and UI. I thought it would be fun to play with it and try to develop my own application that will monitor heart rate, sleep quality, and steps and allow customizing the settings.</p><p><strong>GATT</strong></p><p>Let me tell you a little bit about <strong>GATT(The Generic Attribute Profile)</strong>. It is a profile that defines data transfer procedures and formats between a <strong>client</strong>(<em>central</em>) and a <strong>server</strong>(<em>peripheral</em>) in a BLE connection. The server contains the data to be monitored and listens to requests from clients and sends responses back. The Client can discover the services of the Server and send requests and receive responses. So basically in this project, the watch is our server and the phone is our client.</p><p>A server contains data in form of attributes. Attributes are the smallest addressable pieces of information that are actually transferred between devices. <strong>Services</strong> and <strong>Characteristics</strong> are defined to organize this data in a practical manner. You can see the illustration of the data hierarchy that is defined by GATT.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/891/1*Rcim9fs95KZ4ncN8-2Dseg.png" /><figcaption>image source: <a href="https://www.oreilly.com/library/view/getting-started-with/9781491900550/ch04.html#gatt_data_hierarchy">https://www.oreilly.com/library/view/getting-started-with/9781491900550/ch04.html#gatt_data_hierarchy</a></figcaption></figure><p>Each service with a 16-bit UUID(Universally Unique Identifier)s contains characteristics that have related user data. Characteristic is a container for user data and may have descriptors that provide meta-information about the characteristic.</p><p>Access permission determines whether the client can read and write attribute values. Each attribute can have one of the following access permissions: none, write, read, and both. For characteristics, to understand which operations are allowed, characteristic properties were defined. Besides <strong>Read</strong> and <strong>Write</strong> properties, there are also <strong>Write without response</strong>, <strong>Notify</strong> and <strong>Indicate</strong>. Notify and Indicate properties are particularly important because these operations are initiated by the server, but the client first must activate them using the descriptors described in the <strong>Client Characteristic Configuration Descriptor</strong>.</p><p>For further information about GATT, I suggest this reading <a href="https://www.oreilly.com/library/view/getting-started-with/9781491900550/ch04.html">Getting Started with Bluetooth Low Energy</a></p><p><strong>Sniffing BLE packets</strong></p><p>From some previous projects on BLE, I knew an application named <a href="https://www.nordicsemi.com/Products/Development-tools/nRF-Connect-for-mobile"><strong>nRF Connect</strong></a> by Nordic Semiconductor which provides users to explore GATT services and characteristics and runs on both iOS and Android. It is a really handy application if you have no documentation about the GATT services of the device.</p><p>In the first step, to understand the GATT services of the device, I used nRF Connect. Below you can see the services and characteristics that I explored thanks to the application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/891/1*x02RtQdqlslvek1VLStdKg.png" /></figure><p>When I try to be notified of the characteristic that named heart rate measurement via nRF Connect, could not see any value or value updating. But after enabling the client characteristic configuration, I got all updates instantly. <strong>Client characteristic configuration </strong>acts as a switch that enables or disables server-initiated updates, but only for the feature in which it is located. After the bit is set to 1 (true), the server responds with a write response and starts sending the appropriate packets if it wants to inform the client about a value change. After I start to develop the application realized that <em>CoreBluetooth</em> already handles this with <strong><em>setNotifyValue()</em></strong>, so there is no need to write a value to this descriptor. If you still try to set this configuration with <em>writeValue()</em> you will get an error like below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*_NU_u8Jgxa3riUn5a610Mg.png" /></figure><p>Although nRF Connect really help me to understand services and characteristics of the device, I could not figure out how can I read step count or how can I change the settings. What I need was sniffing of Bluetooth packets that the current app sending or receiving from the device to understand which characteristic stands for. Further Googling, I came across the <strong>PacketLogger</strong> which is presented at a <a href="https://developer.apple.com/videos/play/wwdc2019/901/">session</a> of WWDC19. It was exactly what I need, PacketLogger is a packet analysis application that can help you debug and trace your application. To <strong>trace the packets live</strong>, I downloaded the <a href="https://developer.apple.com/bug-reporting/profiles-and-logs/?name=Bluetooth"><strong>Bluetooth logging profile</strong></a> and install it on my phone. Here is a <a href="https://www.bluetooth.com/blog/a-new-way-to-debug-iosbluetooth-applications/">link</a> for more detailed instructions for setup.</p><p>On the screenshot below, you can see the Bluetooth packets that are sent or received from the current outdated app that I mentioned before. To get detailed information, I filter the logs for each characteristic and tried to analyze raw data. There are also filtering options in PacketLogger that helped me to filter the logs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R6QGr75JHx4_MI4le8psgA.png" /><figcaption>image: Screenshot from PacketLogger that shows all Bluetooth packets that are sent to the device or received from the device</figcaption></figure><p>The next step was filtering the logs and trying to understand the characteristics. The challenging part is understanding which byte represents what. That was easy for heart rate but for others, I had to analyze logs from different situations and try to understand the differences.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pUEOHz64r4J2s1lU7up_7w.png" /></figure><p>You can see the characteristic which gives step count below. First I had to convert hex values to decimals and then after a couple of trials, I figure out which byte represents what, still there are bytes that I couldn’t understand what they represent.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dLaff_fuaG57-iSlI1x4Nw.png" /></figure><p>Next characteristic is used to write settings. For this device, each setting can be customized via same characteristic. 3rd byte refers to the type of the setting. For example, if 3rd byte is equal to 9 it means it will set the alarm clock, if it is equal to 3 it means it will set display options. I found the pattern by setting different alarms and different repeat times for each. Below you can see the details to set clock.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C-m5Y4I59jpJs1n86AzJxQ.png" /></figure><p><strong>Develop MySmartWatch App</strong></p><p>After figure out how to read heart rate, step count and how to customize settings, the next step was try to develop the application. By the way, I could not figure out sleep quality logs, so for now I skip that. In this project, my goal was make a project using CoreBluetooth and get used to using RxSwift.</p><p>The process of connecting a device and receiving data or writing the data is shown in the flow. Scanned peripherals can be listened to by implementing the <strong><em>CBCentralManagerDelegate</em></strong><em> </em>methods, and discovered services and characteristics can be listened to by implementing <strong><em>CBPeripheralDelegate</em></strong><em> </em>methods.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tKFaXl9OdaQ0gOv9frXv_Q.png" /></figure><p>The only type of peripheral I am currently interested in is a heart rate monitor, so I need to look for only heart rate monitors. With Bluetooth-speak, I only look for peripherals that offer heart rate services. From previous service discoveries, we already know the UUID of heart rate service is <strong>0x180D. </strong>In CoreBluetooth <strong><em>CBUUID</em></strong> is representing the UUID. By using the string value of UUID we can create a <em>CBUUID</em> object and pass it to <em>scanForPeripherals(withServices:)</em> function as an array. In this way, we only list heart rate monitors.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/68748b186ba63b152bfe2fd13d7e7e51/href">https://medium.com/media/68748b186ba63b152bfe2fd13d7e7e51/href</a></iframe><p>Instead of implementing mentioned delegate methods, I used <strong><em>DelegateProxy</em></strong> which is a core feature in RxSwift to wrap the return value from the original delegate to an observable sequence. Let me show you how it works.</p><p>Below you can see a part from <em>RxCBCentralManagerDelegateProxy</em>. <strong><em>methodInvoked(selector:)</em></strong><em> </em>has ability to forward delegate method to observable. <strong><em>didDiscoverPeripheral</em></strong><em> </em>returns <em>Observable&lt;Peripheral&gt; </em>by taking parameters of <em>centralManager(_:didDiscover:advertisementData:rssi:).</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/09e0e04ff0c3936d1bb0d1d2bb77f28a/href">https://medium.com/media/09e0e04ff0c3936d1bb0d1d2bb77f28a/href</a></iframe><p>When I try to forward <em>centralManagerDidUpdateState(_:) to an observable, </em>I also had to add delegate implementation in proxy because it is a required delegate method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0fef36159de704d94d0cbe6d6a2d0a30/href">https://medium.com/media/0fef36159de704d94d0cbe6d6a2d0a30/href</a></iframe><p>But I got the following warning.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/940/1*Qjn9ejzp_nI7olxrqwcw6Q.png" /></figure><p>When you implement required delegate methods in your proxy, since it’s already implemented <em>rx_delegate.observe()</em> will not receive any events for that method. That’s why I got the warning. Below you can find the solution for it. <strong><em>_forwardToDelegate </em></strong>clears delegate and that means some of the features that depend on that delegate being set will likely stop working. So we should declare a <em>PublishSubject</em> in our proxy and sent an event to it before calling<em> _forwardToDelegate</em><strong><em> </em></strong>method<strong><em>.</em></strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b6e95a018b2d9288d47238b92b7b801b/href">https://medium.com/media/b6e95a018b2d9288d47238b92b7b801b/href</a></iframe><p>I also create a proxy for <em>CBPeripheral</em>. Below you can see <em>RxCBPeripheralDelegateProxy. </em>As a reminder, if there is another function with the same name and parameter names as the function that you try to get via <em>selector, </em>you should add the function definition to not get “Ambiguous use of function()” compile error.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*d87MzzSM0ng0UtEQFAIGdQ.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/141477cbecc0baf01e65448590c51124/href">https://medium.com/media/141477cbecc0baf01e65448590c51124/href</a></iframe><p>I created <em>BluetoothService</em> to define the Bluetooth-related operations; start/stop scanning, connect to peripheral, get services, get characteristics, notify characteristic, etc. <em>BluetoothService</em> was shared between view models to keep one instance of <em>CBCentralManager</em>. Although Apple says you can have more than one CBCentralManager instance in a single app (<a href="https://developer.apple.com/forums/thread/20810">Here is the link</a>), I also had problems when I used more than one instance in my previous projects. So anyway, I shared BluetoothService between view models to use one instance of the central manager.</p><p>Below you can see <em>startScaning(:)</em> function and usage of it. To start scanning, it is required that central manager state must be <strong><em>poweredOn</em></strong>. That’s why observable of the state is subscribed and if next element is <em>poweredOn</em> scan is started. The result of <em>startScanning(:) </em>is<em> </em>observable sequence of Peripheral and it is used as data source of <strong><em>tableview.rx.items</em></strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c0c0760b4dae8d0a8486294db9543836/href">https://medium.com/media/c0c0760b4dae8d0a8486294db9543836/href</a></iframe><p>Below you can see <em>setNotify</em> function and usage of it. <em>SetNotify </em>returns observable of characteristic value that come from <em>peripheral.rx.didUpdateValue. PeripheralViewModel</em> call <em>setNotify </em>function and pass the heart rate service UUID and characteristic UUID as parameters, and using <strong><em>flatMap() </em></strong>operator it returns an observable of Int value that is used to drive the <em>heartRate</em> BehaviorRelay. In view controller <strong><em>drive()</em></strong> method is used to bind <em>heartRate</em> observable with the UI components. In this way, heart rate text and activity indicator are updated in a reactive way.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac9e3f39338215f830dcda46129b6d0d/href">https://medium.com/media/ac9e3f39338215f830dcda46129b6d0d/href</a></iframe><p>When you try to bind an Observable with a UI element you can use both <em>bindTo</em> and <em>drive</em> methods. <em>drive</em> ensures that observe occurs only on the main thread, so for driving a UI component it is better to use <em>drive</em>.</p><p>I am fairly new to RxSwift. That’s why I tried to mention the errors and warnings I got and how I came up with a solution. You can find more from the <a href="https://github.com/yoktanela/MySmartWatch"><strong>Github repo</strong></a>.</p><p>Here are some screenshots from the application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/891/1*YptzxR8GlUNS2rzvKOyEqg.png" /><figcaption>Display scanned peripherals and display heart rate measurement</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/891/1*rvdveRhNzRwTmBdMN8veIQ.png" /><figcaption>Display step count</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/891/1*6HcoeJ7mIqgfRXH-iTXvNQ.png" /><figcaption>Setting the alarm clock</figcaption></figure><p><strong>And that’s it!</strong></p><p>In this project, I learned to use PacketLogger and how to live track the Bluetooth packets which really helped me to complete this app with lots of features. Implementing this application with RxSwift helped me to get used to this framework. Although I had used CoreBluetooth in previous projects, with RxSwift and DelegateProxy feature, it is easier to handle asynchronous processes and develop dynamic applications that respond to data changes.</p><p>Working with BLE is really fun and interesting. But it also challenging if you don’t have any documentation about the services of the device. I hope this project helps you to get a general understanding of how to dig into developing an app for a BLE device.</p><p>If you have any questions or suggestions please share them with me.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=23f322ae1474" width="1" height="1" alt=""><hr><p><a href="https://medium.com/deliveryherotechhub/ios-smartwatch-app-using-corebluetooth-and-rxswift-23f322ae1474">iOS Smartwatch App using CoreBluetooth and RxSwift</a> was originally published in <a href="https://medium.com/deliveryherotechhub">Delivery Hero Tech Hub</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>