Compare commits
No commits in common. "76b4e4d8d82b3f97e2e2cfc3302fe5ab2c107416" and "fe08d434a0e1c1f8a8c2f6058efdfe001b4216ce" have entirely different histories.
76b4e4d8d8
...
fe08d434a0
5 changed files with 48 additions and 97 deletions
|
@ -3,7 +3,20 @@
|
||||||
<component name="deploymentTargetDropDown">
|
<component name="deploymentTargetDropDown">
|
||||||
<value>
|
<value>
|
||||||
<entry key="app">
|
<entry key="app">
|
||||||
<State />
|
<State>
|
||||||
|
<runningDeviceTargetSelectedWithDropDown>
|
||||||
|
<Target>
|
||||||
|
<type value="RUNNING_DEVICE_TARGET" />
|
||||||
|
<deviceKey>
|
||||||
|
<Key>
|
||||||
|
<type value="SERIAL_NUMBER" />
|
||||||
|
<value value="adb-6lib7t69autkdauc-vxlVsT._adb-tls-connect._tcp" />
|
||||||
|
</Key>
|
||||||
|
</deviceKey>
|
||||||
|
</Target>
|
||||||
|
</runningDeviceTargetSelectedWithDropDown>
|
||||||
|
<timeTargetWasSelectedWithDropDown value="2024-05-17T14:54:32.741207568Z" />
|
||||||
|
</State>
|
||||||
</entry>
|
</entry>
|
||||||
</value>
|
</value>
|
||||||
</component>
|
</component>
|
||||||
|
|
|
@ -1,33 +1,18 @@
|
||||||
package eu.m724.bt
|
package eu.m724.bt
|
||||||
|
|
||||||
import android.Manifest
|
import android.Manifest
|
||||||
import android.annotation.SuppressLint
|
|
||||||
import android.app.Activity
|
|
||||||
import android.bluetooth.BluetoothAdapter
|
|
||||||
import android.bluetooth.BluetoothDevice
|
|
||||||
import android.bluetooth.le.BluetoothLeScanner
|
import android.bluetooth.le.BluetoothLeScanner
|
||||||
import android.content.Intent
|
|
||||||
import android.content.pm.PackageManager
|
import android.content.pm.PackageManager
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Handler
|
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.activity.ComponentActivity
|
import androidx.activity.ComponentActivity
|
||||||
import androidx.activity.compose.setContent
|
import androidx.activity.compose.setContent
|
||||||
import androidx.activity.result.ActivityResult
|
|
||||||
import androidx.activity.result.ActivityResultCallback
|
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.compose.foundation.layout.fillMaxSize
|
import androidx.compose.foundation.layout.fillMaxSize
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
|
||||||
import androidx.compose.foundation.lazy.LazyColumn
|
|
||||||
import androidx.compose.foundation.lazy.items
|
|
||||||
import androidx.compose.material3.ListItem
|
|
||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.material3.Surface
|
import androidx.compose.material3.Surface
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.mutableStateListOf
|
|
||||||
import androidx.compose.runtime.remember
|
|
||||||
import androidx.compose.runtime.snapshots.SnapshotStateList
|
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.tooling.preview.Preview
|
import androidx.compose.ui.tooling.preview.Preview
|
||||||
import androidx.core.app.ActivityCompat
|
import androidx.core.app.ActivityCompat
|
||||||
|
@ -37,48 +22,25 @@ import eu.m724.bt.ui.theme.PicoBtAndroidTheme
|
||||||
class MainActivity : ComponentActivity() {
|
class MainActivity : ComponentActivity() {
|
||||||
private val requestPermission =
|
private val requestPermission =
|
||||||
registerForActivityResult(
|
registerForActivityResult(
|
||||||
ActivityResultContracts.RequestMultiplePermissions()
|
ActivityResultContracts.RequestPermission()
|
||||||
) { granted ->
|
) { granted ->
|
||||||
val all = granted.values.all { all -> all }
|
onPermissionGranted(granted)
|
||||||
onPermissionGranted(all)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private val btEnableActivityResult =
|
|
||||||
registerForActivityResult(
|
|
||||||
ActivityResultContracts.StartActivityForResult(),
|
|
||||||
ActivityResultCallback<ActivityResult> { result ->
|
|
||||||
if (result.resultCode != Activity.RESULT_OK) {
|
|
||||||
Toast.makeText(
|
|
||||||
applicationContext,
|
|
||||||
"you must enable bluetooth",
|
|
||||||
Toast.LENGTH_SHORT
|
|
||||||
).show()
|
|
||||||
finishAffinity()
|
|
||||||
}
|
|
||||||
bluetoothHelper.start()
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
private val deviceList = mutableStateListOf("a", "b", "c")
|
|
||||||
|
|
||||||
private fun bluetoothPermission() {
|
private fun bluetoothPermission() {
|
||||||
requestPermission.launch(
|
if (checkSelfPermission(
|
||||||
arrayOf(
|
|
||||||
Manifest.permission.BLUETOOTH_CONNECT,
|
|
||||||
Manifest.permission.BLUETOOTH_SCAN
|
Manifest.permission.BLUETOOTH_SCAN
|
||||||
)
|
) != PackageManager.PERMISSION_GRANTED
|
||||||
)
|
) {
|
||||||
|
requestPermission.launch(Manifest.permission.BLUETOOTH_SCAN)
|
||||||
|
} else onPermissionGranted(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
private lateinit var bluetoothHelper: BluetoothHelper
|
private lateinit var bluetoothHelper: BluetoothHelper
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
bluetoothHelper = BluetoothHelper(applicationContext, deviceList)
|
bluetoothHelper = BluetoothHelper(applicationContext)
|
||||||
Handler().postDelayed({
|
|
||||||
System.out.println("aded")
|
|
||||||
deviceList.add("f")
|
|
||||||
}, 1000)
|
|
||||||
setContent {
|
setContent {
|
||||||
PicoBtAndroidTheme {
|
PicoBtAndroidTheme {
|
||||||
// A surface container using the 'background' color from the theme
|
// A surface container using the 'background' color from the theme
|
||||||
|
@ -87,13 +49,6 @@ class MainActivity : ComponentActivity() {
|
||||||
color = MaterialTheme.colorScheme.background
|
color = MaterialTheme.colorScheme.background
|
||||||
) {
|
) {
|
||||||
Greeting("Android")
|
Greeting("Android")
|
||||||
LazyColumn (
|
|
||||||
modifier = Modifier.fillMaxWidth()
|
|
||||||
) {
|
|
||||||
items(deviceList) { item ->
|
|
||||||
Text(text = item)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -106,17 +61,11 @@ class MainActivity : ComponentActivity() {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun onPermissionGranted(granted: Boolean) {
|
private fun onPermissionGranted(granted: Boolean) {
|
||||||
if (!granted) {
|
Toast.makeText(
|
||||||
Toast.makeText(
|
applicationContext,
|
||||||
applicationContext,
|
if (granted) "yes!!!!!!!!" else "no :(",
|
||||||
"you must grant bluetooth permission",
|
Toast.LENGTH_SHORT
|
||||||
Toast.LENGTH_SHORT
|
).show()
|
||||||
).show()
|
|
||||||
finishAffinity()
|
|
||||||
} else {
|
|
||||||
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
|
|
||||||
btEnableActivityResult.launch(enableBtIntent)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package eu.m724.bt.code
|
package eu.m724.bt.code
|
||||||
|
|
||||||
import android.Manifest
|
import android.Manifest
|
||||||
import android.annotation.SuppressLint
|
|
||||||
import android.bluetooth.BluetoothAdapter
|
import android.bluetooth.BluetoothAdapter
|
||||||
import android.bluetooth.BluetoothManager
|
import android.bluetooth.BluetoothManager
|
||||||
import android.bluetooth.le.BluetoothLeScanner
|
import android.bluetooth.le.BluetoothLeScanner
|
||||||
|
@ -9,64 +8,54 @@ import android.bluetooth.le.ScanCallback
|
||||||
import android.bluetooth.le.ScanResult
|
import android.bluetooth.le.ScanResult
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.pm.PackageManager
|
import android.content.pm.PackageManager
|
||||||
import androidx.compose.runtime.snapshots.SnapshotStateList
|
|
||||||
import androidx.core.app.ActivityCompat
|
import androidx.core.app.ActivityCompat
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import java.util.logging.Handler
|
|
||||||
|
|
||||||
class BluetoothHelper(
|
class BluetoothHelper(
|
||||||
val context: Context,
|
val context: Context
|
||||||
val deviceList: SnapshotStateList<String>
|
|
||||||
) {
|
) {
|
||||||
private var started: Boolean = false
|
|
||||||
private val bluetoothManager: BluetoothManager =
|
private val bluetoothManager: BluetoothManager =
|
||||||
context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
|
context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
|
||||||
private val bluetoothAdapter: BluetoothAdapter =
|
private val bluetoothAdapter: BluetoothAdapter =
|
||||||
bluetoothManager.adapter
|
bluetoothManager.adapter
|
||||||
private lateinit var bluetoothLeScanner: BluetoothLeScanner
|
private val bluetoothLeScanner: BluetoothLeScanner =
|
||||||
|
bluetoothAdapter.bluetoothLeScanner
|
||||||
|
|
||||||
@SuppressLint("MissingPermission")
|
@Throws(SecurityException::class)
|
||||||
fun start(): Int {
|
private fun init() {
|
||||||
if (started) return 0
|
|
||||||
started = true
|
|
||||||
|
|
||||||
bluetoothLeScanner = bluetoothAdapter.bluetoothLeScanner
|
|
||||||
|
|
||||||
if (!bluetoothAdapter.isEnabled)
|
|
||||||
return 2
|
|
||||||
|
|
||||||
bluetoothLeScanner.startScan(scanCallback)
|
bluetoothLeScanner.startScan(scanCallback)
|
||||||
System.out.println("scanning now")
|
|
||||||
android.os.Handler().postDelayed(Runnable {
|
|
||||||
bluetoothLeScanner.stopScan(scanCallback)
|
|
||||||
}, 10000)
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun onFailed(errorCode: Int) {
|
private fun onFailed(errorCode: Int) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("MissingPermission")
|
|
||||||
private fun addDevice(result: ScanResult) {
|
private fun addDevice(result: ScanResult) {
|
||||||
println("Found: ${result.device.name} (${result.device.address})")
|
if (ActivityCompat.checkSelfPermission(
|
||||||
CoroutineScope(Dispatchers.Main).launch {
|
context,
|
||||||
deviceList.add(result.device.name)
|
Manifest.permission.BLUETOOTH_CONNECT
|
||||||
|
) != PackageManager.PERMISSION_GRANTED
|
||||||
|
) {
|
||||||
|
// TODO: Consider calling
|
||||||
|
// ActivityCompat#requestPermissions
|
||||||
|
// here to request the missing permissions, and then overriding
|
||||||
|
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
|
||||||
|
// int[] grantResults)
|
||||||
|
// to handle the case where the user grants the permission. See the documentation
|
||||||
|
// for ActivityCompat#requestPermissions for more details.
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
println(result.device.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
private val scanCallback: ScanCallback =
|
private val scanCallback: ScanCallback =
|
||||||
object : ScanCallback() {
|
object : ScanCallback() {
|
||||||
override fun onScanResult(callbackType: Int, result: ScanResult) {
|
override fun onScanResult(callbackType: Int, result: ScanResult) {
|
||||||
super.onScanResult(callbackType, result)
|
|
||||||
addDevice(result)
|
addDevice(result)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onScanFailed(errorCode: Int) {
|
override fun onScanFailed(errorCode: Int) {
|
||||||
super.onScanFailed(errorCode)
|
super.onScanFailed(errorCode)
|
||||||
println("scam failed :((( $errorCode")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onBatchScanResults(results: MutableList<ScanResult>?) {
|
override fun onBatchScanResults(results: MutableList<ScanResult>?) {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
[versions]
|
[versions]
|
||||||
agp = "8.4.0"
|
agp = "8.3.1"
|
||||||
kotlin = "1.9.0"
|
kotlin = "1.9.0"
|
||||||
coreKtx = "1.12.0"
|
coreKtx = "1.12.0"
|
||||||
junit = "4.13.2"
|
junit = "4.13.2"
|
||||||
|
|
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
|
@ -1,6 +1,6 @@
|
||||||
#Thu May 16 18:07:40 CEST 2024
|
#Thu May 16 18:07:40 CEST 2024
|
||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
|
|
Loading…
Reference in a new issue