Use the Samsung Accessory Protocol (SAP) API

  1. Create a new Tizen Wearable application project.

    Note

    If you are new to the Tizen C# application development, you might want to have a look the Creating Your First Tizen .NET Application article.

    In the Visual Studio go to File -> New -> Project...

    Choose the “Tizen Wearable App” which can be found in the Visual C# -> Tizen group.

    Choose and enter both the name of the project and its location. Click the OK button.

  2. Install the Samsung.Sap NuGet Package.

    Go to Solution Explorer view, right click on your project, then select the “Manage Nuget packages…” option. If you haven’t added the Tizen nuget source before, please click the gear icon next to the “Package source” menu.

    Then please add a new repository with a name “Tizen MyGet” and the address https://tizen.myget.org/F/dotnet/api/v3/index.json

    Choose Tizen repository, and under the “Browse” tab search for Samsung.Sap package and install it.

  3. Add the Accessory Service Profile.

    Communicating with an Android application requires a declaration of descriptions about the Accessory Service Profile. The profile is declared in a separate XML file in the /res/xml folder of the application project. The actual path of the XML file can be described with ‘meta data’ in the tizen-manifest.xml file.

    A. Add the accesoryservice.xml file in the /res/xml folder.

    For an example file, see the following sample Accessory Service Profile. You can modify the id value of the serviceProfile element to define your application. The id value must be unique globally and it is also used in the Tizen side application to identify the service profile.

    Note

    The service profile ID must start with ‘/’, followed by [0-9], [a-z], ‘_’, and ‘/’ (as delimiter). The maximum length is 30 characters. For more information about the XML file, see this.

    
    <resources>
    	<application name="MyApplication">
    		<serviceProfile id="/org/example/myapp/my_message" name="MyMessage"
    		                role="consumer"  version="1.0">
    			<supportedTransports>
    				<transport type="TRANSPORT_BT"/>
    				<transport type="TRANSPORT_WIFI"/>
    			</supportedTransports>
    			<serviceChannel id="110" dataRate="low" priority="low"
    			                reliability="enable">
    			</serviceChannel>
    		</serviceProfile>
    	</application>
    </resources>
    
    

    Click the File > Save accessoryservices.xml As… Click the small triangle by the “Save” button and select “Save with Encoding…” option. Confirm the overwriting of the existing file, and select the "Unicode (UTF-8 without signature) – Codepage 65001” as the encoding. Click the OK button.

    B. Add meta data to define the Accessory Service Profile XML file location.

    • Open the tizen-manifest.xml file in the Tizen manifest editor, and select the Advanced tab.

    • In the Meta Data section, click Add, define the key as accessory-services-location and the value as /res/xml/accessoryservices.xml, and click OK.

  4. Add the required privilege.

    To use the SAP API, the http://developer.samsung.com/tizen/privilege/accessoryprotocol privilege must be added in the tizen-manifest.xml file.

    1. Code

      Open the App.cs file and add “using Samsung.Sap;” at the top.

      Replace the App class with the following class definition:

      
              public class App : Application
              {
              private Agent Agent;
              private Connection Connection;
      
              public App()
              {
                  MainPage = new CirclePage
                  {
                      Content = new StackLayout
                      {
                          VerticalOptions = LayoutOptions.Center,
                          Children = {
                              new Button {
                                  Text = "Connect",
                                  Command = new Command(Connect)
                              }
                          }
                      }
                  };
              }
      
              private async void Connect()
              {
                  try
                  {
                      Agent = await Agent.GetAgent("/sample/hello");
                      var peers = await Agent.FindPeers();
                      if (peers.Count() > 0)
                      {
                          var peer = peers.First();
                          Connection = peer.Connection;
                          Connection.DataReceived -= Connection_DataReceived;
                          Connection.DataReceived += Connection_DataReceived;
                          await Connection.Open();
                      }
                      else
                      {
                          Toast.DisplayText("Any peer not found");
                      }
                  }
                  catch (Exception ex)
                  {
                      Toast.DisplayText(ex.Message);
                  }
              }
      
              private void Connection_DataReceived(object sender, DataReceivedEventArgs e)
              {
                  Toast.DisplayText("Message received");
              }
          }
      
  5. Build

    Connect a Galaxy Watch to the computer. When the device is connected and detected, you will be able to select it as the target for application launch. Press Ctrl-F5 to run the application without debugger (or select “Start without debugging” from the Debug menu).