Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
R
Robobin
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
plw1g21
Robobin
Commits
526befba
Commit
526befba
authored
8 months ago
by
Paul-Winpenny
Browse files
Options
Downloads
Patches
Plain Diff
read and write works, issue is disconnecting doesn't discard properly,
resulting in issues
parent
d5a636f0
No related branches found
No related tags found
1 merge request
!1
App now has a basic structure and BLE support
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
App/RobobinApp/ViewModels/ConnectionPageViewModel.cs
+154
-55
154 additions, 55 deletions
App/RobobinApp/ViewModels/ConnectionPageViewModel.cs
with
154 additions
and
55 deletions
App/RobobinApp/ViewModels/ConnectionPageViewModel.cs
+
154
−
55
View file @
526befba
...
@@ -154,6 +154,7 @@ namespace RobobinApp.ViewModels
...
@@ -154,6 +154,7 @@ namespace RobobinApp.ViewModels
if
(
SelectedWifiNetwork
!=
null
)
if
(
SelectedWifiNetwork
!=
null
)
{
{
Debug
.
WriteLine
(
"Wifi been selected"
);
Debug
.
WriteLine
(
"Wifi been selected"
);
Debug
.
WriteLine
(
$"Selected Network
{
SelectedWifiNetwork
.
SSID
}
"
);
}
}
if
(!
_isConnected
||
_connectedDevice
==
null
)
if
(!
_isConnected
||
_connectedDevice
==
null
)
{
{
...
@@ -315,7 +316,7 @@ namespace RobobinApp.ViewModels
...
@@ -315,7 +316,7 @@ namespace RobobinApp.ViewModels
try
try
{
{
Debug
.
WriteLine
(
$"Attempt
{
attempt
}
: Connecting to device
{
deviceId
}
"
);
Debug
.
WriteLine
(
$"Attempt
{
attempt
}
: Connecting to device
{
deviceId
}
"
);
var
cancellationTokenSource
=
new
CancellationTokenSource
(
TimeSpan
.
FromSeconds
(
30
));
var
cancellationTokenSource
=
new
CancellationTokenSource
(
);
// No timeout specified
device
=
await
_adapter
.
ConnectToKnownDeviceAsync
(
deviceId
,
new
ConnectParameters
(
false
,
true
),
cancellationTokenSource
.
Token
);
device
=
await
_adapter
.
ConnectToKnownDeviceAsync
(
deviceId
,
new
ConnectParameters
(
false
,
true
),
cancellationTokenSource
.
Token
);
return
device
;
return
device
;
}
}
...
@@ -330,7 +331,6 @@ namespace RobobinApp.ViewModels
...
@@ -330,7 +331,6 @@ namespace RobobinApp.ViewModels
}
}
return
device
;
return
device
;
}
}
private
async
void
OnConnect
()
private
async
void
OnConnect
()
{
{
if
(
SelectedDevice
==
null
)
if
(
SelectedDevice
==
null
)
...
@@ -340,17 +340,16 @@ namespace RobobinApp.ViewModels
...
@@ -340,17 +340,16 @@ namespace RobobinApp.ViewModels
}
}
Debug
.
WriteLine
(
$"Attempting to connect to
{
SelectedDevice
.
Name
}
, MAC Address
{
SelectedDevice
.
MacAddress
}
"
);
Debug
.
WriteLine
(
$"Attempting to connect to
{
SelectedDevice
.
Name
}
, MAC Address
{
SelectedDevice
.
MacAddress
}
"
);
await
Application
.
Current
.
MainPage
.
DisplayAlert
(
"Connection"
,
$"Connecting to
{
SelectedDevice
.
Name
}
..."
,
"OK"
);
try
try
{
{
await
_adapter
.
StopScanningForDevicesAsync
();
await
_adapter
.
StopScanningForDevicesAsync
();
Debug
.
WriteLine
(
"Stopped scanning for devices."
);
IsConnected
=
true
;
IsConnected
=
true
;
// Connect to the selected device
_connectedDevice
=
await
TryConnectAsync
(
Guid
.
Parse
(
SelectedDevice
.
MacAddress
));
_connectedDevice
=
await
TryConnectAsync
(
Guid
.
Parse
(
SelectedDevice
.
MacAddress
));
if
(
_connectedDevice
!=
null
)
{
Debug
.
WriteLine
(
$"Successfully connected to
{
SelectedDevice
.
Name
}
."
);
Debug
.
WriteLine
(
$"Successfully connected to
{
SelectedDevice
.
Name
}
."
);
var
services
=
await
_connectedDevice
.
GetServicesAsync
();
var
services
=
await
_connectedDevice
.
GetServicesAsync
();
IsWifiNetworkSelectionVisible
=
true
;
IsWifiNetworkSelectionVisible
=
true
;
...
@@ -359,67 +358,120 @@ namespace RobobinApp.ViewModels
...
@@ -359,67 +358,120 @@ namespace RobobinApp.ViewModels
{
{
Debug
.
WriteLine
(
$"Service: ID=
{
service
.
Id
}
, Name=
{
service
.
Name
}
"
);
Debug
.
WriteLine
(
$"Service: ID=
{
service
.
Id
}
, Name=
{
service
.
Name
}
"
);
if
(
service
.
Id
.
Equals
(
Guid
.
Parse
(
"00000001-710e-4a5b-8d75-3e5b444bc3cf"
))
)
}
var
transmitService
=
services
.
FirstOrDefault
(
s
=>
s
.
Id
==
Guid
.
Parse
(
SendReceiveServiceUUID
));
Debug
.
WriteLine
(
"Transmit service was accessed"
);
Debug
.
WriteLine
(
$"
{
transmitService
.
Name
}
:
{
transmitService
.
Id
}
"
);
if
(
transmitService
==
null
)
{
{
Debug
.
WriteLine
(
"Transmit service wasn't found"
);
var
characteristics
=
await
service
.
GetCharacteristicsAsync
();
}
var
characteristics
=
await
transmitService
.
GetCharacteristicsAsync
();
foreach
(
var
characteristic
in
characteristics
)
foreach
(
var
characteristic
in
characteristics
)
{
{
Debug
.
WriteLine
(
$"
{
characteristic
.
Uuid
}
|
{
characteristic
.
Name
}
"
);
Debug
.
WriteLine
(
$"
{
characteristic
.
Uuid
}
|
{
characteristic
.
Name
}
"
);
if
(
characteristic
.
Uuid
.
Equals
(
rxUUID
))
if
(
characteristic
.
Uuid
.
Equals
(
rxUUID
))
{
{
Debug
.
WriteLine
(
"Found Read"
);
var
result
=
await
ReadOperationAsync
(
characteristic
);
// Read the value from the characteristic
var
temperatureData
=
await
characteristic
.
ReadAsync
();
// Convert the byte array to a string
var
temperatureValue
=
System
.
Text
.
Encoding
.
UTF8
.
GetString
(
temperatureData
.
data
);
Debug
.
WriteLine
(
$"Temperature Value:
{
temperatureValue
}
"
);
}
}
else
if
(
characteristic
.
Uuid
.
Equals
(
wxUUID
))
else
if
(
characteristic
.
Uuid
.
Equals
(
wxUUID
))
{
{
Debug
.
WriteLine
(
"Found Write"
);
const
string
data
=
"C"
;
await
WriteOperationAsync
(
characteristic
,
data
);
// Read the current value (if needed)
var
writeValueData
=
await
characteristic
.
ReadAsync
();
var
writeValue
=
System
.
Text
.
Encoding
.
UTF8
.
GetString
(
writeValueData
.
data
);
Debug
.
WriteLine
(
$"Current Write Value:
{
writeValue
}
"
);
}
}
}
}
}
}
var
transmissionService
=
services
.
FirstOrDefault
(
s
=>
s
.
Id
==
Guid
.
Parse
(
SendReceiveServiceUUID
));
var
readCharacteristic
=
await
transmissionService
.
GetCharacteristicAsync
(
Guid
.
Parse
(
rxUUID
));
var
result2
=
await
ReadOperationAsync
(
readCharacteristic
);
}
}
else
{
Debug
.
WriteLine
(
"Failed to connect to the device."
);
IsConnected
=
false
;
}
}
catch
(
DeviceConnectionException
ex
)
catch
(
DeviceConnectionException
ex
)
{
{
Debug
.
WriteLine
(
$"Connection error:
{
ex
.
Message
}
"
);
Debug
.
WriteLine
(
$"Connection error:
{
ex
.
Message
}
"
);
await
Application
.
Current
.
MainPage
.
DisplayAlert
(
"Connection Error"
,
$"Failed to connect:
{
ex
.
Message
}
"
,
"OK"
);
await
Application
.
Current
.
MainPage
.
DisplayAlert
(
"Connection Error"
,
$"Failed to connect:
{
ex
.
Message
}
"
,
"OK"
);
}
}
catch
(
TaskCanceledException
ex
)
catch
(
Exception
ex
)
{
Debug
.
WriteLine
(
$"General connection error:
{
ex
.
Message
}
"
);
await
Application
.
Current
.
MainPage
.
DisplayAlert
(
"Connection Error"
,
$"An error occurred:
{
ex
.
Message
}
"
,
"OK"
);
}
finally
{
// Ensure we reset connection state
if
(
_connectedDevice
==
null
)
{
{
Debug
.
WriteLine
(
$"TaskCanceledException:
{
ex
.
Message
}
"
);
await
Application
.
Current
.
MainPage
.
DisplayAlert
(
"Connection Error"
,
"Connection timed out."
,
"OK"
);
_connectedDevice
=
null
;
IsConnected
=
false
;
IsConnected
=
false
;
// Consider resetting any other related UI state
}
}
}
private
async
Task
<
string
>
ReadOperationAsync
(
ICharacteristic
characteristic
,
int
retryCount
=
3
)
{
for
(
int
attempt
=
1
;
attempt
<=
retryCount
;
attempt
++)
{
try
{
Debug
.
WriteLine
(
$"Attempt
{
attempt
}
: Reading from characteristic
{
characteristic
.
Uuid
}
"
);
var
temperatureData
=
await
characteristic
.
ReadAsync
();
var
temperatureValue
=
System
.
Text
.
Encoding
.
UTF8
.
GetString
(
temperatureData
.
data
);
Debug
.
WriteLine
(
$"Temperature Value:
{
temperatureValue
}
"
);
return
temperatureValue
;
}
}
catch
(
Exception
ex
)
catch
(
Exception
ex
)
{
{
Debug
.
WriteLine
(
$"General connection error:
{
ex
.
Message
}
"
);
Debug
.
WriteLine
(
$"Read attempt
{
attempt
}
failed:
{
ex
.
Message
}
"
);
await
Application
.
Current
.
MainPage
.
DisplayAlert
(
"Connection Error"
,
$"An error occurred:
{
ex
.
Message
}
"
,
"OK"
);
if
(
attempt
==
retryCount
)
{
Debug
.
WriteLine
(
"Max retry attempts reached for reading."
);
throw
;
}
else
{
await
Task
.
Delay
(
1000
);
}
}
}
}
return
null
;
}
}
private
async
Task
WriteOperationAsync
(
ICharacteristic
characteristic
,
string
sendData
,
int
retryCount
=
3
)
{
byte
[]
data
=
System
.
Text
.
Encoding
.
UTF8
.
GetBytes
(
sendData
);
for
(
int
attempt
=
1
;
attempt
<=
retryCount
;
attempt
++)
{
try
{
Debug
.
WriteLine
(
$"Attempt
{
attempt
}
: Writing to characteristic
{
characteristic
.
Uuid
}
"
);
await
characteristic
.
WriteAsync
(
data
);
Debug
.
WriteLine
(
"Write operation succeeded."
);
return
;
}
catch
(
Exception
ex
)
{
Debug
.
WriteLine
(
$"Write attempt
{
attempt
}
failed:
{
ex
.
Message
}
"
);
if
(
attempt
==
retryCount
)
{
Debug
.
WriteLine
(
"Max retry attempts reached for writing."
);
throw
;
}
else
{
await
Task
.
Delay
(
1000
);
}
}
}
}
private
async
void
OnDisconnect
()
private
async
void
OnDisconnect
()
{
{
if
(
_connectedDevice
!=
null
)
if
(
_connectedDevice
!=
null
)
...
@@ -435,6 +487,7 @@ namespace RobobinApp.ViewModels
...
@@ -435,6 +487,7 @@ namespace RobobinApp.ViewModels
_connectedDevice
.
Dispose
();
_connectedDevice
.
Dispose
();
_connectedDevice
=
null
;
_connectedDevice
=
null
;
IsConnected
=
false
;
IsConnected
=
false
;
ScanDevices
();
await
Application
.
Current
.
MainPage
.
DisplayAlert
(
"Disconnected"
,
"Device disconnected successfully."
,
"OK"
);
await
Application
.
Current
.
MainPage
.
DisplayAlert
(
"Disconnected"
,
"Device disconnected successfully."
,
"OK"
);
}
}
catch
(
Exception
ex
)
catch
(
Exception
ex
)
...
@@ -452,4 +505,50 @@ namespace RobobinApp.ViewModels
...
@@ -452,4 +505,50 @@ namespace RobobinApp.ViewModels
}
}
/**
*
var transmitService = services.FirstOrDefault(s => s.Id == Guid.Parse(SendReceiveServiceUUID));
Debug.WriteLine("Transmit service was accessed");
Debug.WriteLine($"{transmitService.Name} : {transmitService.Id}");
if (transmitService == null)
{
Debug.WriteLine("Transmit service wasn't found");
}
var characteristics = await transmitService.GetCharacteristicsAsync();
foreach (var characteristic in characteristics)
{
Debug.WriteLine($"{characteristic.Uuid} | {characteristic.Name}");
if (characteristic.Uuid.Equals(rxUUID))
{
var result = await ReadOperationAsync(characteristic);
}
else if (characteristic.Uuid.Equals(wxUUID))
{
//await WriteOperationAsync(characteristic);
}
}
}
catch (DeviceConnectionException ex)
{
Debug.WriteLine($"Connection error: {ex.Message}");
await Application.Current.MainPage.DisplayAlert("Connection Error", $"Failed to connect: {ex.Message}", "OK");
}
catch (TaskCanceledException ex)
{
Debug.WriteLine($"TaskCanceledException: {ex.Message}");
await Application.Current.MainPage.DisplayAlert("Connection Error", "Connection timed out.", "OK");
_connectedDevice = null;
IsConnected = false;
}
catch (Exception ex)
{
Debug.WriteLine($"General connection error: {ex.Message}");
await Application.Current.MainPage.DisplayAlert("Connection Error", $"An error occurred: {ex.Message}", "OK");
}
} **/
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment