kopia lustrzana https://github.com/meshtastic/Meshtastic-Android
we now request perms
rodzic
1c942a10b3
commit
f48ce97d5e
|
@ -9,7 +9,7 @@ android {
|
||||||
buildToolsVersion "29.0.2"
|
buildToolsVersion "29.0.2"
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
applicationId "com.geeksville.meshutil"
|
applicationId "com.geeksville.meshutil"
|
||||||
minSdkVersion 18
|
minSdkVersion 21
|
||||||
targetSdkVersion 29
|
targetSdkVersion 29
|
||||||
versionCode 1
|
versionCode 1
|
||||||
versionName "1.0"
|
versionName "1.0"
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
assets/firmware.bin
|
|
@ -8,6 +8,7 @@
|
||||||
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
|
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- only useful if this phone can do BTLE -->
|
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- only useful if this phone can do BTLE -->
|
||||||
|
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" /> <!-- needed to access bluetooth when app is background -->
|
||||||
|
|
||||||
<uses-permission android:name="android.permission.WAKE_LOCK" /> <!-- for job intent service -->
|
<uses-permission android:name="android.permission.WAKE_LOCK" /> <!-- for job intent service -->
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
package com.geeksville.meshutil
|
package com.geeksville.meshutil
|
||||||
|
|
||||||
|
import android.Manifest
|
||||||
import android.bluetooth.*
|
import android.bluetooth.*
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
|
import android.content.pm.PackageManager
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
@ -10,6 +12,8 @@ import com.google.android.material.snackbar.Snackbar
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
|
import androidx.core.app.ActivityCompat
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
|
|
||||||
import kotlinx.android.synthetic.main.activity_main.*
|
import kotlinx.android.synthetic.main.activity_main.*
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
@ -19,6 +23,7 @@ class MainActivity : AppCompatActivity() {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val REQUEST_ENABLE_BT = 10
|
const val REQUEST_ENABLE_BT = 10
|
||||||
|
const val DID_REQUEST_PERM = 11
|
||||||
}
|
}
|
||||||
|
|
||||||
private val bluetoothAdapter: BluetoothAdapter by lazy(LazyThreadSafetyMode.NONE) {
|
private val bluetoothAdapter: BluetoothAdapter by lazy(LazyThreadSafetyMode.NONE) {
|
||||||
|
@ -26,6 +31,38 @@ class MainActivity : AppCompatActivity() {
|
||||||
bluetoothManager.adapter!!
|
bluetoothManager.adapter!!
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun requestPermission() {
|
||||||
|
val perms = arrayOf(Manifest.permission.ACCESS_FINE_LOCATION,
|
||||||
|
Manifest.permission.ACCESS_BACKGROUND_LOCATION,
|
||||||
|
Manifest.permission.BLUETOOTH,
|
||||||
|
Manifest.permission.BLUETOOTH_ADMIN,
|
||||||
|
Manifest.permission.WAKE_LOCK)
|
||||||
|
|
||||||
|
val missingPerms = perms.filter { ContextCompat.checkSelfPermission(this, it) != PackageManager.PERMISSION_GRANTED }
|
||||||
|
if (missingPerms.isNotEmpty()) {
|
||||||
|
missingPerms.forEach {
|
||||||
|
// Permission is not granted
|
||||||
|
// Should we show an explanation?
|
||||||
|
if (ActivityCompat.shouldShowRequestPermissionRationale(this, it)) {
|
||||||
|
// FIXME
|
||||||
|
// Show an explanation to the user *asynchronously* -- don't block
|
||||||
|
// this thread waiting for the user's response! After the user
|
||||||
|
// sees the explanation, try again to request the permission.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ask for all the missing perms
|
||||||
|
ActivityCompat.requestPermissions(this, missingPerms.toTypedArray(), DID_REQUEST_PERM)
|
||||||
|
|
||||||
|
// DID_REQUEST_PERM is an
|
||||||
|
// app-defined int constant. The callback method gets the
|
||||||
|
// result of the request.
|
||||||
|
} else {
|
||||||
|
// Permission has already been granted
|
||||||
|
SoftwareUpdateService.enqueueWork(this, SoftwareUpdateService.scanDevicesIntent)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.activity_main)
|
setContentView(R.layout.activity_main)
|
||||||
|
@ -43,7 +80,7 @@ class MainActivity : AppCompatActivity() {
|
||||||
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
|
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT)
|
||||||
}
|
}
|
||||||
|
|
||||||
SoftwareUpdateService.enqueueWork(this, SoftwareUpdateService.scanDevicesIntent)
|
requestPermission()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
package com.geeksville.meshutil
|
package com.geeksville.meshutil
|
||||||
|
|
||||||
import android.bluetooth.*
|
import android.bluetooth.*
|
||||||
|
import android.bluetooth.le.*
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
|
import android.os.ParcelUuid
|
||||||
import android.os.SystemClock
|
import android.os.SystemClock
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
|
@ -76,10 +78,19 @@ class SoftwareUpdateService : JobIntentService() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val scanCallback = object : ScanCallback() {
|
||||||
|
override fun onScanFailed(errorCode: Int) {
|
||||||
|
throw NotImplementedError()
|
||||||
|
}
|
||||||
|
|
||||||
// For each device that appears in our scan, ask for its GATT, when the gatt arrives,
|
// For each device that appears in our scan, ask for its GATT, when the gatt arrives,
|
||||||
// check if it is an eligable device and store it in our list of candidates
|
// check if it is an eligable device and store it in our list of candidates
|
||||||
// if that device later disconnects remove it as a candidate
|
// if that device later disconnects remove it as a candidate
|
||||||
private val leScanCallback = BluetoothAdapter.LeScanCallback { device, _, _ ->
|
override fun onScanResult(callbackType: Int, result: ScanResult) {
|
||||||
|
|
||||||
|
// We don't need any more results now
|
||||||
|
bluetoothAdapter.bluetoothLeScanner.stopScan(this)
|
||||||
|
|
||||||
lateinit var bluetoothGatt: BluetoothGatt // late init so we can declare our callback and use this there
|
lateinit var bluetoothGatt: BluetoothGatt // late init so we can declare our callback and use this there
|
||||||
|
|
||||||
//var connectionState = STATE_DISCONNECTED
|
//var connectionState = STATE_DISCONNECTED
|
||||||
|
@ -157,8 +168,11 @@ class SoftwareUpdateService : JobIntentService() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bluetoothGatt = device.connectGatt(this, false, gattCallback)!!
|
bluetoothGatt = result.device.connectGatt(this@SoftwareUpdateService, false, gattCallback)!!
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private fun scanLeDevice(enable: Boolean) {
|
private fun scanLeDevice(enable: Boolean) {
|
||||||
when (enable) {
|
when (enable) {
|
||||||
|
@ -169,11 +183,21 @@ class SoftwareUpdateService : JobIntentService() {
|
||||||
bluetoothAdapter.stopLeScan(leScanCallback)
|
bluetoothAdapter.stopLeScan(leScanCallback)
|
||||||
}, SCAN_PERIOD)
|
}, SCAN_PERIOD)
|
||||||
mScanning = true */
|
mScanning = true */
|
||||||
assert(bluetoothAdapter.startLeScan(leScanCallback))
|
|
||||||
|
val scanner = bluetoothAdapter.bluetoothLeScanner
|
||||||
|
|
||||||
|
// filter and only accept devices that have a sw update service
|
||||||
|
val filter = ScanFilter.Builder().setServiceUuid(ParcelUuid(SW_UPDATE_UUID)).build()
|
||||||
|
val settings = ScanSettings.Builder().
|
||||||
|
setScanMode(ScanSettings.SCAN_MODE_BALANCED).
|
||||||
|
setMatchMode(ScanSettings.MATCH_NUM_ONE_ADVERTISEMENT).
|
||||||
|
setCallbackType(ScanSettings.CALLBACK_TYPE_FIRST_MATCH).
|
||||||
|
build()
|
||||||
|
scanner.startScan(listOf(filter), settings, scanCallback)
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
// mScanning = false
|
// mScanning = false
|
||||||
bluetoothAdapter.stopLeScan(leScanCallback)
|
// bluetoothAdapter.stopLeScan(leScanCallback)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue