Skip to content

Commit 7dc8959

Browse files
committed
Stop bluetooth scanning on back, make constant for log tags, change screenshots
1 parent b0d42a0 commit 7dc8959

17 files changed

+146
-74
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Lescanner
2+
{
3+
/// <summary>
4+
/// Common constants used throughout the Lescanner application.
5+
/// </summary>
6+
public static class Constants
7+
{
8+
/// <summary>
9+
/// Log tag used for all logging in the Lescanner application.
10+
/// </summary>
11+
public const string LOG_TAG = "Lescanner";
12+
}
13+
}

Mobile/LeScanner/Lescanner/DeviceListPage.cs

Lines changed: 76 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,55 @@ public DeviceListPage(TizenBLEService bleService, Navigator navigator)
3434
_bleService = bleService;
3535
_navigator = navigator;
3636
_uiContext = SynchronizationContext.Current; // Capture main UI thread context
37+
3738
AppBar = new AppBar { Title = "Device List" };
38-
InitializeComponent();
3939

40+
if (AppBar.Title != null)
41+
{
42+
AppBar.Title = AppBar.Title.PadLeft(20);
43+
}
44+
4045
// Subscribe to BLE service events
4146
_bleService.DeviceDiscovered += OnDeviceDiscovered;
4247
_bleService.GattConnectionStateChanged += OnGattConnectionStateChanged; // For connection feedback
4348
_bleService.DeviceNameRetrieved += OnDeviceNameRetrieved; // Subscribe to name retrieval event
4449

50+
InitializeComponent();
51+
52+
// Add back button to AppBar
53+
AddBackButtonToAppBar();
54+
4555
// Start scanning process when the page is created.
4656
StartScanningProcess();
4757
}
4858

59+
/// <summary>
60+
/// Adds a back button to the AppBar.
61+
/// </summary>
62+
private void AddBackButtonToAppBar()
63+
{
64+
if (AppBar != null)
65+
{
66+
// Create a back button
67+
var backButton = new Button()
68+
{
69+
Text = "Back",
70+
WidthSpecification = 80,
71+
HeightSpecification = 40
72+
};
73+
74+
// Set button background color
75+
backButton.BackgroundColor = new Color(0.2f, 0.6f, 1.0f, 1.0f);
76+
backButton.TextColor = Color.White;
77+
78+
// Add click handler
79+
backButton.Clicked += OnBackButtonClicked;
80+
81+
// Add button to the AppBar
82+
AppBar.NavigationContent = backButton;
83+
}
84+
}
85+
4986
/// <summary>
5087
/// Initializes the UI components for the page.
5188
/// </summary>
@@ -131,10 +168,10 @@ private void OnDeviceDiscovered(object sender, AdapterLeScanResultChangedEventAr
131168
}
132169
catch (Exception ex)
133170
{
134-
Tizen.Log.Warn("LescannerDeviceListPage", $"Error calling GetDeviceName(0) for {e.DeviceData.RemoteAddress}: {ex.Message}. Will use address.");
171+
Tizen.Log.Warn(Constants.LOG_TAG, $"Error calling GetDeviceName(0) for {e.DeviceData.RemoteAddress}: {ex.Message}. Will use address.");
135172
}
136173
AddDeviceToList(e.DeviceData.RemoteAddress, initialName);
137-
Tizen.Log.Info("LescannerDeviceListPage", $"Device discovered: {e.DeviceData.RemoteAddress}. Initial name from scan: {initialName ?? "N/A"}");
174+
Tizen.Log.Info(Constants.LOG_TAG, $"Device discovered: {e.DeviceData.RemoteAddress}. Initial name from scan: {initialName ?? "N/A"}");
138175
}
139176
}, null);
140177
}
@@ -181,7 +218,7 @@ private void OnDeviceNameRetrieved(object sender, DeviceNameEventArgs e)
181218
if (!string.IsNullOrEmpty(e.DeviceName))
182219
{
183220
deviceLabel.Text = $"{e.DeviceName} ({e.DeviceAddress})";
184-
Tizen.Log.Info("LescannerDeviceListPage", $"Updated UI for {e.DeviceAddress} with name: {e.DeviceName}");
221+
Tizen.Log.Info(Constants.LOG_TAG, $"Updated UI for {e.DeviceAddress} with name: {e.DeviceName}");
185222

186223
// Safely update status label to show device name was updated
187224
try
@@ -190,46 +227,46 @@ private void OnDeviceNameRetrieved(object sender, DeviceNameEventArgs e)
190227
}
191228
catch (Exception statusEx)
192229
{
193-
Tizen.Log.Warn("LescannerDeviceListPage", $"Could not update status label: {statusEx.Message}");
230+
Tizen.Log.Warn(Constants.LOG_TAG, $"Could not update status label: {statusEx.Message}");
194231
}
195232
}
196233
else
197234
{
198235
// Name might be null if not found or failed to read, keep address only.
199-
Tizen.Log.Info("LescannerDeviceListPage", $"Failed to retrieve name for {e.DeviceAddress}. UI remains as address.");
236+
Tizen.Log.Info(Constants.LOG_TAG, $"Failed to retrieve name for {e.DeviceAddress}. UI remains as address.");
200237
}
201238

