@@ -8,9 +8,15 @@ package com.livio.mobilenav
88// Copyright © 2021 Ford. All rights reserved.
99//
1010
11+ import android.Manifest
1112import android.content.Intent
13+ import android.content.pm.PackageManager
14+ import android.os.Build
1215import android.os.Bundle
16+ import android.widget.Toast
17+ import androidx.annotation.RequiresApi
1318import androidx.appcompat.app.AppCompatActivity
19+ import androidx.core.app.ActivityCompat
1420import com.mapbox.android.core.location.LocationEngineProvider
1521import com.mapbox.search.MapboxSearchSdk
1622import java.lang.ref.WeakReference
@@ -19,6 +25,7 @@ class MainActivity : AppCompatActivity() {
1925
2026 companion object {
2127 private var instance: WeakReference <MainActivity >? = null
28+ const val BLUETOOTH_REQUEST_CODE = 200
2229
2330 fun getNewestInstance (): WeakReference <MainActivity >? {
2431 return instance
@@ -40,8 +47,17 @@ class MainActivity : AppCompatActivity() {
4047 catch (exception: IllegalStateException ) {}
4148
4249 setContentView(R .layout.activity_main)
50+
4351 // If we are connected to a module we want to start our SdlService
4452 if (BuildConfig .TRANSPORT == " MULTI" || BuildConfig .TRANSPORT == " MULTI_HB" ) {
53+
54+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
55+ if (! checkBluetoothPermission()) {
56+ requestBluetoothPermission()
57+ return
58+ }
59+ }
60+
4561 SdlReceiver .queryForConnectedService(this )
4662 } else if (BuildConfig .TRANSPORT == " TCP" ) {
4763 val proxyIntent = Intent (this , SdlService ::class .java)
@@ -50,5 +66,44 @@ class MainActivity : AppCompatActivity() {
5066
5167 }
5268
69+ @RequiresApi(Build .VERSION_CODES .S )
70+ private fun checkBluetoothPermission (): Boolean {
71+ val btConnectPermission =
72+ ActivityCompat .checkSelfPermission(applicationContext, Manifest .permission.BLUETOOTH_CONNECT )
73+ return btConnectPermission == PackageManager .PERMISSION_GRANTED
74+ }
75+
76+ @RequiresApi(Build .VERSION_CODES .S )
77+ private fun requestBluetoothPermission () {
78+ ActivityCompat .requestPermissions(
79+ this ,
80+ arrayOf(Manifest .permission.BLUETOOTH_CONNECT ),
81+ BLUETOOTH_REQUEST_CODE
82+ )
83+ }
84+
85+ override fun onRequestPermissionsResult (
86+ requestCode : Int ,
87+ permissions : Array <String ?>,
88+ grantResults : IntArray
89+ ) {
90+ super .onRequestPermissionsResult(requestCode, permissions, grantResults)
91+ when (requestCode) {
92+ BLUETOOTH_REQUEST_CODE -> if (grantResults.isNotEmpty()) {
93+ val connectAccepted = grantResults[0 ] == PackageManager .PERMISSION_GRANTED
94+ if (! connectAccepted) {
95+ Toast .makeText(
96+ this ,
97+ " BLUETOOTH_CONNECT Permission is needed for Bluetooth testing" ,
98+ Toast .LENGTH_LONG
99+ ).show()
100+ }
101+ else {
102+ SdlReceiver .queryForConnectedService(this )
103+ }
104+ }
105+ }
106+ }
107+
53108
54109}
0 commit comments