PC Bluetooth Communication C # code Implementation _c# Tutorial

Source: Internet
Author: User
Tags getstream

This example for everyone to share the C # implementation of PC Bluetooth communication code, for your reference, the specific content as follows

Add Reference InTheHand.Net.Personal.dll

First create a Bluetooth class

Class Lanya {public
string Bluename {get; set;}                  l Bluetooth name public
bluetoothaddress blueaddress {get; set;}        Unique identifier for Bluetooth public
classofdevice Blueclassofdevice {get; set;}      What type of Bluetooth is public
bool Isblueauth {get; set;}                  Specifies that the device validates public
bool Isblueremembered {get; set;}            Remember that the device public
DateTime bluelastseen {get; set;}
Public DateTime bluelastused {get; set;}
}

Then there's the search device.

list<lanya> lanyalist = new list<lanya> ();        The collection of Bluetooth search
bluetoothclient client = new Bluetoothclient ();      
Bluetoothradio radio = Bluetoothradio.primaryradio; Gets the Bluetooth adapter
radio. Mode = radiomode.connectable;            
Bluetoothdeviceinfo[] devices = client. Discoverdevices ()//Search Bluetooth 10 seconds
foreach (var item in devices) {
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});//Add the Bluetooth search to the collection
}
 

Pairing of Bluetooth

Bluetoothclient blueclient = new Bluetoothclient ();
Guid mGUID1 = bluetoothservice.handsfree;    The UUID blueclient for Bluetooth service
 
. Connect (s.blueaddress, Mguid)   //Start pairing Bluetooth 4.0 does not require Setpin

Client

bluetoothclient bl = new bluetoothclient ();//
Guid mGUID2 = Guid.parse ("00001101-0000-1000-8000-00805F9B34FB"); Bluetooth serial service Uuiid

try
{
bl. Connect (s.blue_address, mguid);
"Connection succeeded";
}
catch (Exception x)
{
//exception
}

var v = bl. GetStream ();
byte[] SendData = Encoding.Default.GetBytes ("Life is short, I use Python");
V.write (senddata, 0, senddata.length);       Send

Server-side

Bluetoothlistener = new Bluetoothlistener (mGUID2);
Bluetoothlistener.start ()//start listening

bl = bluetoothlistener.acceptbluetoothclient ()/Receive while


(true)
{
byte[] buffer = new BYTE[100];
Stream Peerstream = bl. GetStream ();

Peerstream.read (buffer, 0, buffer. Length);

String data= Encoding.UTF8.GetString (buffer). ToString (). Replace ("n", "");//Remove the following bytes
}

That's basically it!

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.