202239
// Navigate to UUID page only when we have a valid device name (not null)
203240
if (!string.IsNullOrEmpty(e.DeviceName) && !_navigatedDevices.Contains(e.DeviceAddress))
204241
{
205242
_navigatedDevices.Add(e.DeviceAddress);
206243
string displayName = deviceLabel.Text;
207-
Tizen.Log.Info("LescannerDeviceListPage", $"Device name '{e.DeviceName}' retrieved for {e.DeviceAddress}. Navigating to UUID page with display name: {displayName}");
244+
Tizen.Log.Info(Constants.LOG_TAG, $"Device name '{e.DeviceName}' retrieved for {e.DeviceAddress}. Navigating to UUID page with display name: {displayName}");
208245
_navigator.Push(new UuidListPage(_bleService, _navigator, e.DeviceAddress, displayName));
209246
}
210247
else if (string.IsNullOrEmpty(e.DeviceName))
211248
{
212-
Tizen.Log.Info("LescannerDeviceListPage", $"Device name is null or empty for {e.DeviceAddress}. Waiting for name retrieval before navigation.");
249+
Tizen.Log.Info(Constants.LOG_TAG, $"Device name is null or empty for {e.DeviceAddress}. Waiting for name retrieval before navigation.");
213250
}
214251
else
215252
{
216-
Tizen.Log.Info("LescannerDeviceListPage", $"Already navigated for {e.DeviceAddress}. Skipping duplicate navigation.");
253+
Tizen.Log.Info(Constants.LOG_TAG, $"Already navigated for {e.DeviceAddress}. Skipping duplicate navigation.");
217254
}
218255
}
219256
else
220257
{
221-
Tizen.Log.Warn("LescannerDeviceListPage", $"Received name for {e.DeviceAddress} but it's not in the discovered list (maybe navigated away?).");
258+
Tizen.Log.Warn(Constants.LOG_TAG, $"Received name for {e.DeviceAddress} but it's not in the discovered list (maybe navigated away?).");
222259
}
223260
}
224261
catch (Exception ex)
225262
{
226-
Tizen.Log.Error("LescannerDeviceListPage", $"Exception in OnDeviceNameRetrieved UI thread for {e.DeviceAddress}: {ex.Message}");
263+
Tizen.Log.Error(Constants.LOG_TAG, $"Exception in OnDeviceNameRetrieved UI thread for {e.DeviceAddress}: {ex.Message}");
227264
}
228265
}, null);
229266
}
230267
catch (Exception ex)
231268
{
232-
Tizen.Log.Error("LescannerDeviceListPage", $"Exception in OnDeviceNameRetrieved for {e.DeviceAddress}: {ex.Message}");
269+
Tizen.Log.Error(Constants.LOG_TAG, $"Exception in OnDeviceNameRetrieved for {e.DeviceAddress}: {ex.Message}");
233270
}
234271
}
235272

