Use Web Proxy to Access the Internet When a Samsung Galaxy Watch and a Phone are Connected with Bluetooth
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?