diff --git a/app/src/main/java/xyz/mtfos/btdemo/MainActivity.kt b/app/src/main/java/xyz/mtfos/btdemo/MainActivity.kt index 3f26e53..682815f 100644 --- a/app/src/main/java/xyz/mtfos/btdemo/MainActivity.kt +++ b/app/src/main/java/xyz/mtfos/btdemo/MainActivity.kt @@ -1,6 +1,10 @@ package xyz.mtfos.btdemo +import android.Manifest +import android.annotation.SuppressLint import android.app.Activity +import android.content.pm.PackageManager +import android.os.Build import android.os.Bundle import android.os.Handler import android.widget.Button @@ -31,6 +35,15 @@ class MainActivity : Activity() { ble = BLECls(this@MainActivity) + var permissionCheck: Int = 0 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + permissionCheck += this@MainActivity.checkSelfPermission(android.Manifest.permission.ACCESS_COARSE_LOCATION) + permissionCheck += this@MainActivity.checkSelfPermission(android.Manifest.permission.ACCESS_FINE_LOCATION) + if (permissionCheck != 0) { + requestPermissions(arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION), 111) + } + } + btn.setOnClickListener { val txt: String = intxt.text.toString() ble?.sendLongData(txt.toByteArray()) @@ -44,12 +57,16 @@ class MainActivity : Activity() { } } + override fun onRequestPermissionsResult(requestCode: Int, permissions: Array?, grantResults: IntArray?) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults) + } + override fun onStop() { super.onStop() disconnect() } - fun scanConnect(){ + fun scanConnect() { if (ble?.init()!!) { isInit = true ble?.startScan() @@ -84,5 +101,7 @@ class MainActivity : Activity() { } disconnectBtn.isEnabled = false connectBtn.isEnabled = true + btn.isEnabled = false + isInit = false } } \ No newline at end of file