嵌入式技术应用实践——蓝牙
111041123 孙宏强
一:实习目的
1、了解嵌入式系统和嵌入式技术;
2.、掌握C#语言编程;
3、学会Windows CE操作系统的新建、安装、部署等;
4、学会用C#语言实现蓝牙基本传输功能;
5、掌握蓝牙通信编码技术
二:实习内容
1、用C#语言实现comboBox下拉列表显示功能和清除功能;
2、实现listbox数据显示、添加和删除等功能;
3、编写蓝牙传输时的设备搜索以及文件发送功能;
4、生成安装包;
5、下载到实验箱上,进行现场实验让老师验收。
三:实习主程序
总体的蓝牙功能介绍:
蓝牙操作具有搜索蓝牙设备、浏览发送文件、发送文件、清空蓝牙列表、状态栏、显示时间日期、清空历史记录、的作用。
(1)搜索蓝牙设备
using System;
using System.Collections.Generic;
using https://www.wendangku.net/doc/a015925846.html,ponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using https://www.wendangku.net/doc/a015925846.html,; //必要的引用
using https://www.wendangku.net/doc/a015925846.html,.Bluetooth;//必要的引用
using https://www.wendangku.net/doc/a015925846.html,.Ports;//必要的引用
using https://www.wendangku.net/doc/a015925846.html,.Sockets;//必要的引用
namespace lanya
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonscan_Click(object sender, EventArgs e)
{
BluetoothRadio radio = BluetoothRadio.PrimaryRadio;
if (radio == null)
{
MessageBox.Show("没有蓝牙设备或没有支持的蓝牙协议");
return;
}
radio.Mode = RadioMode.Discoverable;
BluetoothClient client = new BluetoothClient();
statusBar1.Text = "正在搜索中";
Application.DoEvents();
BluetoothDeviceInfo[] bthdevice = client.DiscoverDevices();//通过
client.DiscoverDevices()寻找附近的bluetooth设备
cbdevices.DataSource = bthdevice;
cbdevices.DisplayMember = "DeviceName";
cbdevices.ValueMember = "DeviceAddress";//搜索完成
statusBar1.Text = "搜索完成";}
(2)浏览与发送
private void buttonskip_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK) { textBoxfilename.Text = ofd.FileName; } }
private void Form1_Load(object sender, EventArgs e)}
private void button3_Click(object sender, EventArgs e)
System.Uri uri = new Uri("obex://" + cbdevices.SelectedValue.ToString() + "/" + //获取服务器obex的uri,获取要传送的文件名
System.IO.Path.GetFileName(textBoxfilename.Text.Trim()));
ObexWebResponse response = null; //新建response变量
ObexWebRequest request = new ObexWebRequest(uri); //通过ObexWebRequest 来推文件到目标机器,发送请求,等待回应
try
{
request.ReadFile(textBoxfilename.Text.Trim()); //读出textboxfilename的文件内容 response = (ObexWebResponse)request.GetResponse();//回应封装在ObexWebResponse 类里面。如果目标机器的Obex服务没有打开,会发生文件传输错误。
MessageBox.Show(response.StatusCode.ToString());
}
catch
{
MessageBox.Show("传输失败");
}
finally
{
if (response != null)
{
response.Close();
}
}
其中:
1、 System.Uri uri = new Uri("obex://" +
cbDevices.SelectedValue.ToString() + "/" +
System.IO.Path.GetFileName(tbad.Text.Trim()) + https://www.wendangku.net/doc/a015925846.html,.Dns.GetHostName());其中new Uri("obex://" + cbDevices.SelectedValue.ToString()
代表的是要发送的蓝牙的地址,
System.IO.Path.GetFileName(tbad.Text.Trim())这一句代表的是要发送的文件;https://www.wendangku.net/doc/a015925846.html,.Dns.GetHostName()代表的是本机的地址。
2、Int countre ;
For (counter
=0;counter<=100;counter++)progressBar1.Value=Counter; 这一句代表的是在蓝牙发送过程中加上一个进度指示标志从更加直观的显示发送文件的过程。
(3)蓝牙发送与接收
using System;
using System.Collections.Generic;
using https://www.wendangku.net/doc/a015925846.html,ponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using https://www.wendangku.net/doc/a015925846.html,;
using https://www.wendangku.net/doc/a015925846.html,.Bluetooth;
using https://www.wendangku.net/doc/a015925846.html,.Sockets;
using System.Threading;
namespace _910414zcy
{
public partial class Form1 : Form
{
private Thread threadfile;
private ObexListener listener;
private bool listening;
public Form1()
{
InitializeComponent();
}
private delegate void SafeWinFormsThreadDelegate(string msg); //监听线程
private void WriteMsg(string msg)
{
SafeWinFormsThreadDelegate d;
d = new SafeWinFormsThreadDelegate(UpdateUI);
Invoke(d, new object[] { msg });
}
public void DealWithRequest() //处理请求
{
while(this.listener.IsListening)
try
{
ObexListenerContext olc = listener.GetContext();
ObexListenerRequest olr = olc.Request;
string filename = olr.RawUrl.TrimStart(new char[] { '/' });
olr.WriteFile(DateTime.Now.ToString("2012-12-12") + filename);
}
catch
{ continue; }
}
private void UpdateUI(string msg)
{
listBox1.Items.Add(msg);
}
private void button1_Click(object sender, EventArgs e) //开始按钮程序{
if (listener == null)
{
listener = new ObexListener();
BluetoothRadio.PrimaryRadio.Mode = RadioMode.Discoverable;
}
listener.Start();
listening = true;
threadfile = new Thread(DealWithRequest);
threadfile.Start();
button1.Enabled = false;
button2.Enabled = true;
UpdateUI("监听开始!");
}
private void button2_Click(object sender, EventArgs e) //停止按钮程序
{
if (listener != null)
{
listener.Stop();
}
if (threadfile != null)
{
threadfile.Abort();
}
button1.Enabled = true;
button2.Enabled = false;
点按“开始”按钮,文本框显示“监听开始”
(4)部署应用程序
当在桌面计算机上完成了Windows CE应用程序开发和测试工作之后,就需要将其部署到设备中去运行。具体步骤如下:
①部署.NET Compact Framework2.0;
②制作CAB安装包;
③创建CAB安装包项目;
④编辑器,当CAB安装包项目创建好了之后,https://www.wendangku.net/doc/a015925846.html,2005就会自动打开文件
系统编辑器;
⑤添加可执行文件;
⑥创建快捷方式;
⑦生成安装包和分发安装包
四、实习心得:
为期一周的时间很快过去了,时间虽短,却是让我受益匪浅。这次的基于WINCE平台的蓝牙嵌入式系统的应用实践实习
让我在短短的时间内了解了嵌入式系统和嵌入式技术,学会了用C语言编写程序来达到实现蓝牙基本传输功能。同时对项目的安装和部署也有了初步的认识。同时也明白了一个学生要想真正掌握一门课程、一个知识点,仅仅通过课本上的理论是不可能实现的,只有将理论与实践融合起来,将理论付诸于实践,将实践回归于理论,才是掌握并且运用一个知识点最好的方式。在实习中与队友的合作是必不可少的,要有团队合作精神,善于听取不同的建议,分享我们的想法,共同去做好这次的实习。同时也谢谢两位实习老师,在我们的设计过程中所遇到的问题一次次不厌烦的给与指导和帮助。这一次的实习是我在整个大学中平凡却又特别的一个经历。
嵌入式实验报告
姓名:孙宏强
学号:111041123
学院班级:电信学院111041A 专业:通信工程