NuiBlue is built on the InTheHand.Net library from 32feet.NET allowing for simplified Bluetooth Control.

 

Quick Started Guide

Downloading Images from Bluetooth Device

This guide will demonstrate how to use the BluetoothController class to download Images from a bluetooth enabled device. The image will then be presented in a windows touch friendly scatterview.

Note: You will need to have Visual Studio 2010 installed along with Surface Toolkit for Windows Touch Beta installed

  1. Create a new project in visual studio 2010. Enter the name "NuiBlueSurface", Select a save location and click OK.
  2. In the solution explorer. Open the SufaceWindow1.xaml file.
  3. Add Width="1024" and Height="768" to the end of first tag titled s:SurfaceWindow. This will set the size of your window when the application is run.
  4. Insert The Following  XAML code:
     
    <DockPanel>
            <UniformGrid Background="Black" Height="50" DockPanel.Dock="Top" Columns="3">
                <s:SurfaceButton Content="Download" Foreground="White" Click="SurfaceButton_Click" />
                <TextBox x:Name="txtStatus"></TextBox>
            </UniformGrid>
            <s:ScatterView Name="MainScatterView"/>
    </DockPanel>

    What this code does is is add a UniformGrid and a ScatterView to a DockPanel. The Dock Panel is used to dock the uniform grid, with a download button and status text, to the top of the window, and The ScatterView will occupy the rest of the visible area.
  5. In the solution explorer. Right-click on References and click on Add Reference. Navigate to the NuiBlue source bin folder and add NuiBlue.dll, InTheHand.Net.Personal.dll, and Brecham.Obex.dll.
  6. In the solution explorer. Right-click the SufaceWindow1.xaml file and click on view code.
  7. In the Code Editor. Include the NuiBlue library in class.

    using NuiBlue.Bluetooth;

  8. Create 2 delegate methods that will handle cross thread communication that an asynchronous transfer will cause. The delegate methods are used to get status updates and downloaded image data.

    public delegate void Status(object sender, string status);
    public delegate void downloadedImage(object sender, BluetoothEventArgs e);

  9. Open the design view. Double-click on the Surface Button to create a new Click event for it.
  10. Add this code to the new click event method.

    BluetoothController bc = new BluetoothController();
    bc.dataDownloaded += new BluetoothController.DataDownloadedHandler(bc_dataDownloaded);
    bc.StatusUpdate += new BluetoothController.StatusEventHandler(bc_StatusUpdate);
    bc.setAddress("<address>");
    bc.setPin("<pin>");
    bc.setMaxFiles(1);
    bc.downloadData(true, BluetoothController.Type.image, null, null);

    • The bluetooth controller for interaction with remote bluetooth device.
    • The event handlers used in the example receive status updates and downloaded data information.
    • The device address and the pin (pin you will enter when you connect for the first time) is assigned to the controller
    • The MaxFiles needs to be set when using the automated download to limit the number of files.
    • The DownloadData method takes 4 arguments:
      • Asynchronous download (boolean)
      • Data type to download (BluetoothController.Type)
      • Folder to download from (string) - pass null to attempt to select folder automatically
      • File to Download (string) - pass null to attempt to download the most recent file
  11. Add The event methods called by the bluetoothController event handlers.

    void bc_StatusUpdate(object sender, string status)
    {
        Dispatcher.BeginInvoke(new Status(statusUpdate), sender, status);
    }
    void bc_dataDownloaded(object sender, BluetoothEventArgs e)
    {
        Dispatcher.BeginInvoke(new downloadedImage(addImage), sender, e);
    }

    These 2 methods receive data from the bluetooth  controller and marshal that data onto the GUI thread.
  12. Add the 2 methods that actually display received data on the screen.

    void statusUpdate(object sender, string status)
    {
        txtStatus.Text = status;
    }
    void addImage(object sender, BluetoothEventArgs e)
    {
        try
        {
            Image image = new Image();
            image.Source = new BitmapImage(new Uri(e.FilePath));

            MainScatterView.Items.Add(image);
        }
        catch (Exception ex)
        {
            MessageBox.Show("**" + e.FileName + "**" + ex.Message);
        }
    }

  13. Press F6 to build the project. A bin folder should now be created for your project.
  14. Locate the folders.xml in the source bin folder and copy it to your projects bin folder.
    Note: this step will be changed in future updates.
  15. Press F5 to run.

Last edited Aug 11, 2011 at 3:46 AM by jpdev832, version 11

Comments

No comments yet.