Thursday, July 29, 2010

Enumerating Bluetooth Devices from .NETCF

I recently had a project where I needed to send data to Bluetooth devices. The client applications where to run in several platforms, currently only J2ME (Nokia phones) and Windows Mobile phones. Windows Mobile actually offers a pretty decent Bluetooth stack but not all devices use this. One of the devices I needed to use used the Widcomm stack. Luckily, there is an open source project called 32feet.NET which came in very handy for providing a layer over the 2 different stacks I use. The 32feet.NET library was also incredibly easy and fun to use.

In this article I'd like to demonstrate how to enumerate Bluetooth devices using .NETCF and the 32feet.NET library. The following code will work on both Microsoft and Widcomm Bluetooth stacks:

using System.Diagnostics;
using InTheHand.Net.Sockets;
 
namespace BluetoothSample
{
    static class Program
    {
        private static void Main()
        {
            BluetoothDeviceInfo[] devices;
            using (BluetoothClient sdp = new BluetoothClient())
                devices = sdp.DiscoverDevices();
 
            foreach (BluetoothDeviceInfo deviceInfo in devices)
            {
                Debug.WriteLine(string.Format("{0} ({1})",deviceInfo.DeviceName, deviceInfo.DeviceAddress));
            }
        }
    }
}

An interesting thing I had to consider for this project was the CPU architecture or endianness of the device I'm running on and the device I'm sending data to. I needed to reverse the byte order of the numeric data I sent and received.

Monday, July 26, 2010

Cropping an Image in .NETCF

I've seen some people ask how to crop an image in .NET Compact Framework in the community. Although the operation is pretty simple, I'll share a code snippet anyway.

public static Image Crop(Image image, Rectangle bounds)
{
    Image newImage = new Bitmap(bounds.Width, bounds.Height);
 
    using (Graphics g = Graphics.FromImage(newImage))
        g.DrawImage(image, 
                    new Rectangle(0, 0, newImage.Width, newImage.Height), 
                    bounds, 
                    GraphicsUnit.Pixel);
 
    return newImage;
 
}

What the code above does is to create a new image and draw part of the source image specified in the bounds to the new image.

Monday, July 12, 2010

.NET Compact Framework 3.5 Data Driven Applications

I recently discovered a new book called .NET Compact Framework 3.5 Data Driven Applications. I've so far only read a few chapters but it seems like a promising book providing a lot of examples.

Here's a downloadable free chapter :
http://www.packtpub.com/data-driven-applications-with-.net-compact-framework-3-5/book?utm_source=christian-helle.blogspot.com&utm_medium=bookrev&utm_content=blog&utm_campaign=mdb_003787

I plan to write a detailed review of the book once I'm done reading it