C# 在PocketPC上的通过蓝牙(bluetooth)发送数据

本文介绍了如何在C#环境下,利用PocketPC通过蓝牙发送数据到MOTO E680i。文章详细讲解了使用opennetcf组件库简化蓝牙设备搜索、连接及数据发送的过程,并提供了发送数据的示例代码,包括数据包格式的构造和错误处理。
摘要由CSDN通过智能技术生成

测试环境,使用dopod818写蓝牙的数据发送,而接收端直接使用MOTO E680i进行测试

原想使用eVC++开发,都是些API调用的方式,够麻烦,不想这样麻烦,无意发现opennetcf的组件库,哈哈,简单多了!
开始吧,用组件吧,毕竟VS2003也是用MS的,自己无需从底层写了,可以注重应用的开发!

搜索蓝牙设备,并记录蓝牙设备的MAC,然后通过连接,取得NetworkStream的实例对象,通过NetworkStream发送数据

BluetoothClient bc = new BluetoothClient();
BluetoothDeviceInfo[] bdi = bc.DiscoverDevices(20);

Guid spguid = BluetoothService.ObexObjectPush;
string BTMAC= bdi[0].DeviceID.ToString();
BluetoothAddress btaddress = BluetoothAddress.Parse(BTMAC);
BluetoothEndPoint endpoint = = new BluetoothEndPoint(btaddress, spguid);

BluetoothClient client = new BluetoothClient();client.Connect(endpoint);
NetworkStream stream = (NetworkStream)client.GetStream();


好了,然后可以通过stream发送数据了,主要是数据包的格式,网上有很多,需要的朋友在google中搜一下吧!
int result = BluetoothSend("PUT","测试.txt","来自ITBABY的818的数据...");

     switch (result)
     {
      case 160: // 0xa0
       MessageBox.Show("发送成功...");
       break;
     
      case 197: // 0xc5
       MessageBox.Show("方法丢失...");
       break;

      case 192: // 0xc0
       MessageBox.Show("Bad Request");
       break;

      default:
       MessageBox.Show("其它错误");
       break;
     }


private int BluetoothSend(string tReqType, string tName, string tFileContent)
  {
   int i;
   int offset;
   int packetsize;
   byte reqtype = 0x82;
   
   //如果是其它格式,此处的定义数据就可以使用
   //int tTypeLen = 0x03;
   //int typeheadsize;
   //int typesizeHi = 0x00;
   //int typesizeLo = 0x03;
   
   if (tReqType == "GET")reqtype = 0x83;   // 131 接受
   if (tReqType == "PUT")reqtype = 0x82;   // 130 发送

   packetsize = 3;

   //Name Header
   //由于MOTO E680I的Linux系统,在显示接受到蓝牙发送的数据显示的名称正常,用其它编码方式全部是乱码
   byte[] tNameU = System.Text.Encoding.BigEndianUnicode.GetBytes(tName);
   int tNameLength = tNameU.Length;
   int nameheadsize = (3 + (tNameLength*2) + 2);
   int namesizeHi = (nameheadsize & 0xff00)/0xff;
   int namesizeLo = nameheadsize & 0x00ff;
   packetsize = packetsize + nameheadsize;
   
 
   //Body
   //由于MOTO E680I的Linux系统,在测试时,选则GBK打开,可以正常显示
   byte[] bstr = System.Text.Encoding.GetEncoding("gb2312").GetBytes(tFileContent); 
   int fileLen = bstr.Length; 
   int fileheadsize = 3 + fileLen  ;
   int filesizeHi = (fileheadsize & 0xff00)/0xff;;
   int filesizeLo = fileheadsize & 0x00ff;;

   packetsize = packetsize + fileheadsize;

   int packetsizeHi = (packetsize & 0xff00)/0xff;
   int packetsizeLo = packetsize & 0x00ff;

   byte[] tSendByte = new byte[packetsize];

   //头信息
   tSendByte[0] = reqtype;          // 发送或接受的
   tSendByte[1] = Convert.ToByte(packetsizeHi);    // 包长度的 高字节
   tSendByte[2] = Convert.ToByte(packetsizeLo);    // 包长度的 低字节

   offset = 2;

   //头信息的文件名称
   tSendByte[offset+1] = 0x01;         // 文件名称开始标志 

 
   tSendByte[offset+2] = Convert.ToByte(namesizeHi);   // 文件名称高字节(2 bytes)
   tSendByte[offset+3] = Convert.ToByte(namesizeLo);   // 文件名称低字节(2 bytes)

   // Name+/n/n in unicode
   tNameU.CopyTo(tSendByte,offset+4);

   offset = offset + 3 + (tNameLength*2);
   tSendByte[offset+1] = 0x00;         // /n
   tSendByte[offset+2] = 0x00;         // /n

   offset = offset + 2;


   //Body
   tSendByte[offset+1] = 0x49;         //内容开始标志
   tSendByte[offset+2] = Convert.ToByte(filesizeHi);   //
   tSendByte[offset+3] = Convert.ToByte(filesizeLo);   //


   bstr.CopyTo(tSendByte,offset+4);
   

   offset = offset+3+fileLen;

   
   stream.Write(tSendByte,0,tSendByte.Length );
     

   //监听输出的标志数据,来判断是否发送成功
   //这里可以使用等待消息的回复,来判断
   
   bool x = stream.DataAvailable;

   byte[] tArray4 = new byte[3];
   stream.Read(tArray4,0,3);

   x = stream.DataAvailable;

   if (tArray4[0] == 160) // 0xa0
   {
    int plength = (tArray4[1] * 256) + tArray4[2] -3;
    byte[] tArray5 = new byte[plength];
    if (plength >0)
    {
     stream.Read(tArray5,0,plength);
    }
    return 160;
   }

   if (tArray4[0] == 197) // 0xc5 不支持的方法
   {
    return 197;
   }

   if (tArray4[0] == 192) // 0xc0 错误请求
   {
    return 192;
   }
   
   return 0;
  }

用完了,别忘了关闭stream
以上所有操作蓝牙的类库说明等,请到www.opennetcf.org搜索查看,或下载

原码下载:(蓝牙操作的类库已经包含在bin/debug目录中)
http://itbaby.jss.cn/download/BTC.rar

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 17
    评论
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值