PC Bluetooth Communication C # code implementation

Source: Internet
Author: User
Tags getstream

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.