Click here to Skip to main content
15,867,568 members
Articles / Mobile Apps / Windows Mobile
Tip/Trick

A Simple Bluetooth Application

Rate me:
Please Sign up or sign in to vote.
4.64/5 (9 votes)
16 Dec 2013CPOL 45K   13   7
This tip enables you to discover bluetooth features and how we can use its API to develop a simple application.

Introduction

If you want to know how to use the Bluetooth features to send data using your SmartPhone, you are at the right place. Here, we will learn that together step by step. 

Background

Bluetooth is a wireless communication technology that devices use to communicate with each other within just a 10 meter. So we must include ID_CAP_PROXIMITY and ID_CAP_NETWORKING capabilities in our application manifest.

Using the Code

First of all, we must discover other devices and applications using the PeerFinder class.

C#
StreamSocket socket;//socket is created to enable app-to-app and  app-to-device communication
public async void FindPeers()
{
    // find other devices
    IReadOnlyList<PeerInformation> peers = await PeerFinder.FindAllPeersAsync();
    if (peers.Count > 0)
    {   // establish connection with the fist peer
        socket = await PeerFinder.ConnectAsync(peers[0]); // stop finding 
        			// in order to conserve battery life
        // stop finding in order to conserve battery life
         PeerFinder.Stop();
    }
}

Then, the application searches to see if it is running an instance of itself.

C#
public void Advertise()
{
    PeerFinder.DisplayName ="SuperMario";
    PeerFinder.Start();
}

Now, it is important to use the StreamSocket instance to connect to the peer application.

C#
public void MySampleApp()
{
    PeerFinder.ConnectionRequested += PeerFinder_connexionRequested;
}

private void PeerFinder_connexionRequested
	(object sender, ConnectionRequestedEventArgs args)
{
    MessageBoxResult result = MessageBox.Show
    (string.Format("{0} is trying to connect. 
    Would you like to accept?",args.PeerInformation.DisplayName),
    "My bleutooth Chat App",MessageBoxButton.OKCancel );
    if (result == MessageBoxResult.OK)
    {
        socket.Connect(args.PeerInformation);
    }
} 

Great! So we want to read the incoming messages that are transmitted from another device or peer applications. That is why we use the DataReader class to load and read data.

C#
DataReader dataReader = new DataReader(socket.InputStream);
await dataReader.loadAsync(4);// get the size of message
uint messageLen =(uint)DataReader.readInt32();
await dataReader.loadAsync(messageLen)//send the message
String Message = dataReader.ReadString(messageLen); 

And we use the DataWriter class in order to send outgoing messages to an external device or peer application.

C#
DataWriter dataWriter = new DataWriter(socket.OutputStream);
// send the message length first
dataWriter.WriteInt32(Message.length);
await dataWriter.StoreAsync();
//send the actual message
dataWriter.WriteString(message);
await dataWriter.StoreAsync();

Any comments are most welcome.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
Tunisia Tunisia
Microsoft Certified Professional, Big Data and Cloud Architect.
Mail : hadrichmed@gmail.com

Comments and Discussions

 
QuestionUsing Bluetooth Microphone to record Audio Pin
Sriram Mani23-Aug-15 23:12
Sriram Mani23-Aug-15 23:12 
QuestionI can't receive the data Pin
Member 108652224-Jun-14 17:35
Member 108652224-Jun-14 17:35 
QuestionWhat's ID_CAP_PROXIMITY and ID_CAP_NETWORKING ?? Pin
Med_Dev17-Apr-14 7:59
Med_Dev17-Apr-14 7:59 
AnswerRe: What's ID_CAP_PROXIMITY and ID_CAP_NETWORKING ?? Pin
Hadrich Mohamed17-Apr-14 8:14
professionalHadrich Mohamed17-Apr-14 8:14 
GeneralRe: What's ID_CAP_PROXIMITY and ID_CAP_NETWORKING ?? Pin
Ramsin9-Aug-16 8:06
Ramsin9-Aug-16 8:06 
QuestionBluetooth use. Pin
theChosenOnePL9-Dec-13 8:22
professionaltheChosenOnePL9-Dec-13 8:22 
AnswerRe: Bluetooth use. Pin
Hadrich Mohamed27-Dec-13 10:20
professionalHadrich Mohamed27-Dec-13 10:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.