Fixed Bluetooth scan not working on Android 6.0

This commit is contained in:
Felix Ableitner 2016-09-19 16:31:35 +09:00
parent 1d7055f1d6
commit 083c0a2d03
3 changed files with 33 additions and 8 deletions

View file

@ -9,6 +9,7 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-feature android:name="android.hardware.bluetooth" android:required="false" />

View file

@ -18,6 +18,8 @@
<!-- Label for button to finish activity -->
<string name="done">Done</string>
<!-- Toast shown if location permission is not granted -->
<string name="toast_location_required">Location permission is required to scan for other Bluetooth devices</string>
<!-- MainActivity -->

View file

@ -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 = {