{"id":6019,"date":"2021-01-20T11:46:52","date_gmt":"2021-01-20T11:46:52","guid":{"rendered":"https:\/\/www.beaconzone.co.uk\/blog\/?p=6019"},"modified":"2021-01-20T12:07:22","modified_gmt":"2021-01-20T12:07:22","slug":"reactive-bluetooth-programming","status":"publish","type":"post","link":"https:\/\/www.beaconzone.co.uk\/blog\/reactive-bluetooth-programming\/","title":{"rendered":"Reactive Bluetooth Programming"},"content":{"rendered":"\n<p>Connecting programatically to a Bluetooth device involves the following stages:<\/p>\n\n\n\n<ol><li>Scanning for peripherals&#8217; <a rel=\"noreferrer noopener\" href=\"https:\/\/bluetoothle.wiki\/advertising\" target=\"_blank\">advertising<\/a><\/li><li>Connecting to the peripheral via <a rel=\"noreferrer noopener\" href=\"https:\/\/bluetoothle.wiki\/gatt\" target=\"_blank\">GATT<\/a> <\/li><li>Discovering the peripheral&#8217;s services<\/li><li>Discovering the peripheral&#8217;s characteristics<\/li><li>Reading and writing to a characteristic&#8217;s value and\/or monitoring characteristic value change via a Notify<\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"441\" src=\"https:\/\/www.beaconzone.co.uk\/blog\/wp-content\/uploads\/2021\/01\/bluetoothle.png\" alt=\"\" class=\"wp-image-6031\" srcset=\"https:\/\/www.beaconzone.co.uk\/blog\/wp-content\/uploads\/2021\/01\/bluetoothle.png 739w, https:\/\/www.beaconzone.co.uk\/blog\/wp-content\/uploads\/2021\/01\/bluetoothle-300x179.png 300w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><\/figure>\n\n\n\n<p>Each of these stages is asynchronous because each takes a relatively long time in computing terms. This means the code needs to call something but continue running to remain responsive to other events and later process the result of a stage via a callback from the Bluetooth library. The connection isn&#8217;t reliable because it&#8217;s wireless. Different kinds of failure, also notified via callbacks, require the code to go back one or more stages depending on the severity of the error.<\/p>\n\n\n\n<p>All this gets very messy, confusing and difficult to understand in the resultant code. Reactive (Rx) programming attempts to solve such asynchronous complexity problems by using the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Observer_pattern\" target=\"_blank\" rel=\"noreferrer noopener\">Observable Pattern<\/a> to broadcast and subscribe to values and other events from an Observable stream.<\/p>\n\n\n\n<p>There are Reactive implementations such as <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/ReactiveX\/RxSwift\" target=\"_blank\">RxSwift<\/a>  on iOS and <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/ReactiveX\/RxJava\" target=\"_blank\">RxJava<\/a>  on Android. There are also Bluetooth specific libraries such as <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/Polidea\/RxBluetoothKit\" target=\"_blank\">RxBluetoothKit<\/a> for iOS\/OSX and <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/Polidea\/RxAndroidBle\" target=\"_blank\">RxAndroidBle<\/a> that make asynchronous Bluetooth code in Swift\/Java much easier to understand and maintain.<\/p>\n\n\n\n<p>Reactive programming used to be very popular not just for asynchronous programming but also for general Android programming. It has fallen out use for general programming mainly due to Kotlin which is now the \u2018latest thing\u2019. <\/p>\n\n\n\n<p>Naive developers have a tendency to want to use the &#8216;latest thing&#8217; or &#8216;clever techniques&#8217; while experienced developers choose the right tool for the job. A common error is to over-use and combine design patterns, such as observables, in simple scenarios, which hinders rather than simplifies understanding.<\/p>\n\n\n\n<p>The nature and relative complexity of your project should determine whether it&#8217;s worth using Reactive code. It&#8217;s not necessary an &#8216;all or nothing&#8217; decision. It&#8217;s possible to choose to use observable pattern techniques only in parts of code with extreme asynchronous complexity rather than in all the code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Connecting programatically to a Bluetooth device involves the following stages: Scanning for peripherals&#8217; advertising Connecting to the peripheral via GATT Discovering the peripheral&#8217;s services Discovering the peripheral&#8217;s characteristics Reading and writing to a characteristic&#8217;s value and\/or monitoring characteristic value change via a Notify Each of these stages is asynchronous because each takes a relatively long &hellip; <a href=\"https:\/\/www.beaconzone.co.uk\/blog\/reactive-bluetooth-programming\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Reactive Bluetooth Programming&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28,27],"tags":[],"_links":{"self":[{"href":"https:\/\/www.beaconzone.co.uk\/blog\/wp-json\/wp\/v2\/posts\/6019"}],"collection":[{"href":"https:\/\/www.beaconzone.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.beaconzone.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.beaconzone.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.beaconzone.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=6019"}],"version-history":[{"count":12,"href":"https:\/\/www.beaconzone.co.uk\/blog\/wp-json\/wp\/v2\/posts\/6019\/revisions"}],"predecessor-version":[{"id":6035,"href":"https:\/\/www.beaconzone.co.uk\/blog\/wp-json\/wp\/v2\/posts\/6019\/revisions\/6035"}],"wp:attachment":[{"href":"https:\/\/www.beaconzone.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=6019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.beaconzone.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=6019"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.beaconzone.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=6019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}