@@ -241,7 +278,7 @@ private async void OnDeviceTapped(string deviceAddress)
241278
{
242279
try
243280
{
244-
Tizen.Log.Info("LescannerDeviceListPage", $"Device tapped: {deviceAddress}. Initiating GATT connection.");
281+
Tizen.Log.Info(Constants.LOG_TAG, $"Device tapped: {deviceAddress}. Initiating GATT connection.");
245282
_statusLabel.Text = $"Connecting to {deviceAddress}...";
246283

247284
// Stop scanning before attempting connection
@@ -258,13 +295,13 @@ private async void OnDeviceTapped(string deviceAddress)
258295

259296
if (!connectionResult)
260297
{
261-
Tizen.Log.Error("LescannerDeviceListPage", $"Failed to initiate connection to {deviceAddress}");
298+
Tizen.Log.Error(Constants.LOG_TAG, $"Failed to initiate connection to {deviceAddress}");
262299
_statusLabel.Text = $"Failed to connect, retrying...";
263300
}
264301
}
265302
catch (Exception ex)
266303
{
267-
Tizen.Log.Error("LescannerDeviceListPage", $"Exception in OnDeviceTapped for {deviceAddress}: {ex.Message}");
304+
Tizen.Log.Error(Constants.LOG_TAG, $"Exception in OnDeviceTapped for {deviceAddress}: {ex.Message}");
268305
_statusLabel.Text = $"Connection error: {ex.Message}";
269306
}
270307
}
@@ -292,14 +329,36 @@ private void OnGattConnectionStateChanged(object sender, GattConnectionStateChan
292329
}
293330
catch (Exception ex)
294331
{
295-
Tizen.Log.Error("LescannerDeviceListPage", $"Exception in OnGattConnectionStateChanged UI thread: {ex.Message}");
332+
Tizen.Log.Error(Constants.LOG_TAG, $"Exception in OnGattConnectionStateChanged UI thread: {ex.Message}");
296333
}
297334
}, null);
298335
}
299336
catch (Exception ex)
300337
{
301-
Tizen.Log.Error("LescannerDeviceListPage", $"Exception in OnGattConnectionStateChanged: {ex.Message}");
338+
Tizen.Log.Error(Constants.LOG_TAG, $"Exception in OnGattConnectionStateChanged: {ex.Message}");
302339
}
303340
}
341+
342+
/// <summary>
343+
/// Event handler for the custom back button click.
344+
/// Stops the scanning process and navigates back to the previous page.
345+
/// </summary>
346+
/// <param name="sender">The event source.</param>
347+
/// <param name="e">The event arguments.</param>
348+
private async void OnBackButtonClicked(object sender, ClickedEventArgs e)
349+
{
350+
Tizen.Log.Info("LescannerDeviceListPage", "Back button clicked. Stopping scan and navigating back.");
351+
352+
// Stop the scanning process immediately
353+
if (_isScanActive)
354+
{
355+
_statusLabel.Text = "Stopping scan...";
356+
await StopScanningProcessAsync();
357+
}
358+
359+
// Navigate back to the previous page
360+
_navigator.Pop();
361+
}
362+
304363
}
305364
}

Mobile/LeScanner/Lescanner/HomePage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private void InitializeComponent()
5656
/// <param name="e">The event arguments.</param>
5757
private void OnScanButtonClicked(object sender, ClickedEventArgs e)
5858
{
59-
Tizen.Log.Info("LescannerHomePage", "BLE Scan button clicked.");
59+
Tizen.Log.Info(Constants.LOG_TAG, "BLE Scan button clicked.");
6060
if (_bleService.IsBluetoothEnabled())
6161
{
6262
_statusLabel.Text = "Tap 'BLE Scan' to start.";
@@ -67,7 +67,7 @@ private void OnScanButtonClicked(object sender, ClickedEventArgs e)
6767
{
6868
_statusLabel.Text = "Please turn on Bluetooth.";
6969
// Optionally, show a more persistent message or a Toast.
70-
Tizen.Log.Warn("LescannerHomePage", "Bluetooth is not enabled.");
70+
Tizen.Log.Warn(Constants.LOG_TAG, "Bluetooth is not enabled.");
7171
}
7272
}
7373
}

0 commit comments

Comments
 (0)