views:

432

answers:

4

I am trying to use Symbol.WPAN.Bluetooth that comes with the EMDK for Symbol devices.

Does anyone happen to have a working example that transfers data?

Symbol's example just pairs the devices. (They apparently think that transfering data is not really needed in a Personal Area network example.)

Anyway, I know this is a long shot, but if anyone has gotten this to work I would love to see some code.

This is what I have tried. I have one device press button1 and another device press button2. The read value is always a zero length byte array.

using System.Text;
using System.Windows.Forms;
using Symbol.WPAN;
using Symbol.WPAN.Bluetooth;

namespace SmartDeviceProject1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Bluetooth bluetooth = new Bluetooth();
            if (bluetooth.IsEnabled != true)
            {
                bluetooth.Enable();
                bluetooth.RadioMode = BTH_RADIO_MODE.BTH_DISCOVERABLE_AND_CONNECTABLE;
            }

            RemoteDevice connectedDevice = null;
            foreach (RemoteDevice remoteDevice in MakeEnumerable(bluetooth.RemoteDevices))
            {
                if ((remoteDevice.Name == "WM_Dan")  && (remoteDevice.IsPaired == false))
                {
                    remoteDevice.Pair();
                    connectedDevice = remoteDevice;
                }
            }

            string test;
            test = "Testing this out";
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] encTest = encoding.GetBytes(test);


            if (connectedDevice != null)
            {
                connectedDevice.WriteTimeout = 20000;
                connectedDevice.Write(encTest);
            }


        }

        public static IEnumerable<RemoteDevice> MakeEnumerable(RemoteDevices devices)
        {
            for (var i = 0; i < devices.Length; i++)
            {
                yield return devices[i];
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Bluetooth bluetooth = new Bluetooth();

            if (bluetooth.IsEnabled != true)
            {
                bluetooth.Enable();
                bluetooth.RadioMode = BTH_RADIO_MODE.BTH_DISCOVERABLE_AND_CONNECTABLE;
            }

            RemoteDevice connectedDevice = null;
            foreach (RemoteDevice remoteDevice in MakeEnumerable(bluetooth.RemoteDevices))
            {
                if ((remoteDevice.Name == "WM_Dan2") && (remoteDevice.IsPaired == false))
                {
                    remoteDevice.Pair();
                    connectedDevice = remoteDevice;
                }
            }

            string test;
            test = "Testing this out";
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] encTest = encoding.GetBytes(test);
            byte[] encTest2;
            string test2;

            if (connectedDevice != null)
            {
                connectedDevice.ReadTimeout = 20000;
                encTest2 = connectedDevice.Read(encTest.Length);
                test2 = encoding.GetString(encTest2, 0, encTest2.Length);
                MessageBox.Show(test2);
            }

        }

    }
}
+1  A: 

I gave up on using the built in com port connection and opened a SerialPort object on the connection.

SerialPort sp = new SerialPort();
sp.PortName = "COM" + connectedDevice.LocalComPort.ToString();
sp.BaudRate = 9600;
sp.DataBits = 8;
sp.Parity = Parity.None;
sp.StopBits = StopBits.One;

sp.Open();
sp.Open();
sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
sp.ErrorReceived += new SerialErrorReceivedEventHandler(sp_ErrorReceived);
sp.WriteLine(textBoxSend.Text);

I also found that even though their docs said that LocalComPort was auto assigned, this was not always the truth. It was best to use their BTExplorer to set it first.

As well, there OpenComPort would work in situations where is should not -- using Reflector it is pretty obviously wrong. There are checking the return of ::CreateFile("COM" + port...) against 0 instead of -1 (INVALID_HANDLE_VALUE)

Jack Bolding
Bless you for posting this. I had given up. I will try this code out and hopefully I can get some sort of communication to happen...
Vaccano
Just saw this was still hanging around. I will try to check it out soon and see if it works.
Vaccano