Last active
February 3, 2021 21:42
-
-
Save Kaspic/79b8ab7528f149909164f6f161654914 to your computer and use it in GitHub Desktop.
Samsung Galaxy Watch Companion App Tutorial - Part 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8" ?> | |
<resources> | |
<application name="GalaxyConsumer"> | |
<serviceProfile id="/com/example" name="GalaxyConsumer" role="consumer" version="1.0"> | |
<supportedTransports> | |
<transport type="TRANSPORT_BT"/> | |
</supportedTransports> | |
<serviceChannel id="330" dataRate="low" priority="low" reliability="enable"> | |
</serviceChannel> | |
</serviceProfile> | |
</application> | |
</resources> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8" ?> | |
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" | |
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | |
x:Class="GalaxyConsumer.MainPage"> | |
<ContentPage.Content> | |
<StackLayout | |
HorizontalOptions="FillAndExpand" | |
Orientation="Vertical" | |
VerticalOptions="FillAndExpand"> | |
<Button | |
x:Name="connectButton" | |
Text="Connect" | |
Clicked="On_Connect_Clicked" | |
HorizontalOptions="FillAndExpand" /> | |
<Button | |
x:Name="disconnectButton" | |
Text="Disconnect" | |
Clicked="On_Disconnect_Clicked" | |
HorizontalOptions="FillAndExpand"/> | |
</StackLayout> | |
</ContentPage.Content> | |
</ContentPage> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Samsung.Sap; | |
using System; | |
using System.Linq; | |
using Xamarin.Forms; | |
using Xamarin.Forms.Xaml; | |
namespace GalaxyConsumer | |
{ | |
[XamlCompilation(XamlCompilationOptions.Compile)] | |
public partial class MainPage : ContentPage | |
{ | |
private Agent agent; | |
private Peer peer; | |
private Connection connection; | |
private bool isSearchRunning; | |
public MainPage() | |
{ | |
InitializeComponent(); | |
} | |
private void On_Connect_Clicked(object sender, EventArgs e) | |
{ | |
if (peer == null && !isSearchRunning) | |
{ | |
SearchForPeersAndConnectAsync(); | |
} | |
} | |
private void On_Disconnect_Clicked(object sender, EventArgs e) | |
{ | |
Disconnect(); | |
} | |
private async void SearchForPeersAndConnectAsync() | |
{ | |
try | |
{ | |
isSearchRunning = true; | |
agent = await Agent.GetAgent("/com/example"); | |
var peers = await agent.FindPeers(); | |
if (peers.Count() > 0) | |
{ | |
peer = peers.First(); | |
connection = peer.Connection; | |
connection.DataReceived -= Connection_DataReceived; | |
connection.DataReceived += Connection_DataReceived; | |
connection.StatusChanged -= Connection_StatusChanged; | |
connection.StatusChanged += Connection_StatusChanged; | |
await connection.Open(); | |
ShowToastMessage("Connection success, you can now receive data"); | |
} | |
else | |
{ | |
ShowToastMessage("Error while searching for peer. Is the bluetooth connection enabled?"); | |
} | |
} | |
catch (Exception) | |
{ | |
ShowToastMessage("Error while connecting to peer"); | |
Disconnect(); | |
} | |
finally | |
{ | |
isSearchRunning = false; | |
} | |
} | |
private void Connection_StatusChanged(object sender, ConnectionStatusEventArgs e) | |
{ | |
if (e.Reason == ConnectionStatus.ConnectionClosed || e.Reason == ConnectionStatus.ConnectionLost) | |
{ | |
Disconnect(); | |
} | |
} | |
private void Disconnect() | |
{ | |
if (connection != null) | |
{ | |
connection.DataReceived -= Connection_DataReceived; | |
connection.StatusChanged -= Connection_StatusChanged; | |
connection.Close(); | |
} | |
connection = null; | |
peer = null; | |
agent = null; | |
} | |
private void Connection_DataReceived(object sender, Samsung.Sap.DataReceivedEventArgs receivedEventArgs) | |
{ | |
var data = System.Text.Encoding.ASCII.GetString(receivedEventArgs.Data); | |
ShowToastMessage(data); | |
} | |
private void ShowToastMessage(string message) | |
{ | |
Tizen.Wearable.CircularUI.Forms.Toast.DisplayText(message, 3000); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment