PC上面的藍芽的通訊(C#)

來源:互聯網
上載者:User

標籤:

添加引用InTheHand.Net.Personal.dll

首先建立一個藍芽類

1 class LanYa {2 public string blueName { get; set; }                                   //l藍芽名字3 public BluetoothAddress blueAddress { get; set; }               //藍芽的唯一識別碼4 public ClassOfDevice blueClassOfDevice { get; set; }            //藍芽是何種類型5 public bool IsBlueAuth { get; set; }                                    //指定裝置通過驗證6 public bool IsBlueRemembered { get; set; }                        //記住裝置7 public DateTime blueLastSeen { get; set; }8 public DateTime blueLastUsed { get; set; }9 }

 

然後就是搜尋裝置

1 List<LanYa> lanYaList = new List<LanYa>();                //搜尋到的藍芽的集合2 BluetoothClient client = new BluetoothClient();            3 BluetoothRadio radio = BluetoothRadio.PrimaryRadio;  //擷取藍芽適配器4 radio.Mode = RadioMode.Connectable;                        5 BluetoothDeviceInfo[] devices = client.DiscoverDevices();//搜尋藍芽  10秒鐘6 foreach (var item in devices) {7 lanYaList.Add(new LanYa { blueName = item.DeviceName, blueAddress = item.DeviceAddress, blueClassOfDevice = item.ClassOfDevice, IsBlueAuth = item.Authenticated, IsBlueRemembered = item.Remembered, blueLastSeen = item.LastSeen, blueLastUsed = item.LastUsed });//把搜尋到的藍芽添加到集合中8 }

 

藍芽的配對

1 BluetoothClient blueclient = new BluetoothClient();2 Guid mGUID1 = BluetoothService.Handsfree;       //藍芽服務的uuid3 4  blueclient.Connect(s.blueAddress, mGUID)      //開始配對  藍芽4.0不需要setpin

 

用戶端

 1 BluetoothClient bl = new BluetoothClient();// 2 Guid mGUID2 = Guid.Parse("00001101-0000-1000-8000-00805F9B34FB");//藍芽串口服務的uuiid 3  4 try 5 { 6 bl.Connect(s.blue_address, mGUID); 7 //"串連成功"; 8 } 9 catch(Exception x)10 {11 //異常12 }13 14 var v = bl.GetStream();15 byte[] sendData = Encoding.Default.GetBytes(“人生苦短,我用python”);16 v.Write(sendData, 0, sendData.Length);              //發送

 

 

伺服器端

 1 bluetoothListener = new BluetoothListener(mGUID2); 2 bluetoothListener.Start();//開始監聽 3  4 bl = bluetoothListener.AcceptBluetoothClient();//接收 5  6  7 while (true) 8 { 9 byte[] buffer = new byte[100];10 Stream peerStream = bl.GetStream();11 12 peerStream.Read(buffer, 0, buffer.Length);13 14 string data= Encoding.UTF8.GetString(buffer).ToString().Replace("\0", "");//去掉後面的\0位元組15 }

 

基本上就是這些吧!

 

PC上面的藍芽的通訊(C#)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.