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.