Bluetooth communication on the PC (C #),
Add reference InTheHand. Net. Personal. dll
First create a Bluetooth class
1 class LanYa {2 public string blueName {get; set;} // l Bluetooth name 3 public incluthaddress blueAddress {get; set ;} // The unique identifier of Bluetooth 4 public ClassOfDevice blueClassOfDevice {get; set;} // The type of Bluetooth 5 public bool IsBlueAuth {get; set ;} // verify that the specified device has passed 6 public bool IsBlueRemembered {get; set;} // remember that the device 7 public DateTime blueLastSeen {get; set;} 8 public DateTime bluelostused {get; set ;} 9}
Then, search for devices.
1 List <LanYa> lanYaList = new List <LanYa> (); // a set of searched Bluetooth 2 descrithclient client = new descrithclient (); 3 descrithradio radio = descrithradio. primaryRadio; // obtain the Bluetooth adapter 4 radio. mode = RadioMode. connectable; 5 define thdeviceinfo [] devices = client. discoverDevices (); // search for Bluetooth for 10 seconds 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, bluelostused = item. lastUsed}); // Add the searched Bluetooth to the Collection 8}
Bluetooth pairing
1 required thclient blueclient = new required thclient (); 2 Guid mGUID1 = required thservice. handsfree; // The uuid3 4 blueclient of the Bluetooth service. connect (s. blueAddress, mGUID) // start pairing Bluetooth 4.0 without setpin
Client
1 required thclient bl = new required thclient (); // 2 Guid mGUID2 = Guid. parse ("20171101-0000-1000-8000-00805f9b34fb"); // uuiid of the Bluetooth serial port service 3 4 try 5 {6 bl. connect (s. blue_address, mGUID); 7 // "connection successful"; 8} 9 catch (Exception x) 10 {11 // Exception 12} 13 14 var v = bl. getStream (); 15 byte [] sendData = Encoding. default. getBytes ("My life is short, I use python"); 16 v. write (sendData, 0, sendData. length); // send
Server
1 Listen thlistener = new listen thlistener (mGUID2); 2 listen thlistener. start (); // Start listening for 3 4 bl = listen thlistener. acceptblustmthclient (); // receives 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", ""); // remove the \ 0 byte 15}
Basically, that's all!