About Bluetooth,OpenNETCF,32feet

Last post 11-22-2011 1:19 by Nemo. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 11-07-2011 3:31

    • Nemo
    • Top 500 Contributor
    • Joined on 11-06-2011
    • Shanghai
    • Posts 3

    About Bluetooth,OpenNETCF,32feet

    I use a USB bluetooth dongle connect to PC,it's been enumerated as a COM device,now i want to develop an application communicate with a bluetooth module,the device searching is finished such as:

    using System.Net.Sockets;
    using OpenNETCF;
    using OpenNETCF.Net;
    using OpenNETCF.Net.Bluetooth;
    using OpenNETCF.Net.Sockets;


    private void Search_Click(object sender, EventArgs e)
    {
        BluetoothClient blueToothClient = new BluetoothClient();
        textBox1.AppendText("Discovering Devices... ");
    &nbsp;&nbsp;&nbsp; comboBox1.Items.Clear();</P> <P>&nbsp;&nbsp;&nbsp; bluetoothDeviceInfo = blueToothClient.DiscoverDevices(6);</P> <P>&nbsp;&nbsp;&nbsp; foreach (BluetoothDeviceInfo bDi in bluetoothDeviceInfo)
    &nbsp;&nbsp;&nbsp; {
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //if (bDi.DeviceName == "IT-300 BT Dev")//i set my bluetooth module as "IT-300 BT Dev"
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; comboBox1.Items.Add(bDi.DeviceName);
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; comboBox1.SelectedIndex = 0;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; textBox1.AppendText(bDi.DeviceID + " " + bDi.DeviceName + " ");
    &nbsp;&nbsp;&nbsp; }
    &nbsp;&nbsp;&nbsp; comboBox1.SelectedIndex = comboBox1.Items.IndexOf("IT-300 BT Dev");</P> <P>&nbsp;&nbsp;&nbsp; textBox1.AppendText(bluetoothDeviceInfo.Length + " Devices found. ");</P> <P>&nbsp;&nbsp;&nbsp; if (comboBox1.SelectedIndex == 0)
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; textBox1.AppendText("Find IT-300 BT Dev!");
    &nbsp;&nbsp;&nbsp; else
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; textBox1.AppendText("Did not find IT-300 BT Dev!");</P> <P>&nbsp;&nbsp;&nbsp; textBox1.AppendText(" " + System.Environment.NewLine);
    }

    Now,if the bluetooth module's pairing code is "1234",then how to finish the pairing process,and when the pairing is finished wether the communication can be read/write the COM port?someone's kind to provide me a example?

    this code is base on OpenNETCF,it's said that 32feet is another choice,but i didn't find the method,is there someone kind to give me some help?

    魑魅魍魉
  • 11-07-2011 14:28 In reply to

    Re: About Bluetooth,OpenNETCF,32feet

     Hi.

    For bluetooth communication I use 32feet library.

    First of all you have to "discover" devices using something like this:

                InTheHand.Net.Sockets.BluetoothClient bc = null;
                InTheHand.Net.Sockets.BluetoothDeviceInfo[] deviceInfo = null;
                try
                {
                    bc = new InTheHand.Net.Sockets.BluetoothClient();
                    deviceInfo = bc.DiscoverDevices();
                    if (deviceInfo.GetLength(0) != 0)
                    {
                        List<object> listData = new List<object>();
                        foreach (InTheHand.Net.Sockets.BluetoothDeviceInfo bdinfo in deviceInfo)
                        {
                            listData.Add(bdinfo.DeviceName + " {" + bdinfo.DeviceAddress.ToInt64().ToString() + "}");
                        }

                         /// SHOW BLUETOOTH LIST AND SELECT ONE...
                    }
                    else
                        MessageBox.Show("no bluetooth device found");
                }
                finally
                {
                    if (bc != null)
                    {
                        bc.Close();
                        bc.Dispose();
                        bc = null;
                    }
                }

     

     

    Then, when you have the device address, you can pair and print to it

                InTheHand.Net.BluetoothAddress deviceAddress = InTheHand.Net.BluetoothAddress.None;
                InTheHand.Net.BluetoothEndPoint ep = null;
                InTheHand.Net.Sockets.BluetoothClient bc = null;
                System.Net.Sockets.NetworkStream stream = null;

                    InTheHand.Net.BluetoothAddress bthAddress = null;

                    if (address == 0)
                    {
                        throw new Exception("no printer found");
                    }
                    else
                        bthAddress = new InTheHand.Net.BluetoothAddress(address); // THIS ADDRESS IS THE DEVICE ADDRESS

                    ep = new InTheHand.Net.BluetoothEndPoint(bthAddress, InTheHand.Net.Bluetooth.BluetoothService.SerialPort);
                    bc = new InTheHand.Net.Sockets.BluetoothClient();

                        InTheHand.Net.Bluetooth.BluetoothSecurity.SetPin(bthAddress, "0000"); // SET PIN

                    bc.Connect(ep);
                    stream = bc.GetStream();
                }

                try
                {

                    string line = " TEST LINE";

                    System.Text.Encoding encoding = new System.Text.ASCIIEncoding();
                    byte [] array = encoding.GetBytes(line);
                    stream.Write(array, 0, array.GetLength(0));

                        byte [] array = {13,10};
                        stream.Write(array, 0, array.GetLength(0));
                }
                catch (Exception ex)
                {
                    logger.LogError("SendToPrinter", "", ex.Message);
                    throw ex;
                }
                finally
                {

                    if (stream != null)
                    {
                        stream.Close();
                        stream = null;
                    }
               
                    if (bc != null)
                    {
                        bc.Close();
                        bc.Dispose();
                        bc = null;
                    }
                   
                }
     

  • 11-22-2011 1:19 In reply to

    • Nemo
    • Top 500 Contributor
    • Joined on 11-06-2011
    • Shanghai
    • Posts 3

    Re: About Bluetooth,OpenNETCF,32feet

    sorry,the poor format:

     put it here again:

     private void Search_Click(object sender, EventArgs e)
    {
        textBox1.AppendText("Discovering Devices...\n");
        BluetoothDeviceInfo[] bluetoothDeviceInfo = blueToothClient.DiscoverDevices(6);;
        comboBox1.Items.Clear();
        deviceAddressesDictionary.Clear();
       
        foreach (BluetoothDeviceInfo bDi in bluetoothDeviceInfo)
        {
            comboBox1.Items.Add(bDi.DeviceName);
            deviceAddressesDictionary[bDi.DeviceName] = bDi.DeviceID;
            textBox1.AppendText(bDi.DeviceID + "\t" + bDi.DeviceName + "\n");
        }

        comboBox1.SelectedIndex = comboBox1.Items.IndexOf("IT-300 BT Dev");
        textBox1.AppendText(bluetoothDeviceInfo.Length + " Devices found.\n");

        if (comboBox1.SelectedIndex == 0)
            textBox1.AppendText("找到IT-300 BT Dev!");
        else
            textBox1.AppendText("没有找到IT-300 BT Dev!");

        textBox1.AppendText("\r\n" + System.Environment.NewLine);
    }

    private void Pair_Click(object sender, EventArgs e)
    {
        Guid spguid = BluetoothService.SerialPort;
        try
        {
            BluetoothAddress deviceAddress = deviceAddressesDictionary["IT-300 BT Dev"];
            BluetoothEndPoint endPoint = new BluetoothEndPoint(deviceAddress, spguid);

            blueToothClient.SetPin(deviceAddress, "1234");
            blueToothClient.Connect(endPoint);

            MessageBox.Show("Pair successful.");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    魑魅魍魉
Page 1 of 1 (3 items)