mirror of
https://github.com/yggdrasil-network/yggdrasil-android.git
synced 2025-04-28 22:25:09 +03:00
add support for peer discovery over BLE
This commit is contained in:
parent
9df80c0612
commit
39eba06f0c
11 changed files with 196 additions and 12 deletions
|
@ -1,9 +1,11 @@
|
|||
package eu.neilalexander.yggdrasil
|
||||
|
||||
import android.Manifest
|
||||
import android.app.Activity
|
||||
import android.content.*
|
||||
import android.graphics.Color
|
||||
import android.net.VpnService
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.widget.Switch
|
||||
import android.widget.TextView
|
||||
|
@ -14,6 +16,7 @@ import androidx.appcompat.widget.LinearLayoutCompat
|
|||
import androidx.core.content.edit
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
||||
import androidx.preference.PreferenceManager
|
||||
import com.permissionx.guolindev.PermissionX
|
||||
import eu.neilalexander.yggdrasil.PacketTunnelProvider.Companion.STATE_INTENT
|
||||
import mobile.Mobile
|
||||
import org.json.JSONArray
|
||||
|
@ -43,6 +46,46 @@ class MainActivity : AppCompatActivity() {
|
|||
}
|
||||
}
|
||||
|
||||
private fun checkBLEPermissions() {
|
||||
val preferences = PreferenceManager.getDefaultSharedPreferences(this.baseContext)
|
||||
val bleEnabled = preferences.getBoolean(BLE_ENABLED, (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S))
|
||||
if (!bleEnabled) {
|
||||
return
|
||||
}
|
||||
|
||||
PermissionX.init(this)
|
||||
.permissions(
|
||||
Manifest.permission.ACCESS_FINE_LOCATION,
|
||||
Manifest.permission.BLUETOOTH_ADVERTISE,
|
||||
Manifest.permission.BLUETOOTH_CONNECT,
|
||||
Manifest.permission.BLUETOOTH_SCAN,
|
||||
)
|
||||
.onExplainRequestReason { scope, deniedList ->
|
||||
scope.showRequestReasonDialog(
|
||||
deniedList,
|
||||
getString(R.string.explain_perms),
|
||||
getString(R.string.ok),
|
||||
getString(R.string.cancel),
|
||||
)
|
||||
}
|
||||
.onForwardToSettings { scope, deniedList ->
|
||||
scope.showForwardToSettingsDialog(
|
||||
deniedList,
|
||||
getString(R.string.manual_perms),
|
||||
getString(R.string.ok),
|
||||
getString(R.string.cancel),
|
||||
)
|
||||
}
|
||||
.request { allGranted, _, _ ->
|
||||
if(!allGranted) {
|
||||
preferences.edit().apply {
|
||||
putBoolean(BLE_ENABLED, false)
|
||||
commit()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
|
@ -65,6 +108,7 @@ class MainActivity : AppCompatActivity() {
|
|||
enabledSwitch.setOnCheckedChangeListener { _, isChecked ->
|
||||
when (isChecked) {
|
||||
true -> {
|
||||
checkBLEPermissions()
|
||||
val vpnIntent = VpnService.prepare(this)
|
||||
if (vpnIntent != null) {
|
||||
startVpnActivity.launch(vpnIntent)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue