From 083c0a2d0347665c63a894cce29cff24158d1f01 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 19 Sep 2016 16:31:35 +0900 Subject: [PATCH] Fixed Bluetooth scan not working on Android 6.0 --- android/src/main/AndroidManifest.xml | 1 + android/src/main/res/values/strings.xml | 2 + .../activities/FirstStartActivity.scala | 38 +++++++++++++++---- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml index fcf3439..42a3e70 100644 --- a/android/src/main/AndroidManifest.xml +++ b/android/src/main/AndroidManifest.xml @@ -9,6 +9,7 @@ + diff --git a/android/src/main/res/values/strings.xml b/android/src/main/res/values/strings.xml index 40f7caa..4acf7d8 100644 --- a/android/src/main/res/values/strings.xml +++ b/android/src/main/res/values/strings.xml @@ -18,6 +18,8 @@ Done + + Location permission is required to scan for other Bluetooth devices diff --git a/android/src/main/scala/com/nutomic/ensichat/activities/FirstStartActivity.scala b/android/src/main/scala/com/nutomic/ensichat/activities/FirstStartActivity.scala index 5bec7ce..2ad7329 100644 --- a/android/src/main/scala/com/nutomic/ensichat/activities/FirstStartActivity.scala +++ b/android/src/main/scala/com/nutomic/ensichat/activities/FirstStartActivity.scala @@ -1,15 +1,19 @@ package com.nutomic.ensichat.activities +import android.Manifest import android.bluetooth.BluetoothAdapter +import android.content.pm.PackageManager import android.content.{Context, Intent} import android.os.Bundle import android.preference.PreferenceManager +import android.support.v4.app.ActivityCompat +import android.support.v4.content.ContextCompat import android.support.v7.app.AppCompatActivity import android.view.View.OnClickListener import android.view.inputmethod.{EditorInfo, InputMethodManager} import android.view.{KeyEvent, View} import android.widget.TextView.OnEditorActionListener -import android.widget.{Button, EditText, TextView} +import android.widget.{Button, EditText, TextView, Toast} import com.nutomic.ensichat.R import com.nutomic.ensichat.core.interfaces.SettingsInterface import com.nutomic.ensichat.core.interfaces.SettingsInterface._ @@ -20,6 +24,7 @@ import com.nutomic.ensichat.core.interfaces.SettingsInterface._ class FirstStartActivity extends AppCompatActivity with OnEditorActionListener with OnClickListener { private val KeyIsFirstStart = "first_start" + private val RequestLocationPermission = 127 private lazy val preferences = PreferenceManager.getDefaultSharedPreferences(this) private lazy val imm = getSystemService(Context.INPUT_METHOD_SERVICE) @@ -31,11 +36,6 @@ class FirstStartActivity extends AppCompatActivity with OnEditorActionListener w override def onCreate(savedInstanceState: Bundle): Unit = { super.onCreate(savedInstanceState) - if (!preferences.getBoolean(KeyIsFirstStart, true)) { - startMainActivity() - return - } - setContentView(R.layout.activity_first_start) setTitle(R.string.welcome) @@ -43,7 +43,16 @@ class FirstStartActivity extends AppCompatActivity with OnEditorActionListener w username.setOnEditorActionListener(this) done.setOnClickListener(this) - imm.showSoftInput(username, InputMethodManager.SHOW_IMPLICIT) + val permission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) + if (preferences.getBoolean(KeyIsFirstStart, true)) { + imm.showSoftInput(username, InputMethodManager.SHOW_IMPLICIT) + } + else if (permission != PackageManager.PERMISSION_GRANTED) { + requestLocationPermission() + } + else { + startMainActivity() + } } /** @@ -77,7 +86,20 @@ class FirstStartActivity extends AppCompatActivity with OnEditorActionListener w .putString(KeyAddresses, DefaultAddresses) .apply() - startMainActivity() + requestLocationPermission() + } + + private def requestLocationPermission(): Unit = + ActivityCompat.requestPermissions(this, Array(Manifest.permission.ACCESS_COARSE_LOCATION), RequestLocationPermission) + + override def onRequestPermissionsResult(requestCode: Int, + permissions: Array[String], grantResults: Array[Int]): Unit = requestCode match { + case RequestLocationPermission => + if (grantResults.length > 0 && grantResults(0) == PackageManager.PERMISSION_GRANTED) { + startMainActivity() + } else { + Toast.makeText(this, R.string.toast_location_required, Toast.LENGTH_SHORT).show() + } } def startMainActivity(): Unit = {