{"id":496,"date":"2021-12-20T12:49:52","date_gmt":"2021-12-20T11:49:52","guid":{"rendered":"http:\/\/www.bellingo.de\/blog\/?p=496"},"modified":"2021-12-20T13:04:53","modified_gmt":"2021-12-20T12:04:53","slug":"devblog-a-bluetooth-novices-tale","status":"publish","type":"post","link":"https:\/\/www.bellingo.de\/blog\/devblog-a-bluetooth-novices-tale\/","title":{"rendered":"DevBlog: A Bluetooth Novice&#8217;s Tale"},"content":{"rendered":"\n<p>Once upon a time not so long ago there was a not-so-young programmer. He thought, &#8222;let&#8217;s make <a href=\"https:\/\/www.bellingo.de\/blog\/devblog-restart\/\">a game<\/a> where people connect their smartphones via Bluetooth to trade with each other&#8220;. And so he went on a perilous adventure with many loops and turns where in the end, the simplest solution won out. <\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Have you ever tried connecting two Android devices with Bluetooth? It can be done, obviously, but it&#8217;s not as easy as you may think. First off, Unity does not offer any support for Bluetooth natively, of course. So I started by browsing the asset store. There are a couple of packages for different application scenarios such as coupling all kinds of devices with different kinds of Bluetooth. For of course, there is not just one Bluetooth, but there is <a href=\"https:\/\/en.wikipedia.org\/wiki\/Bluetooth\">classic Bluetooth<\/a> and <a href=\"https:\/\/en.wikipedia.org\/wiki\/Bluetooth_Low_Energy\">Bluetooth Low Energy<\/a> (BLE).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Bluetooth Quest<\/h2>\n\n\n\n<p>I did not know that Bluetooth LE existed before, so after a little bit of reading I decided to give it a try. BLE has the advantage that devices broadcast their data to anyone in the vicinity and coupling devices is not necessary. On the other hand, bandwidth is very low and security may be an issue. Implementing a first connection using an asset store package was done fairly quickly. <\/p>\n\n\n\n<p>But then I had to realise that bandwidth was just <em>too<\/em> low for my purposes. A BLE advertising package only has <a href=\"https:\/\/docs.silabs.com\/bluetooth\/2.13\/general\/adv-and-scanning\/bluetooth-adv-data-basics\">31 bytes of payload<\/a>, including the device name and some other data. What was left was not enough for even a single Guid! Establishing a BLE client-server connection and sending several packages worked, kind of, but exchanging any significant amount of data was out of the question. Well, I know there is more to BLE, such as <a href=\"https:\/\/www.bluetooth.com\/blog\/exploring-bluetooth5-whats-new-in-advertising\/\">secondary advertising channels<\/a>, but I consider these the highest levels of the Bluetooth tower of wisdom and never ventured there.<\/p>\n\n\n\n<p>So I decided that BLE would only be used for determining what other players were around, and the real data would be transmitted via an extra Bluetooth classic connection. But here, the real perils were already waiting for me. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Classic Perils<\/h2>\n\n\n\n<p>As usual, I tried to save some money and not buy another package for Unity, but to implement the Android Bluetooth adapter myself. A foolish notion, as it turned out. <\/p>\n\n\n\n<p>There is an <a href=\"https:\/\/github.com\/android\/connectivity-samples\/tree\/master\">Android example<\/a> implementation of a simple Bluetooth chat application you can download from GitHub. Additionally, there is a <a href=\"https:\/\/github.com\/seiji\/unity-bluetooth\">Unity Bluetooth plugin<\/a> available on GitHub, too. I used these as a basis for my own interface, tweaking and adapting them for my own needs. It took me a few weeks in August, but I got it running. To a certain point, at least.<\/p>\n\n\n\n<p>The workflow now was as follows: connect two devices via Bluetooth LE by setting up a master-slave connection. Pass information about the other player, such as his ID and name, in several messages. A connect button is shown, which initiates a Bluetooth Classic connection when pressed. This is used to exchange the bulk of data necessary for playing.<\/p>\n\n\n\n<p>This worked, most of the time, for my own devices, but not always. Sometimes, I had to restart my app, sometimes the bluetooth interface. But things got worse the first time I asked a neighbour to try my game. It was impossible to get a connection with his device. I found numerous tales on what could be the reason for devices not connecting, such as that you should only have <a href=\"https:\/\/stackoverflow.com\/questions\/18657427\/ioexception-read-failed-socket-might-closed-bluetooth-on-android-4-3?rq=1\">one paired device at a time<\/a>. It is, of course, no feasible solution to tell players to unpair all their devices &#8211; and that was not the problem anyway. So in the end, I yielded.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Tower of Bluetooth Wisdom<\/h2>\n\n\n\n<p>The <a href=\"https:\/\/www.bluetooth.com\/specifications\/specs\/core-specification\/\">Bluetooth Core Specification<\/a> is a tome of 3000 pages, written in a language only the wise ones can understand. It can do incredible things, but it is not for the untrained novice. I entered this tower of wisdom and ventured up a few levels, but in the end, I decided to just stay in the entrance hall and gawk at its wonders. The lesson is that many times, the simplest solutions are the best.<\/p>\n\n\n\n<p>So what did I do? Now, I only send one advertising package via Bluetooth LE over which devices make themselves known. This package contains an identifier that is sent to a internet server, which identifies nearby players. The connection is then established via this server and data is exchanged over the internet. It&#8217;s as simple as that.<\/p>\n\n\n\n<p>By the way, I ended up using a Bluetooth LE <a href=\"https:\/\/assetstore.unity.com\/packages\/tools\/network\/bluetooth-networking-for-ios-tvos-and-android-124274\">Unity asset<\/a> by <a href=\"http:\/\/www.shatalmic.com\/\">Shatalmic, llc<\/a>. I had a couple of questions which were always answered very quickly by its maintainer. So if you want to start with BLE despite my story, give this library a try.<\/p>\n\n\n\n<p>What are your experiences with Bluetooth, especially in the context of gaming and in Unity? Is there a game out there which actually makes use of Bluetooth? Tell me in the comments below! Or subscribe to my <a href=\"https:\/\/twitter.com\/GamesVineyard\" target=\"_blank\" rel=\"noreferrer noopener\">Twitter<\/a> account or <a href=\"https:\/\/www.bellingo.de\/blog\/language\/en\/feed\/\">RSS feed<\/a> for more news on Network Traders.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Once upon a time not so long ago there was a not-so-young programmer. He thought, &#8222;let&#8217;s make a game where people connect their smartphones via Bluetooth to trade with each other&#8220;. And so he went on a perilous adventure with many loops and turns where in the end, the simplest solution won out.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[229,49,55],"tags":[234,230,231,67],"class_list":["post-496","post","type-post","status-publish","format-standard","hentry","category-devblog","category-game-development","category-unity-en","tag-bluetooth","tag-devblog","tag-network-traders","tag-unity-en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>DevBlog: A Bluetooth Novice&#039;s Tale - Games:Tech:Blog<\/title>\n<meta name=\"description\" content=\"This is about my quest for connecting two Android devices via Bluetooth for Network Traders, which did not end as intended.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.bellingo.de\/blog\/devblog-a-bluetooth-novices-tale\/\" \/>\n<meta property=\"og:locale\" content=\"de_DE\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"DevBlog: A Bluetooth Novice&#039;s Tale - Games:Tech:Blog\" \/>\n<meta property=\"og:description\" content=\"This is about my quest for connecting two Android devices via Bluetooth for Network Traders, which did not end as intended.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.bellingo.de\/blog\/devblog-a-bluetooth-novices-tale\/\" \/>\n<meta property=\"og:site_name\" content=\"Games:Tech:Blog\" \/>\n<meta property=\"article:published_time\" content=\"2021-12-20T11:49:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-20T12:04:53+00:00\" \/>\n<meta name=\"author\" content=\"Ingo\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Verfasst von\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ingo\" \/>\n\t<meta name=\"twitter:label2\" content=\"Gesch\u00e4tzte Lesezeit\" \/>\n\t<meta name=\"twitter:data2\" content=\"5\u00a0Minuten\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.bellingo.de\/blog\/devblog-a-bluetooth-novices-tale\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.bellingo.de\/blog\/devblog-a-bluetooth-novices-tale\/\"},\"author\":{\"name\":\"Ingo\",\"@id\":\"https:\/\/www.bellingo.de\/blog\/#\/schema\/person\/82498f8ac21d11f5948cab6d0a9807e9\"},\"headline\":\"DevBlog: A Bluetooth Novice&#8217;s Tale\",\"datePublished\":\"2021-12-20T11:49:52+00:00\",\"dateModified\":\"2021-12-20T12:04:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.bellingo.de\/blog\/devblog-a-bluetooth-novices-tale\/\"},\"wordCount\":843,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.bellingo.de\/blog\/#\/schema\/person\/82498f8ac21d11f5948cab6d0a9807e9\"},\"keywords\":[\"Bluetooth\",\"DevBlog\",\"Network Traders\",\"Unity\"],\"articleSection\":[\"DevBlog\",\"Game development\",\"Unity\"],\"inLanguage\":\"de\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.bellingo.de\/blog\/devblog-a-bluetooth-novices-tale\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.bellingo.de\/blog\/devblog-a-bluetooth-novices-tale\/\",\"url\":\"https:\/\/www.bellingo.de\/blog\/devblog-a-bluetooth-novices-tale\/\",\"name\":\"DevBlog: A Bluetooth Novice's Tale - Games:Tech:Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.bellingo.de\/blog\/#website\"},\"datePublished\":\"2021-12-20T11:49:52+00:00\",\"dateModified\":\"2021-12-20T12:04:53+00:00\",\"description\":\"This is about my quest for connecting two Android devices via Bluetooth for Network Traders, which did not end as intended.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.bellingo.de\/blog\/devblog-a-bluetooth-novices-tale\/#breadcrumb\"},\"inLanguage\":\"de\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.bellingo.de\/blog\/devblog-a-bluetooth-novices-tale\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.bellingo.de\/blog\/devblog-a-bluetooth-novices-tale\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.bellingo.de\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"DevBlog: A Bluetooth Novice&#8217;s Tale\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.bellingo.de\/blog\/#website\",\"url\":\"https:\/\/www.bellingo.de\/blog\/\",\"name\":\"Games:Tech:Blog\",\"description\":\"A Blog about Games, Technology in Games, and Technology in General\",\"publisher\":{\"@id\":\"https:\/\/www.bellingo.de\/blog\/#\/schema\/person\/82498f8ac21d11f5948cab6d0a9807e9\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.bellingo.de\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"de\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.bellingo.de\/blog\/#\/schema\/person\/82498f8ac21d11f5948cab6d0a9807e9\",\"name\":\"Ingo\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"de\",\"@id\":\"https:\/\/www.bellingo.de\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/93a4b9881ee1983f4fafc7e996ce82a42c3a1540c18f7417809c08a19658f167?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/93a4b9881ee1983f4fafc7e996ce82a42c3a1540c18f7417809c08a19658f167?s=96&d=mm&r=g\",\"caption\":\"Ingo\"},\"logo\":{\"@id\":\"https:\/\/www.bellingo.de\/blog\/#\/schema\/person\/image\/\"},\"url\":\"https:\/\/www.bellingo.de\/blog\/author\/ingo\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"DevBlog: A Bluetooth Novice's Tale - Games:Tech:Blog","description":"This is about my quest for connecting two Android devices via Bluetooth for Network Traders, which did not end as intended.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.bellingo.de\/blog\/devblog-a-bluetooth-novices-tale\/","og_locale":"de_DE","og_type":"article","og_title":"DevBlog: A Bluetooth Novice's Tale - Games:Tech:Blog","og_description":"This is about my quest for connecting two Android devices via Bluetooth for Network Traders, which did not end as intended.","og_url":"https:\/\/www.bellingo.de\/blog\/devblog-a-bluetooth-novices-tale\/","og_site_name":"Games:Tech:Blog","article_published_time":"2021-12-20T11:49:52+00:00","article_modified_time":"2021-12-20T12:04:53+00:00","author":"Ingo","twitter_card":"summary_large_image","twitter_misc":{"Verfasst von":"Ingo","Gesch\u00e4tzte Lesezeit":"5\u00a0Minuten"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.bellingo.de\/blog\/devblog-a-bluetooth-novices-tale\/#article","isPartOf":{"@id":"https:\/\/www.bellingo.de\/blog\/devblog-a-bluetooth-novices-tale\/"},"author":{"name":"Ingo","@id":"https:\/\/www.bellingo.de\/blog\/#\/schema\/person\/82498f8ac21d11f5948cab6d0a9807e9"},"headline":"DevBlog: A Bluetooth Novice&#8217;s Tale","datePublished":"2021-12-20T11:49:52+00:00","dateModified":"2021-12-20T12:04:53+00:00","mainEntityOfPage":{"@id":"https:\/\/www.bellingo.de\/blog\/devblog-a-bluetooth-novices-tale\/"},"wordCount":843,"commentCount":0,"publisher":{"@id":"https:\/\/www.bellingo.de\/blog\/#\/schema\/person\/82498f8ac21d11f5948cab6d0a9807e9"},"keywords":["Bluetooth","DevBlog","Network Traders","Unity"],"articleSection":["DevBlog","Game development","Unity"],"inLanguage":"de","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.bellingo.de\/blog\/devblog-a-bluetooth-novices-tale\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.bellingo.de\/blog\/devblog-a-bluetooth-novices-tale\/","url":"https:\/\/www.bellingo.de\/blog\/devblog-a-bluetooth-novices-tale\/","name":"DevBlog: A Bluetooth Novice's Tale - Games:Tech:Blog","isPartOf":{"@id":"https:\/\/www.bellingo.de\/blog\/#website"},"datePublished":"2021-12-20T11:49:52+00:00","dateModified":"2021-12-20T12:04:53+00:00","description":"This is about my quest for connecting two Android devices via Bluetooth for Network Traders, which did not end as intended.","breadcrumb":{"@id":"https:\/\/www.bellingo.de\/blog\/devblog-a-bluetooth-novices-tale\/#breadcrumb"},"inLanguage":"de","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.bellingo.de\/blog\/devblog-a-bluetooth-novices-tale\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.bellingo.de\/blog\/devblog-a-bluetooth-novices-tale\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.bellingo.de\/blog\/"},{"@type":"ListItem","position":2,"name":"DevBlog: A Bluetooth Novice&#8217;s Tale"}]},{"@type":"WebSite","@id":"https:\/\/www.bellingo.de\/blog\/#website","url":"https:\/\/www.bellingo.de\/blog\/","name":"Games:Tech:Blog","description":"A Blog about Games, Technology in Games, and Technology in General","publisher":{"@id":"https:\/\/www.bellingo.de\/blog\/#\/schema\/person\/82498f8ac21d11f5948cab6d0a9807e9"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.bellingo.de\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"de"},{"@type":["Person","Organization"],"@id":"https:\/\/www.bellingo.de\/blog\/#\/schema\/person\/82498f8ac21d11f5948cab6d0a9807e9","name":"Ingo","image":{"@type":"ImageObject","inLanguage":"de","@id":"https:\/\/www.bellingo.de\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/93a4b9881ee1983f4fafc7e996ce82a42c3a1540c18f7417809c08a19658f167?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/93a4b9881ee1983f4fafc7e996ce82a42c3a1540c18f7417809c08a19658f167?s=96&d=mm&r=g","caption":"Ingo"},"logo":{"@id":"https:\/\/www.bellingo.de\/blog\/#\/schema\/person\/image\/"},"url":"https:\/\/www.bellingo.de\/blog\/author\/ingo\/"}]}},"_links":{"self":[{"href":"https:\/\/www.bellingo.de\/blog\/wp-json\/wp\/v2\/posts\/496","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bellingo.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bellingo.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bellingo.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bellingo.de\/blog\/wp-json\/wp\/v2\/comments?post=496"}],"version-history":[{"count":9,"href":"https:\/\/www.bellingo.de\/blog\/wp-json\/wp\/v2\/posts\/496\/revisions"}],"predecessor-version":[{"id":507,"href":"https:\/\/www.bellingo.de\/blog\/wp-json\/wp\/v2\/posts\/496\/revisions\/507"}],"wp:attachment":[{"href":"https:\/\/www.bellingo.de\/blog\/wp-json\/wp\/v2\/media?parent=496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bellingo.de\/blog\/wp-json\/wp\/v2\/categories?post=496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bellingo.de\/blog\/wp-json\/wp\/v2\/tags?post=496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}