91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C# 在PC上的通過藍牙(Bluetooth)發送數據到手機的方法

發布時間:2021-06-16 10:01:17 來源:億速云 閱讀:284 作者:chen 欄目:編程語言

本篇內容主要講解“C# 在PC上的通過藍牙(Bluetooth)發送數據到手機的方法”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“C# 在PC上的通過藍牙(Bluetooth)發送數據到手機的方法”吧!

概述

在PC端用.NET開發一個藍牙下載的程序。實現在PC上查找周圍的藍牙設備(主要是手機),并將PC上的文件通過藍牙傳輸到手機上。目前我采用的是OpenNETCF.Net.Bluetooth,能夠正常發現周圍的藍牙設備,手機也能夠正常進行藍牙連接并接收文件。

代碼實現

1、查找周圍藍牙設備

BluetoothClient bc = new BluetoothClient();         BluetoothDeviceInfo[] bdi;         bdi = bc.DiscoverDevices(10);//查找周圍最多10個藍牙設備         if (bdi.Length == 0)         {             this.ShowErrMsg("請確認您的藍牙已開啟!");             return;         }

2、打開數據流

#region OBEXOpenStream     public bool OBEXOpenStream(string BTMAC)     {          // serial port UUID         Guid spguid = OpenNETCF.Net.Bluetooth.BluetoothService.ObexObjectPush;         OpenNETCF.Net.BluetoothAddress btaddress = OpenNETCF.Net.BluetoothAddress.Parse(BTMAC);         client = new OpenNETCF.Net.Sockets.BluetoothClient();          // define endpoint         OpenNETCF.Net.BluetoothEndPoint endpoint = new OpenNETCF.Net.BluetoothEndPoint(btaddress, spguid);          try         {             //open socket             client.Connect(endpoint);         }         catch         {             //unable to connect (server not listening on spguid)             return false;         }          //connect socket         stream = client.GetStream();         return true;     }     #endregion

3、配對連接

private bool OBEXConnect()     {         //send client request         byte[] ConnectPacket = new byte[7];          ConnectPacket[0] = 0x80;      // Connect         ConnectPacket[1] = 0x00;      // Packetlength Hi Byte         ConnectPacket[2] = 0x07;      // Packetlength Lo Byte         ConnectPacket[3] = 0x10;      // Obex v1         ConnectPacket[4] = 0x00;      // no flags         ConnectPacket[5] = 0x20;      // 8k max packet size Hi Byte         ConnectPacket[6] = 0x00;      // 8k max packet size Lo Byte          stream.Write(ConnectPacket, 0, ConnectPacket.Length);          //listen for server response         byte[] ReceiveBufferA = new byte[3];         stream.Read(ReceiveBufferA, 0, 3);          if (ReceiveBufferA[0] == 160) // 0xa0         {              //success, decode rest of packet             int plength = (0xff * ReceiveBufferA[1]) + ReceiveBufferA[2]; //length of packet is...              //listen for rest of packet             byte[] ReceiveBufferB = new byte[plength - 3];             stream.Read(ReceiveBufferB, 0, plength - 3);              int obver = ReceiveBufferB[0]; //server obex version (16 = v1.0)             int cflags = ReceiveBufferB[1]; //connect flags             int maxpack = (0xff * ReceiveBufferB[2]) + ReceiveBufferB[3]; //max packet size              return true;         }         else         {             return false;         }     }

4、發送文本內容

//send client request, start put                         string tName = "BRT優惠卷.txt";                         string tType = "";                         string tFileContent = "優惠卷內容:" + this.txtContent.Text.Trim();                          int result = OBEXRequest("PUT", tName, tType, tFileContent);                         if (result == 160)                         {                             SendSuccess = true;                         }

完整代碼

using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Net.Sockets; using OpenNETCF; using OpenNETCF.Net; using OpenNETCF.Net.Bluetooth; using OpenNETCF.Net.Sockets;  public partial class _Default : System.Web.UI.Page  {     //Stream stream;     NetworkStream stream;     OpenNETCF.Net.Sockets.BluetoothClient client;     protected void Page_Load(object sender, EventArgs e) {      }      #region ShowErrMsg     private void ShowErrMsg(string err) {         string str;         str = "<script language='javascript'>";         str += " alert('" + err + "')";         str += "</script>";         this.RegisterStartupScript("", str);     }     #endregion     protected void btnSend_Click(object sender, EventArgs e) {         if (this.txtContent.Text.Trim() == string.Empty)         {             this.ShowErrMsg("請輸入發送內容!");             return;         }         BluetoothClient bc = new BluetoothClient();         BluetoothDeviceInfo[] bdi;         bdi = bc.DiscoverDevices(10);//查找周圍最多10個藍牙設備         if (bdi.Length == 0)         {             this.ShowErrMsg("請確認您的藍牙已開啟!");             return;         }         bool SendSuccess = false;         try         {             foreach (BluetoothDeviceInfo bd in bdi)             {                 if (OBEXOpenStream(bd.DeviceID.ToString()))                 {                      if (OBEXConnect())                     {                         //send client request, start put                         string tName = "BRT優惠卷.txt";                         string tType = "";                         string tFileContent = "優惠卷內容:" + this.txtContent.Text.Trim();                          int result = OBEXRequest("PUT", tName, tType, tFileContent);                         if (result == 160)                         {                             SendSuccess = true;                         }                     }                     OBEXCloseStream();                 }                 else                 {                     this.ShowErrMsg("連接失敗!" + bd.DeviceName.ToString());                 }              }             if (SendSuccess)             {                 this.ShowErrMsg("發送成功,謝謝使用!");             }         }         finally         {             OBEXCloseStream();         }     }       #region OBEXOpenStream     public bool OBEXOpenStream(string BTMAC) {          // serial port UUID         Guid spguid = OpenNETCF.Net.Bluetooth.BluetoothService.ObexObjectPush;         OpenNETCF.Net.BluetoothAddress btaddress = OpenNETCF.Net.BluetoothAddress.Parse(BTMAC);         client = new OpenNETCF.Net.Sockets.BluetoothClient();          // define endpoint         OpenNETCF.Net.BluetoothEndPoint endpoint = new OpenNETCF.Net.BluetoothEndPoint(btaddress, spguid);          try         {             //open socket             client.Connect(endpoint);         }         catch         {             //unable to connect (server not listening on spguid)             return false;         }          //connect socket         stream = client.GetStream();         return true;     }     #endregion      #region OBEXCloseStream     private void OBEXCloseStream() {         if (stream != null)         {             stream.Close();         }         if (client != null)         {             client.Close();         }     }     #endregion      #region  OBEXConnect     private bool OBEXConnect() {         //send client request         byte[] ConnectPacket = new byte[7];          ConnectPacket[0] = 0x80;      // Connect         ConnectPacket[1] = 0x00;      // Packetlength Hi Byte         ConnectPacket[2] = 0x07;      // Packetlength Lo Byte         ConnectPacket[3] = 0x10;      // Obex v1         ConnectPacket[4] = 0x00;      // no flags         ConnectPacket[5] = 0x20;      // 8k max packet size Hi Byte         ConnectPacket[6] = 0x00;      // 8k max packet size Lo Byte          stream.Write(ConnectPacket, 0, ConnectPacket.Length);          //listen for server response         byte[] ReceiveBufferA = new byte[3];         stream.Read(ReceiveBufferA, 0, 3);          if (ReceiveBufferA[0] == 160) // 0xa0         {              //success, decode rest of packet             int plength = (0xff * ReceiveBufferA[1]) + ReceiveBufferA[2]; //length of packet is...              //listen for rest of packet             byte[] ReceiveBufferB = new byte[plength - 3];             stream.Read(ReceiveBufferB, 0, plength - 3);              int obver = ReceiveBufferB[0]; //server obex version (16 = v1.0)             int cflags = ReceiveBufferB[1]; //connect flags             int maxpack = (0xff * ReceiveBufferB[2]) + ReceiveBufferB[3]; //max packet size              return true;         }         else         {             return false;         }     }     #endregion      #region OBEXRequest     private int OBEXRequest(string tReqType, string tName, string tType, string tFileContent) {         //send client request          int i;         int offset;         int packetsize;         byte reqtype = 0x82;          int tTypeLen = 0x03;         int typeheadsize;         int typesizeHi = 0x00;         int typesizeLo = 0x03;          //tName = "contact.vcf";         //tType = "text/x-vCard";         //tFileContent = "BEGIN:VCARD\r\nVERSION:2.1\r\nN:;aardvark\r\nFN:aardvark\r\nEND:VCARD\r\n";          if (tReqType == "GET")         {             reqtype = 0x83;      // 131 GET-Final         }          if (tReqType == "PUT")         {             reqtype = 0x82;      // 130 PUT-Final         }          packetsize = 3;          //Name Header         int tNameLength = tName.Length;         int nameheadsize = (3 + (tNameLength * 2) + 2);         int namesizeHi = (nameheadsize & 0xff00) / 0xff;         int namesizeLo = nameheadsize & 0x00ff;         packetsize = packetsize + nameheadsize;          if (tType != "")         {             //Type Header             tTypeLen = tType.Length;             typeheadsize = 3 + tTypeLen + 1;             typesizeHi = (typeheadsize & 0xff00) / 0xff;             typesizeLo = typeheadsize & 0x00ff;             packetsize = packetsize + typeheadsize;         }          //Body         int fileLen = System.Text.Encoding.Default.GetBytes(tFileContent).Length;//tFileContent.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];          //PUT-final Header         tSendByte[0] = reqtype;                    // Request type e.g. PUT-final 130         tSendByte[1] = Convert.ToByte(packetsizeHi);        // Packetlength Hi         tSendByte[2] = Convert.ToByte(packetsizeLo);        // Packetlength Lo          offset = 2;          //Name Header         tSendByte[offset + 1] = 0x01;                  // HI for Name header             tSendByte[offset + 2] = Convert.ToByte(namesizeHi);      // Length of Name header (2 bytes per char)         tSendByte[offset + 3] = Convert.ToByte(namesizeLo);      // Length of Name header (2 bytes per char)          // Name+\n\n in unicode         byte[] tNameU = System.Text.Encoding.BigEndianUnicode.GetBytes(tName);         tNameU.CopyTo(tSendByte, offset + 4);          offset = offset + 3 + (tNameLength * 2);         tSendByte[offset + 1] = 0x00;                  // null term         tSendByte[offset + 2] = 0x00;                  // null term          offset = offset + 2;          if (tType != "")         {             //Type Header             tSendByte[offset + 1] = 0x42;                  // HI for Type Header 66             tSendByte[offset + 2] = Convert.ToByte(typesizeHi);      // Length of Type Header             tSendByte[offset + 3] = Convert.ToByte(typesizeLo);      // Length of Type Header              for (i = 0; i <= (tTypeLen - 1); i++)             {                 tSendByte[offset + 4 + i] = Convert.ToByte(Convert.ToChar(tType.Substring(i, 1)));             }             tSendByte[offset + 3 + tTypeLen + 1] = 0x00;            // null terminator              offset = offset + 3 + tTypeLen + 1;         }          //Body         tSendByte[offset + 1] = 0x49;                  //HI End of Body 73         tSendByte[offset + 2] = Convert.ToByte(filesizeHi);      //         tSendByte[offset + 3] = Convert.ToByte(filesizeLo);      //1k payload + 3 for HI header          //for (i = 0; i <= (fileLen - 1); i++)         //{         //    tSendByte[offset + 4 + i] = Convert.ToByte(tFileContent.Substring(i, 1));//Convert.ToByte(Convert.ToChar(tFileContent.Substring(i, 1)));         //}         byte[] bfile = System.Text.Encoding.Default.GetBytes(tFileContent);         for (i = 0; i <= bfile.Length - 1; i++)         {             tSendByte[offset + 4 + i] = bfile[i];         }          //tSendByte[offset+4+fileLen] = 0x00;              // null terminator          offset = offset + 3 + fileLen;          stream.Write(tSendByte, 0, tSendByte.Length);          //listen for server response          //TODO: can hang here forever waiting response...          bool x = stream.DataAvailable; // changed bluetoothclient - public NetworkStream GetStream()          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);                 //TODO: data in returned packet to deal with             }             return 160;         }          if (tArray4[0] == 197) // 0xc5 Method not allowed         {             return 197;         }          if (tArray4[0] == 192) // 0xc0 Bad Request         {             return 192;         }          return 0;     }      #endregion }

到此,相信大家對“C# 在PC上的通過藍牙(Bluetooth)發送數據到手機的方法”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

石台县| 大埔区| 湘阴县| 遵义市| 柞水县| 乃东县| 龙井市| 庆云县| 泊头市| 鹿邑县| 武功县| 黑龙江省| 铁岭市| 商洛市| 宜丰县| 德格县| 准格尔旗| 枝江市| 长寿区| 清水县| 淮南市| 泰宁县| 朝阳县| 睢宁县| 南靖县| 海门市| 漠河县| 丰台区| 德昌县| 禹州市| 大田县| 墨江| 长沙县| 临朐县| 哈尔滨市| 娄底市| 瑞丽市| 南康市| 博客| 漳平市| 凤山县|