Quantcast
Channel: Windows Phone – Mike Hole
Viewing all articles
Browse latest Browse all 4

Agent Emulator & Bluetooth Device Communications

0
0

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



Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images