它将数据传送Symbol.WPAN.Bluetooth例子 [英] Symbol.WPAN.Bluetooth example that transfers data

查看:193
本文介绍了它将数据传送Symbol.WPAN.Bluetooth例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Symbol.WPAN.Bluetooth随附EMDK为标志的设备。

有谁恰好有一个工作的例子,传输数据?

符号的例子只是对设备。 (他们显然认为传输数据是不是真的需要在个人区域网络的例子。)

不管怎样,我知道这是一个长镜头,但如果有人已经得到了这个工作,我很想看到一些code。

这是我都试过了。我有一个设备preSS Button1的与其他设备preSS按钮2。读值始终是零长度的字节数组。

 使用System.Text;
使用System.Windows.Forms的;
使用Symbol.WPAN;
使用Symbol.WPAN.Bluetooth;命名空间SmartDeviceProject1
{
    公共部分Form1类:表格
    {
        公共Form1中()
        {
            的InitializeComponent();
        }        私人无效的button1_Click(对象发件人,EventArgs的发送)
        {
            蓝牙蓝牙=新的蓝牙();
            如果(bluetooth.IsEnabled!= TRUE)
            {
                bluetooth.Enable();
                bluetooth.RadioMode = BTH_RADIO_MODE.BTH_DISCOVERABLE_AND_CONNECTABLE;
            }            远端设备connectedDevice = NULL;
            的foreach(远端设备远端设备在MakeEnumerable(bluetooth.RemoteDevices))
            {
                如果((remoteDevice.Name ==WM_Dan)及及(remoteDevice.IsPaired ==假))
                {
                    remoteDevice.Pair();
                    connectedDevice =远端设备;
                }
            }            字符串的测试;
            测试=测试了这一点;
            ASCIIEncoding编码=新ASCIIEncoding();
            字节[] = encTest encoding.GetBytes(试验);
            如果(connectedDevice!= NULL)
            {
                connectedDevice.WriteTimeout = 20000;
                connectedDevice.Write(encTest);
            }
        }        公共静态的IEnumerable<远端设备> MakeEnumerable(RemoteDevices设备)
        {
            对于(VAR I = 0; I< devices.Length;我++)
            {
                产量返回设备[I]
            }
        }        私人无效button2_Click(对象发件人,EventArgs的发送)
        {
            蓝牙蓝牙=新的蓝牙();            如果(bluetooth.IsEnabled!= TRUE)
            {
                bluetooth.Enable();
                bluetooth.RadioMode = BTH_RADIO_MODE.BTH_DISCOVERABLE_AND_CONNECTABLE;
            }            远端设备connectedDevice = NULL;
            的foreach(远端设备远端设备在MakeEnumerable(bluetooth.RemoteDevices))
            {
                如果((remoteDevice.Name ==WM_Dan2)及及(remoteDevice.IsPaired ==假))
                {
                    remoteDevice.Pair();
                    connectedDevice =远端设备;
                }
            }            字符串的测试;
            测试=测试了这一点;
            ASCIIEncoding编码=新ASCIIEncoding();
            字节[] = encTest encoding.GetBytes(试验);
            字节[] encTest2;
            串TEST2;            如果(connectedDevice!= NULL)
            {
                connectedDevice.ReadTimeout = 20000;
                encTest2 = connectedDevice.Read(encTest.Length);
                TEST2 = encoding.GetString(encTest2,0,encTest2.Length);
                MessageBox.Show(测试2);
            }        }    }
}


解决方案

我放弃了使用内置的COM端口连接和连接上打开了一个的SerialPort对象。

 的SerialPort SP =新的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 + =新SerialDataReceivedEventHandler(sp_DataReceived);
sp.ErrorReceived + =新SerialErrorReceivedEventHandler(sp_ErrorReceived);
sp.WriteLine(textBoxSend.Text);

我还发现,尽管他们的文档说,LocalComPort被自动分配,这并不总是事实。这是最好用自己的BTExplorer先进行设定。

同时,还有OpenComPort会在情况下是不应该工作 - 使用反射是pretty显然是错误的。目前正在检查 ::的CreateFile(COM+端口......)的回归反对0而不是-1( INVALID_HANDLE_VALUE

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);
            }

        }

    }
}

解决方案

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)

这篇关于它将数据传送Symbol.WPAN.Bluetooth例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