{"id":121,"date":"2020-08-02T09:32:59","date_gmt":"2020-08-02T07:32:59","guid":{"rendered":"http:\/\/www.schmidt-welt.net\/weblog\/?p=121"},"modified":"2020-08-02T09:32:59","modified_gmt":"2020-08-02T07:32:59","slug":"check-permissions-before-your-android-app-uses-ble","status":"publish","type":"post","link":"https:\/\/www.schmidt-welt.net\/weblog\/check-permissions-before-your-android-app-uses-ble\/","title":{"rendered":"Check permissions before your Android app uses BLE"},"content":{"rendered":"\n<p>If you want to implement an android app that use BLE,\u00c2\u00a0 the app must get the <strong><em>ACCESS_COARSE_LOCATION<\/em><\/strong> permissions. Android will then automatically ask the user if he\/she agrees when using a bluetooth function for the first time.(for example at startScan). But if the app has no permissions to use the location, your bluetooth call will fail. Therefore, an early check is better to avoid this problem.\u00c2\u00a0<\/p>\n\n\n\n<p>My approach looks like this:<\/p>\n\n\n\n<p>1) Add to your AndroidManifest.xml:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u00c2\u00a0&lt;uses-permission android:name=\"android.permission.ACCESS_COARSE_LOCATION\" \/><\/pre>\n\n\n\n<p>If you only use BLE it es sufficient to request the coarse location permission (location without GPS). If you need a GPS position for your app, you can replace <strong><em>ACCESS_COARSE_LOCATION<\/em><\/strong> with <strong><em>ACCESS_FINE_LOCATION<\/em><\/strong>. You don&#8217;t need both.<\/p>\n\n\n\n<p>2) Then add the permission check to place before the first called bluetooth method.\u00c2\u00a0 For example directly after starting the app.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">private boolean hasLocationPermission() {\n\n  if (ContextCompat.checkSelfPermission(\n    context, \n    Manifest.permission.ACCESS_COARSE_LOCATION\n    ) != PackageManager.PERMISSION_GRANTED) {\n\n    ActivityCompat.requestPermissions(\n      getCurrentActivity(),\n      new String[] {\n        Manifest.permission.ACCESS_COARSE_LOCATION\n      },\n      REQUEST_COARSE_LOCATION_PERMISSIONS);\n    return false;\n  } else {\n<em>    doWhatEverYouWillDoWithBluetooth()<\/em>;\n    return true;\n  }\n}<\/pre>\n\n\n\n<p>3) Finally, the callback must be implemented in your activity class to react to the user&#8217;s decision<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public void onRequestPermissionsResult(\n  int requestCode, \n  String permissions[], \n  int[] grantResults) {\n\n  if (requestCode == REQUEST_COARSE_LOCATION_PERMISSIONS) {\n    if (grantResults.length > 0 &amp;&amp; grantResults[0] ==  \n      PackageManager.PERMISSION_GRANTED) {\n      \n<em>      doWhatEverYouWillDoWithBluetooth();<\/em>\n    } else {\n<em>      doWhatEverYouWillDoWithoutBluetooth();<\/em>\n    }\n  }\n}<\/pre>\n\n\n\n<p>That&#8217;s it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you want to implement an android app that use BLE,\u00c2\u00a0 the app must get the ACCESS_COARSE_LOCATION permissions. Android will then automatically ask the user if he\/she agrees when using a bluetooth function for the first time.(for example at startScan). But if the app has no permissions to use the location, your bluetooth call will [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[10,11],"class_list":["post-121","post","type-post","status-publish","format-standard","hentry","category-allgemein","tag-android","tag-bluetooth"],"_links":{"self":[{"href":"https:\/\/www.schmidt-welt.net\/weblog\/wp-json\/wp\/v2\/posts\/121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.schmidt-welt.net\/weblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.schmidt-welt.net\/weblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.schmidt-welt.net\/weblog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.schmidt-welt.net\/weblog\/wp-json\/wp\/v2\/comments?post=121"}],"version-history":[{"count":1,"href":"https:\/\/www.schmidt-welt.net\/weblog\/wp-json\/wp\/v2\/posts\/121\/revisions"}],"predecessor-version":[{"id":122,"href":"https:\/\/www.schmidt-welt.net\/weblog\/wp-json\/wp\/v2\/posts\/121\/revisions\/122"}],"wp:attachment":[{"href":"https:\/\/www.schmidt-welt.net\/weblog\/wp-json\/wp\/v2\/media?parent=121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.schmidt-welt.net\/weblog\/wp-json\/wp\/v2\/categories?post=121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.schmidt-welt.net\/weblog\/wp-json\/wp\/v2\/tags?post=121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}