Use Web Proxy to Access the Internet When a Samsung Galaxy Watch and a Phone are Connected with Bluetooth
Juwon Ahn
Staff Engineer
You can connect to the Internet or communicate with other devices after you set up a network. When Galaxy Watch is not connected to a mobile phone, you can still be connected to the Internet through the watch's Wi-Fi or cellular network.
If a Galaxy Watch is connected to a phone, the watch's Wi-Fi is automatically disabled internally and the watch communicates with a network through the phone. This reduces watch battery consumption.
When not paired with a mobile phone, a Galaxy Watch use its own data connectivity (Wi-Fi or cellular networks) to transmit and receive http and https packets. However, you can apply web proxy to the watch so it can perform these functions when the watch is connected to a phone with Bluetooth.
This blog describes how you can use web proxy to access the Internet whether or not Galaxy Watch is connected to a mobile phone. We'll learn how to download a file from a host server and request data from a specified resource.
Declare the necessary privileges
To access the Internet and use ConnectionManager API, declare the following required privileges in your application's manifest file:
<privileges>
<privilege>http://tizen.org/privilege/internet</privilege>
<privilege>http://tizen.org/privilege/network.get</privilege>
</privileges>
Note that http://tizen.org/privilege/internet
requires a user permission, because users can be charged further fees to access the Internet.
Info: To learn how to get a user's permission, see the Galaxy Watch: working with user privacy related permissions in Tizen .NET Applications tutorial. You must be logged in with your Samsung account to view this tutorial.
Check connectivity
use
ConnectionManager
API
With the type and state of the current connectivity, we can find out whether a Galaxy Watch is connected to a phone and if Wi-Fi is activated on the watch.
Check the type and state of the current network connectivity with the ConnectionManager
API.
using Tizen.Network.Connection;
ConnectionItem connection = ConnectionManager.CurrentConnection;
Tizen.Log.Info(Program.LOG_TAG,
"Connection(" + currentConnection.Type + ", " + currentConnection.State + ")");
if (connection.Type == ConnectionType.Disconnected) {
// There is no available connectivity
} else if (connection.Type == ConnectionType.Ethernet) {
// When Galaxy Watch has a Bluetooth connection to a mobile phone
} else if (connection.Type == ConnectionType.Cellular) {
// When Galaxy watch communicate with a network through cellular network,
// without access to a smartphone
} else if (connection.Type == ConnectionType.WiFi) {
// When Galaxy watch communicate with a network through Wi-Fi network,
// without access to a smartphone
}
Use web proxy
When Galaxy Watch has a Bluetooth connection to a smart phone, you can enable your application to access the Internet by using web proxy.
To start, get current proxy info by calling ConnectionManager.GetProxy()
, and then set web proxy information to access the Internet as follows:
else if (connection.Type == ConnectionType.Ethernet) {
// Get the current proxy information
var proxyAddress = ConnectionManager.GetProxy(AddressFamily.IPv4);
WebProxy webproxy = new WebProxy(proxyAddress, true);
// Set proxy information to the HttpWebRequest
request.Proxy = webproxy;
}
Access the Internet
As mentioned, we'll get data and download a file from a host server.
1. Request data
You can create a HttpWebRequest
instance with the URI of a specified resource to request resources such as a Web page or a file from a host server, and then request data using the GET
method.
using System.Net;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://developer.samsung.com/tizen");
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content and print log.
LabelText += responseFromServer;
Log.Info(Program.LOG_TAG, "responseFromServer :" + responseFromServer);
// Clean up the streams and the response.
reader.Close();
response.Close();
When you press the GetData
button, you see the following screen:
through a connected phone | through Wi-Fi |
---|---|
2. Download a file
You can download a file from a host server using WebClient
.
WebClient webClient = new WebClient();
if (ConnectionManager.CurrentConnection.Type == ConnectionType.Ethernet)
{
// In case that Samsung Galaxy Watch is connected to a mobile phone,
// Use web proxy
webClient.Proxy = new WebProxy(ConnectionManager.GetProxy(AddressFamily.IPv4), true);
}
webClient.DownloadFileCompleted += WebClient_DownloadFileCompleted;
webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
string DownloadsFolder = Path.Combine(Tizen.Applications.Application.Current.DirectoryInfo.Data, "Downloads");
string pathToNewFile = Path.Combine(DownloadsFolder, Path.GetFileName(FileToDownload));
// Download a file asynchronously
webClient.DownloadFileAsync(new Uri("https://archive.org/download/BigBuckBunny_328/BigBuckBunny_512kb.mp4"),
pathToNewFile);
Run in the background
Tizen restricts apps from running in the background to save the device's limited resources, such as memory and battery, and to create a better app execution environment. Galaxy watches generally go into an idle state, as the screen turns off when there is no user input. However, if you want to play music, download files, or exchange data with other devices, the watch should continue to perform even if the app runs in the background.
In the following cases, Tizen allows apps to keep running in the background:
Category | Description |
---|---|
Media | Play audio, recording, and output streaming video |
Download | Download data with the Tizen Download manager API |
Background network | Process general network operations |
Location | Process location data |
Sensor (context) | Process context data from sensors such as gesture |
IoT Communication and connectivity | Communicate between external devices (such as Wi-Fi and Bluetooth) |
Info: For details, see this article on how an application is allowed to run in the background.
Specify the specific background category in the manifest file to tell the system which background category app you want to run.
Sometimes, a sample app needs to continue to download a file in the background. To do this, we're going to declare background-category
in tizen-manifest.xml
file as follows:
<ui-application appid="org.tizen.example.AccessTheInternet"
exec="AccessTheInternet.dll" multiple="false"
nodisplay="false" taskmanage="true" type="dotnet" launch_mode="single">
<background-category value="background-network" />
</ui-application>
When you press the Download
button, you see the following screen.
| | | |
Sample app
To download a sample app, see WebProxySample
Use what you've learned in this blog to access the Internet, whether or not your Galaxy Watch is connected to a mobile phone. Why not try it today?