Skip to content

Commit 16cbbae

Browse files
author
ars
committed
removed wifi trigger
1 parent 9279257 commit 16cbbae

File tree

5 files changed

+21
-132
lines changed

5 files changed

+21
-132
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@
5050
android:exported="true">
5151
<intent-filter>
5252
<action android:name="android.bluetooth.device.action.ACL_CONNECTED" />
53-
<action android:name="android.net.wifi.STATE_CHANGE" />
54-
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
5553
</intent-filter>
5654
</receiver>
5755

app/src/main/java/com/andrerinas/wirelesshelper/AutoStartReceiver.kt

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import android.bluetooth.BluetoothDevice
44
import android.content.BroadcastReceiver
55
import android.content.Context
66
import android.content.Intent
7-
import android.net.NetworkInfo
8-
import android.net.wifi.WifiManager
97
import android.util.Log
108
import androidx.core.content.ContextCompat
119

@@ -17,47 +15,19 @@ class AutoStartReceiver : BroadcastReceiver() {
1715
Log.i(TAG, "Broadcast received: $action")
1816

1917
val prefs = context.getSharedPreferences("WirelessHelperPrefs", Context.MODE_PRIVATE)
20-
val autoStartMode = prefs.getInt("auto_start_mode", 0) // 0=Off, 1=BT, 2=Wifi
21-
22-
Log.i(TAG, "Current Auto-Start Mode: $autoStartMode")
23-
24-
if (autoStartMode == 0) {
25-
Log.i(TAG, "Auto-start is disabled in settings. Ignoring.")
26-
return
27-
}
28-
29-
when (action) {
30-
BluetoothDevice.ACTION_ACL_CONNECTED -> {
31-
if (autoStartMode == 1) {
32-
val device = intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE)
33-
val targetMac = prefs.getString("auto_start_bt_mac", null)
34-
Log.i(TAG, "BT Device connected: ${device?.name} (${device?.address})")
35-
if (device?.address == targetMac) {
36-
Log.i(TAG, "MATCH! Starting service...")
37-
startService(context)
38-
}
39-
}
40-
}
41-
WifiManager.NETWORK_STATE_CHANGED_ACTION, "android.net.conn.CONNECTIVITY_CHANGE" -> {
42-
if (autoStartMode == 2) {
43-
val wifiManager = context.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
44-
val info = wifiManager.connectionInfo
45-
val ssid = info.ssid.removeSurrounding("\"")
46-
val targetSsid = prefs.getString("auto_start_wifi_ssid", null)
47-
48-
Log.i(TAG, "WiFi State Change. Current SSID: $ssid, Target: $targetSsid")
49-
50-
// Check if actually connected
51-
val networkInfo = intent.getParcelableExtra<NetworkInfo>(WifiManager.EXTRA_NETWORK_INFO)
52-
val isConnected = networkInfo?.isConnected ?: true // Fallback to true if extra missing
53-
54-
Log.i(TAG, "Network isConnected: $isConnected")
55-
56-
if (isConnected && ssid == targetSsid && ssid != "<unknown ssid>" && ssid.isNotEmpty()) {
57-
Log.i(TAG, "MATCH! Starting service...")
58-
startService(context)
59-
}
60-
}
18+
val autoStartMode = prefs.getInt("auto_start_mode", 0) // 0=Off, 1=BT
19+
20+
if (autoStartMode == 0) return
21+
22+
if (action == BluetoothDevice.ACTION_ACL_CONNECTED) {
23+
val device = intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE)
24+
val targetMac = prefs.getString("auto_start_bt_mac", null)
25+
26+
Log.i(TAG, "BT Device connected: ${device?.name} (${device?.address})")
27+
28+
if (device?.address == targetMac) {
29+
Log.i(TAG, "MATCH! Starting service...")
30+
startService(context)
6131
}
6232
}
6333
}
@@ -73,4 +43,4 @@ class AutoStartReceiver : BroadcastReceiver() {
7343
Log.e(TAG, "Failed to auto-start service: ${e.message}", e)
7444
}
7545
}
76-
}
46+
}

app/src/main/java/com/andrerinas/wirelesshelper/MainActivity.kt

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.andrerinas.wirelesshelper
22

3+
import android.app.ActivityManager
34
import android.content.Context
45
import android.content.Intent
56
import android.content.pm.PackageManager
@@ -16,7 +17,6 @@ import androidx.core.app.ActivityCompat
1617
import androidx.core.content.ContextCompat
1718
import androidx.core.content.edit
1819
import com.google.android.material.dialog.MaterialAlertDialogBuilder
19-
import com.andrerinas.wirelesshelper.BuildConfig
2020

2121
class MainActivity : AppCompatActivity() {
2222

@@ -34,8 +34,6 @@ class MainActivity : AppCompatActivity() {
3434
private lateinit var layoutBluetoothDevice: View
3535
private lateinit var tvBluetoothDeviceValue: TextView
3636

37-
private lateinit var layoutWifiNetwork: View
38-
private lateinit var tvWifiNetworkValue: TextView
3937
private lateinit var tvVersionValue: TextView
4038
private lateinit var layoutAbout: View
4139

@@ -62,8 +60,7 @@ class MainActivity : AppCompatActivity() {
6260
private val autoStartModes by lazy {
6361
arrayOf(
6462
getString(R.string.auto_start_no),
65-
getString(R.string.auto_start_bt),
66-
getString(R.string.auto_start_wifi)
63+
getString(R.string.auto_start_bt)
6764
)
6865
}
6966

@@ -90,8 +87,6 @@ class MainActivity : AppCompatActivity() {
9087
layoutBluetoothDevice = findViewById(R.id.layoutBluetoothDevice)
9188
tvBluetoothDeviceValue = findViewById(R.id.tvBluetoothDeviceValue)
9289

93-
layoutWifiNetwork = findViewById(R.id.layoutWifiNetwork)
94-
tvWifiNetworkValue = findViewById(R.id.tvWifiNetworkValue)
9590
tvVersionValue = findViewById(R.id.tvVersionValue)
9691
layoutAbout = findViewById(R.id.layoutAbout)
9792

@@ -106,6 +101,7 @@ class MainActivity : AppCompatActivity() {
106101
.setPositiveButton(android.R.string.ok, null)
107102
.show()
108103
}
104+
109105
btnToggleService.setOnClickListener {
110106
if (isServiceRunning) stopLauncherService() else checkPermissionsAndStart()
111107
}
@@ -143,37 +139,6 @@ class MainActivity : AppCompatActivity() {
143139
layoutBluetoothDevice.setOnClickListener {
144140
showBluetoothDeviceSelector()
145141
}
146-
147-
layoutWifiNetwork.setOnClickListener {
148-
showWifiSelector()
149-
}
150-
}
151-
152-
private fun showWifiSelector() {
153-
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
154-
ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION), 102)
155-
return
156-
}
157-
158-
val wifiManager = applicationContext.getSystemService(Context.WIFI_SERVICE) as android.net.wifi.WifiManager
159-
val ssid = wifiManager.connectionInfo.ssid.removeSurrounding("\"")
160-
161-
if (ssid == "<unknown ssid>" || ssid.isEmpty()) {
162-
Toast.makeText(this, "Connect to a WiFi network first", Toast.LENGTH_LONG).show()
163-
return
164-
}
165-
166-
MaterialAlertDialogBuilder(this, R.style.DarkAlertDialog)
167-
.setTitle("Select WiFi Network")
168-
.setMessage("Do you want to use '$ssid' as the auto-start trigger?")
169-
.setPositiveButton("Use Current WiFi") { _, _ ->
170-
val prefs = getSharedPreferences("WirelessHelperPrefs", Context.MODE_PRIVATE)
171-
prefs.edit { putString("auto_start_wifi_ssid", ssid) }
172-
tvWifiNetworkValue.text = ssid
173-
Toast.makeText(this, "Auto-start linked to $ssid", Toast.LENGTH_SHORT).show()
174-
}
175-
.setNegativeButton(android.R.string.cancel, null)
176-
.show()
177142
}
178143

179144
private fun showBluetoothDeviceSelector() {
@@ -219,7 +184,6 @@ class MainActivity : AppCompatActivity() {
219184
updateAutoStartUI(autoMode)
220185

221186
tvBluetoothDeviceValue.text = prefs.getString("auto_start_bt_name", getString(R.string.not_set))
222-
tvWifiNetworkValue.text = prefs.getString("auto_start_wifi_ssid", getString(R.string.not_set))
223187

224188
updateButtonState(WirelessHelperService.isRunning)
225189
}
@@ -230,21 +194,13 @@ class MainActivity : AppCompatActivity() {
230194
when (mode) {
231195
0 -> { // No
232196
layoutBluetoothDevice.visibility = View.GONE
233-
layoutWifiNetwork.visibility = View.GONE
234197
layoutAutoStart.setBackgroundResource(R.drawable.bg_item_bottom)
235198
}
236199
1 -> { // Bluetooth
237200
layoutBluetoothDevice.visibility = View.VISIBLE
238-
layoutWifiNetwork.visibility = View.GONE
239201
layoutAutoStart.setBackgroundResource(R.drawable.bg_item_middle)
240202
layoutBluetoothDevice.setBackgroundResource(R.drawable.bg_item_bottom)
241203
}
242-
2 -> { // Wifi
243-
layoutBluetoothDevice.visibility = View.GONE
244-
layoutWifiNetwork.visibility = View.VISIBLE
245-
layoutAutoStart.setBackgroundResource(R.drawable.bg_item_middle)
246-
layoutWifiNetwork.setBackgroundResource(R.drawable.bg_item_bottom)
247-
}
248204
}
249205
}
250206

@@ -256,8 +212,6 @@ class MainActivity : AppCompatActivity() {
256212
if (Build.VERSION.SDK_INT >= 31) {
257213
permissions.add("android.permission.BLUETOOTH_CONNECT")
258214
}
259-
// Location is needed for WiFi SSID detection and general network tasks on some versions
260-
permissions.add("android.permission.ACCESS_FINE_LOCATION")
261215

262216
val missingPermissions = permissions.filter {
263217
ContextCompat.checkSelfPermission(this, it) != PackageManager.PERMISSION_GRANTED
@@ -307,8 +261,9 @@ class MainActivity : AppCompatActivity() {
307261

308262
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
309263
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
310-
if (requestCode == 100 && grantResults.all { it == PackageManager.PERMISSION_GRANTED }) {
311-
startLauncherService()
264+
if (grantResults.all { it == PackageManager.PERMISSION_GRANTED }) {
265+
if (requestCode == 100) startLauncherService()
266+
if (requestCode == 101) showBluetoothDeviceSelector()
312267
}
313268
}
314-
}
269+
}

app/src/main/res/layout/activity_main.xml

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -154,38 +154,6 @@
154154
android:textSize="14sp"/>
155155
</LinearLayout>
156156

157-
<!-- 4. Wifi Network -->
158-
<LinearLayout
159-
android:id="@+id/layoutWifiNetwork"
160-
android:layout_width="match_parent"
161-
android:layout_height="wrap_content"
162-
android:orientation="vertical"
163-
android:paddingHorizontal="16dp"
164-
android:paddingVertical="14dp"
165-
android:layout_marginBottom="2dp"
166-
android:background="@drawable/bg_item_bottom"
167-
android:clickable="true"
168-
android:focusable="true"
169-
android:visibility="gone">
170-
171-
<TextView
172-
android:layout_width="match_parent"
173-
android:layout_height="wrap_content"
174-
android:text="@string/wifi_network_label"
175-
android:textColor="@color/text_title"
176-
android:textSize="16sp"
177-
android:textStyle="normal"
178-
android:layout_marginBottom="2dp"/>
179-
180-
<TextView
181-
android:id="@+id/tvWifiNetworkValue"
182-
android:layout_width="match_parent"
183-
android:layout_height="wrap_content"
184-
android:text="@string/not_set"
185-
android:textColor="@color/text_subtitle"
186-
android:textSize="14sp"/>
187-
</LinearLayout>
188-
189157
</LinearLayout>
190158

191159
<!-- INFO Header -->

app/src/main/res/values/strings.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
<string name="auto_start_label">Auto Start Service</string>
1313
<string name="auto_start_no">No (Manual)</string>
1414
<string name="auto_start_bt">On Bluetooth Connection</string>
15-
<string name="auto_start_wifi">On Wifi Connection</string>
1615

1716
<string name="bt_device_label">Bluetooth Device</string>
18-
<string name="wifi_network_label">Wifi Network</string>
1917
<string name="not_set">Not set</string>
2018

2119
<string name="logs_label">Logs:</string>

0 commit comments

Comments
 (0)