Agent Emulator & Bluetooth Device Communications

Yay the Agent smart watch preview SDK is available. You can find it here:

http://www.agentwatches.com/

One of the 1st questions that springs to mind is how do we go about some sort of Bluetooth communication with anything from the emulator? Then I spotted this in the comments section of the Agent kickstarter page:

image

So that got me looking onto how this works so I set about following the instructions and here is the results of my findings:

PC Settings

image

The diagram above depicts how your host PC should be set up . Basically what you see here is that standard Bluetooth on the host machine is capable of providing COM Port based communication.

COM ports go back to the days of RS232 serial and Centronics parallel communication.They take care of the transmission of bytes between machines, Once you have a COM port open each device can send bytes down the wire.

To enable paired devices to open the COM port on your host PC you need to ensure that there is an incoming com port this is done in the Bluetooth settings:

image

You need one incoming COM port, this is done by tapping the add button and selecting incoming as the connection type.

Emulator settings

When you install the Agent SDK it installs the emulator with a standard settings configuration file if you have used the defaults then this config file will be located here:

C:\Program Files (x86)\Secret Labs\AGENT SDK\Emulator\v4.3\AGENT Emulator.exe.emulatorconfig

Open this file and you will see the following section

    <PhysicalSerialPort id="COM1">
      <ComPortHandle>Usart1</ComPortHandle>
      <PhysicalPortName>COM3</PhysicalPortName>
    </PhysicalSerialPort>

Here you set the PhysicalPortName to the name of the port that you have ben given in the Bluetooth settings,

You are now ready to start writing code within your agent application that accepts connections from external paired devices.

Paring a windows phone

image

One area where people get bogged down in when paring a phone with your local PC is that after paring the phone does not stay connected. They tap on the phone in the list and it says connected but then appears to drop the connection after a short period of time.

To understand this you have to ask yourself why would the phone want to be connected via Bluetooth in the first place.The answer being that there isn’t any reason for it. There are no shared services that are part of each OS that require a connection so when the phone realises this it drops the connection.

It’s not until your application opens a connection to a service on the paired PC that the connection becomes active.

From what I know and given this scenario it is not possible for the Agent application to initiate communications with the paired device.

The Agent Application

NOTE: The rest of the code mentioned here will be based upon the AGENT SDK v0.1.1 (June 2013, Preview Release) and will be subject to change as the SDK gets closer to it;s version 1 release.

Given that the Agent emulator has a virtual COM port all the work is carried out using Stream based objects. The System.IO.Ports.SerialPort object is derived from System.Stream, so if you are familiar with working with streams then this should be familiar territory.

To add a serial port to your code that uses the host Bluetooth connection you just add the following line of code:

var serial = new SerialPort(“COM1″);

To make the serial port ready for data then you just open it:

serial.Open();

Ok so now we need to know when data is being sent to the application.This is best done by adding an event handler to the DataReceived event.

serial.DataReceived += _serial_DataReceived;

static void _serial_DataReceived(object sender, SerialDataReceivedEventArgs e)
{

//get data here

}

Agent app wise that’s it for this blog post if you want to see more implementation then have a look at the demonstration code linked at the bottom of this article.

The Windows Phone Application

A good place to start when writing a Bluetooth application for Windows Phone 8 is the Bluetooth App to device sample.

NOTE: One thing to remember when writing a Windows Phone application that uses Bluetooth is to set the ID_CAP_PROXIMITY app capability within your WMAppManifest.xml file (double click the file within VisualStudio) :

image

My example uses some simplified code to connect to the target machine and then send some data to the agent application.

image

When the connect button is pressed the following code is called:

private async Task<bool> SetupDeviceConn()
{
   
//Connect to your paired host pc using BT + StreamSocket (over RFCOMM)
    PeerFinder.AlternateIdentities[“Bluetooth:PAIRED”] = “”
;

    var devices = await PeerFinder.FindAllPeersAsync();

    if (devices.Count == 0)
    {
       
MessageBox.Show(“No paired device”
);
       
await Windows.System.Launcher.LaunchUriAsync(new Uri(“ms-settings-bluetooth:”
));
       
return false
;
    }

    PeerInformation peerInfo = devices.FirstOrDefault(c => c.DisplayName.Contains(“****YOUR PC NAME HERE***”));
   
if (peerInfo == null
)
    {
       
MessageBox.Show(“No paired device”
);
       
await Windows.System.Launcher.LaunchUriAsync(new Uri(“ms-settings-bluetooth:”
));
       
return false
;
    }

    _socket = new StreamSocket();
   
await _socket.ConnectAsync(peerInfo.HostName, “{00001101-0000-1000-8000-00805f9b34fb}”
);

    _dataWriter = new DataWriter(_socket.OutputStream);

    return true;
}

This code is initialises the peer finder service then gets a list of  devices. After that it users a filter of the list to find the peer that contains the name that is given by you (replace the ****YOUR PC NAME HERE*** with the name of your PC as it appears in the devices list).

If the function returns true then a socket connection to the Agent application has been made.

The second button “send stuff” sends the contents of the text box to the agent application. The button tap  calls the following code:

private async void CmdSendStuff_OnTap(object sender, System.Windows.Input.GestureEventArgs e)
{     _dataWriter.WriteString(txtText.Text + '');     await _dataWriter.StoreAsync();
}

In this case the sending of data is simple because we have the use of the DataWriter class to make things easy for us.

The source code can be found here:

https://projects.developer.nokia.com/agentexamples/browser/agentexamples

Useful Links:

http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207007(v=vs.105).aspx

http://forums.agentwatches.com

About these ads
Tagged with: ,
Posted in Agent, C#, Windows 8, Windows Phone
29 comments on “Agent Emulator & Bluetooth Device Communications
  1. […] Agent Emulator & Bluetooth Device Communications (Mike Hole) […]

  2. […] Interested developers can find the SDK here, and read more at Mike Hole’s blog here. […]

  3. […] Interested developers can find the SDK here, and read more at Mike Hole’s blog here. […]

  4. […] Interested developers can find the SDK here, and read more at Mike Hole’s blog here. […]

  5. […] Hole, a developer for Windows Phones has started trying out the SDK and has posted a write up on his experiences so […]

  6. mikehole says:

    If you are wondering where the comments are it’s because they are pingbacks and the site does not show them 😐

  7. […] 经由:Engadget 引用来源:Agent Watches、Mike Hole […]

  8. […] 經由:Engadget引用來源:Agent Watches、Mike Hole […]

  9. […] 经由:Engadget 引用来源:Agent Watches、Mike Hole […]

  10. […] 早前我们报导过的 Kickstarter 集资产品 Agent 智能型手表,已在约五日前超额十倍有多地完成集资。虽然这款产品预计在 12 月才会大量生产,不过其制造商 Secret Labs 已经急不及待地公布它的 SDK。他们指出这个 SDK 是「世界级」,Agent 手表的 app 是可以经由 Microsoft Visual Studio 2012 以 C# 语言去编写。另外 Windows Phone 8 开发者 Mike Hole 已经在他的部落格上写了有关这个 SDK 的简单说明,有兴趣为这只手表开发程序的话可以看看啊。 经由:Engadget 引用来源:Agent Watches、Mike Hole […]

  11. […] 经由:Engadget 引用来源:Agent Watches、Mike Hole […]

  12. […] a June post, @roguecode wrote a 3 part series to get started. There is also a post by @mikehole for communicating with Bluetooth devices then there is also a Tetris clone from Watch App and Watch Face Showcase and a Analog watch face […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: