PC Bluetooth Communication C # code implementation This article is mainly for the introduction of the PC Bluetooth communication C # code implementation, with a certain reference value, interested in small partners can refer to this example for everyone to share the C # implementation of the PC Bluetooth communication code, for everyone to reference, The specific content is as follows Add Reference InTheHand.Net.Personal.dll first create a Bluetooth classclassLanya { Public stringBluename {Get;Set; }//L Bluetooth Name PublicBluetoothaddress blueaddress {Get;Set; }//Unique identifier for Bluetooth PublicClassofdevice Blueclassofdevice {Get;Set; }//What type of Bluetooth is it? Public BOOLIsblueauth {Get;Set; }//specify that the device passes validation Public BOOLisblueremembered {Get;Set; }//Remember device PublicDateTime Bluelastseen {Get;Set; } PublicDateTime bluelastused {Get;Set; }} Then there's the search device list .<LanYa> lanyalist =NewList<lanya> ();//search for a collection of BluetoothBluetoothclient client =Newbluetoothclient (); Bluetoothradio Radio= Bluetoothradio.primaryradio;//get the Bluetooth adapterRadio. Mode =radiomode.connectable; Bluetoothdeviceinfo[] Devices= client. Discoverdevices ();//Search for Bluetooth for 10 secondsforeach(varIteminchdevices) {Lanyalist.add (NewLanya {bluename = Item. DeviceName, blueaddress = Item. DeviceAddress, Blueclassofdevice = Item. Classofdevice, Isblueauth = Item. Authenticated, isblueremembered = Item. Remembered, Bluelastseen = Item. Lastseen, bluelastused = Item. lastused});//Add the search Bluetooth to the collection} Bluetooth pairing bluetoothclient blueclient=Newbluetoothclient (); Guid mGUID1= Bluetoothservice.handsfree;//UUID for Bluetooth serviceblueclient. Connect (s.blueaddress, Mguid)//Start pairing Bluetooth 4.0 no Setpin requiredClient bluetoothclient BL=NewBluetoothclient ();//Guid mGUID2 = Guid.parse ("00001101-0000-1000-8000-00805F9B34FB");//UUIID of Bluetooth serial port serviceTry{bl. Connect (s.blue_address, mguid);//"Successful connection";}Catch(Exception x) {//Exception}varv =Bl. GetStream ();byte[] SendData =Encoding.Default.GetBytes ("Life is short, I use Python"); V.write (SendData,0, senddata.length);//Sendserver-side Bluetoothlistener=NewBluetoothlistener (mGUID2); Bluetoothlistener.start ();//Start listening .BL= Bluetoothlistener.acceptbluetoothclient ();//Receive while(true){byte[] buffer =New byte[ -]; Stream Peerstream=Bl. GetStream ();p eerstream.read (buffer,0, buffer. Length);stringData= Encoding.UTF8.GetString (buffer). ToString (). Replace (" /","");//Remove the bytes from the back} that's basically it!
PC Bluetooth Communication C # code implementation