There is a lot of information on the Internet and it is very easy to use when receiving Bluetooth, but I think it is necessary to summarize this knowledge. Bluetooth development requires a third-party library, InTheHand. net. personal. dll. The two key classes are javasthclient and javasthlistener. First, enable a subthread to continuously receive data. It is easy to use and directly run the Code:
Copy codeThe Code is as follows: using InTheHand. Net. Sockets;
Using System. Threading;
Public MainWindow ()
{
InitializeComponent ();
ListenThread = new Thread (ReceiveData );
ListenThread. Start ();
}
Private void ReceiveData ()
{
Try
{
Guid mGUID = Guid. Parse ("20171101-0000-1000-8000-00805f9b34fb ");
Descrithlistener = new descrithlistener (mGUID );
Listen thlistener. Start ();
Descrithclient = descrithlistener. acceptblustmthclient ();
IsConnected = true;
}
Catch (Exception)
{
IsConnected = false;
}
While (isConnected)
{
String receive = string. Empty;
If (then thclient = null)
{
Break;
}
Try
{
PeerStream = descrithclient. GetStream ();
Byte [] buffer = new byte [6];
PeerStream. Read (buffer, 0, 6 );
Receive = Encoding. UTF8.GetString (buffer). ToString ();
}
Catch (System. Exception)
{
}
Thread. Sleep (100 );
}
}
BluetoothClient extends thclient;
Descrithlistener extends thlistener;
Thread listenThread;
Bool isConnected;
Note: after finding that two mobile phones are successfully paired with the computer, the two mobile phones are connected to the PC software at the same time. If data is sent together, no one on the PC will be able to answer this question.