Bluetooth communication above the PC (C #)

Source: Internet
Author: User
Tags getstream

Add Reference InTheHand.Net.Personal.dll

First create a Bluetooth class

1 classLanya {2  Public stringBluename {Get;Set; }//L Bluetooth Name3  PublicBluetoothaddress blueaddress {Get;Set; }//Unique identifier for Bluetooth4  PublicClassofdevice Blueclassofdevice {Get;Set; }//What type of Bluetooth is it?5  Public BOOLIsblueauth {Get;Set; }//specify that the device passes validation6  Public BOOLisblueremembered {Get;Set; }//Remember device7  PublicDateTime Bluelastseen {Get;Set; }8  PublicDateTime bluelastused {Get;Set; }9}

Then it's the search device.

1List<lanya> lanyalist =NewList<lanya> ();//search for a collection of Bluetooth2Bluetoothclient client =Newbluetoothclient (); 3Bluetoothradio radio = Bluetoothradio.primaryradio;//get the Bluetooth adapter4Radio. Mode =radiomode.connectable; 5Bluetoothdeviceinfo[] devices = client. Discoverdevices ();//Search for Bluetooth for 10 seconds6 foreach(varIteminchdevices) {7Lanyalist.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 collection8}

Pairing of Bluetooth

1 New bluetoothclient (); 2 Guid mGUID1 = bluetoothservice.handsfree;       // UUID for Bluetooth service 3 4  Blueclient. Connect (s.blueaddress, Mguid)      // start pairing  Bluetooth 4.0 does not require Setpin

Client

1bluetoothclient BL =NewBluetoothclient ();//2Guid mGUID2 = Guid.parse ("00001101-0000-1000-8000-00805F9B34FB");//UUIID of Bluetooth serial port service3 4 Try5 {6 Bl. Connect (s.blue_address, mguid);7 //"Successful connection";8 }9 Catch(Exception x)Ten { One //Exception A } -  - varv =Bl. GetStream (); the byte[] SendData =Encoding.Default.GetBytes ("Life is short, I use Python"); -V.write (SendData,0, senddata.length);//Send

Server-side

1Bluetoothlistener =NewBluetoothlistener (mGUID2);2Bluetoothlistener.start ();//Start listening .3 4BL = Bluetoothlistener.acceptbluetoothclient ();//Receive5 6 7  while(true)8 {9 byte[] buffer =New byte[ -];TenStream Peerstream =Bl. GetStream (); One  APeerstream.read (Buffer,0, buffer. Length); -  - stringData= Encoding.UTF8.GetString (buffer). ToString (). Replace (" /","");//Remove the bytes from the back the}

This is basically it!

Bluetooth communication above the PC (C #)

Related Article

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.