This commit is contained in:
Minecon724 2024-05-18 16:17:57 +02:00
parent 6ca0e3d569
commit 76b4e4d8d8
5 changed files with 25 additions and 26 deletions

View file

@ -3,20 +3,7 @@
<component name="deploymentTargetDropDown">
<value>
<entry key="app">
<State>
<targetSelectedWithDropDown>
<Target>
<type value="QUICK_BOOT_TARGET" />
<deviceKey>
<Key>
<type value="VIRTUAL_DEVICE_PATH" />
<value value="$USER_HOME$/.android/avd/Pixel_3a_API_34_extension_level_7_x86_64.avd" />
</Key>
</deviceKey>
</Target>
</targetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2024-05-18T10:28:26.053108340Z" />
</State>
<State />
</entry>
</value>
</component>

View file

@ -37,9 +37,10 @@ import eu.m724.bt.ui.theme.PicoBtAndroidTheme
class MainActivity : ComponentActivity() {
private val requestPermission =
registerForActivityResult(
ActivityResultContracts.RequestPermission()
ActivityResultContracts.RequestMultiplePermissions()
) { granted ->
onPermissionGranted(granted)
val all = granted.values.all { all -> all }
onPermissionGranted(all)
}
private val btEnableActivityResult =
@ -54,18 +55,19 @@ class MainActivity : ComponentActivity() {
).show()
finishAffinity()
}
bluetoothHelper.start()
}
)
private val deviceList = mutableStateListOf("a", "b", "c")
private fun bluetoothPermission() {
if (checkSelfPermission(
Manifest.permission.BLUETOOTH_CONNECT
) != PackageManager.PERMISSION_GRANTED
) {
requestPermission.launch(Manifest.permission.BLUETOOTH_CONNECT)
} else onPermissionGranted(true)
requestPermission.launch(
arrayOf(
Manifest.permission.BLUETOOTH_CONNECT,
Manifest.permission.BLUETOOTH_SCAN
)
)
}
private lateinit var bluetoothHelper: BluetoothHelper

View file

@ -14,19 +14,24 @@ import androidx.core.app.ActivityCompat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.logging.Handler
class BluetoothHelper(
val context: Context,
val deviceList: SnapshotStateList<String>
) {
private var started: Boolean = false
private val bluetoothManager: BluetoothManager =
context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
private val bluetoothAdapter: BluetoothAdapter =
bluetoothManager.adapter
private lateinit var bluetoothLeScanner: BluetoothLeScanner
@Throws(SecurityException::class)
fun init(): Int {
@SuppressLint("MissingPermission")
fun start(): Int {
if (started) return 0
started = true
bluetoothLeScanner = bluetoothAdapter.bluetoothLeScanner
if (!bluetoothAdapter.isEnabled)
@ -34,6 +39,9 @@ class BluetoothHelper(
bluetoothLeScanner.startScan(scanCallback)
System.out.println("scanning now")
android.os.Handler().postDelayed(Runnable {
bluetoothLeScanner.stopScan(scanCallback)
}, 10000)
return 0
}
@ -52,11 +60,13 @@ class BluetoothHelper(
private val scanCallback: ScanCallback =
object : ScanCallback() {
override fun onScanResult(callbackType: Int, result: ScanResult) {
super.onScanResult(callbackType, result)
addDevice(result)
}
override fun onScanFailed(errorCode: Int) {
super.onScanFailed(errorCode)
println("scam failed :((( $errorCode")
}
override fun onBatchScanResults(results: MutableList<ScanResult>?) {

View file

@ -1,5 +1,5 @@
[versions]
agp = "8.3.1"
agp = "8.4.0"
kotlin = "1.9.0"
coreKtx = "1.12.0"
junit = "4.13.2"

View file

@ -1,6 +1,6 @@
#Thu May 16 18:07:40 CEST 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists