In the past two days, the communication between PC and Android mobile phones is limited to the level. There are two methods available: socket based on data packets and Bluetooth. Although it looks simple, it has been adjusted for more than two days. I tested the socket. The latency in the indoor WIFI environment is about 0.1 s. The latency in 3G networks is as high as 3 s, and the port seems to be disconnected as long as no data is sent. In short, it is quite uncomfortable. Therefore, we considered the Bluetooth method.
One of the most commonly used Bluetooth virtual serial ports is to implement Bluetooth communication between mobile phones and PCs. This method can be easily implemented through configuration, which is used by many external Bluetooth GPS devices. However, the well-known Android system does not support most external GPS systems. (most Android mobile phones may contain built-in GPS devices and the external GPS system is too weak ). Therefore, the second type, Bluetooth socket, must be used.
On the computer, I really don't want to develop it in C ++, so I look for it. NET component, but in fact Microsoft's NET Library does not support Bluetooth, so you must use a third-party control, called inthehand.
This article details inthehan
D component, http://www.cnblogs.com/procoder/archive/2009/09/22/1571580.html. Since it discusses the idea of implementing file transmission, we will only discuss simple character transmission in this article.
Its official website is inthehand.net, and most of the class libraries and methods can be found.
The following is the mobile phone initialization code. For more information, see http://android.tgbus.com/android/tutorial/201103/410657.shtml.
Private PrintStream mPrintStream = null;
Private BufferedReader mBufferedReader = null;
Export thadapter mydomainthadapter = null;
Descrithserversocket mBThServer = null;
Descrithsocket mBTHSocket = null;
Mydomainthadapter = descrithadapter. getdefaadapter adapter ();
Mydomainthadapter. enable (); // open bth
Intent discoverableIntent = new Intent (
Descrithadapter. ACTION_REQUEST_DISCOVERABLE); // enables Bluetooth to be in discoverable mode, lasting for 150 s
DiscoverableIntent. putExtra (
Descrithadapter. EXTRA_DISCOVERABLE_DURATION, 150 );
The following is the core code for initialization on the PC: The PC appears as a client. It needs to search for the mobile phone's bluetooth MAC address for communication. GUID, also known as UUID, is a way to mark hardware addresses.
/// <Summary>
/// Open the port
/// </Summary>
/// <Param name = "Name"> port name </param>
/// <Returns> Successful </returns>
Public bool OpenPort (string Name)
{
InTheHand. Net. Bluetooth. Fig. PrimaryRadio. Mode = InTheHand. Net. Bluetooth. RadioMode. Connectable;
InTheHand. Net. Sockets. javasthclient cli = new InTheHand. Net. Sockets. javasthclient ();
InTheHand. Net. Sockets. effecthdeviceinfo [] devices = cli. DiscoverDevices ();
Foreach (InTheHand. Net. Sockets. descrithdeviceinfo device in devices) // device search
{
Device. Update ();
Device. Refresh ();
MessageBox. Show ("device found ");
Break;
}
Descrithdeviceinfo bd = new descrithdeviceinfo (devices [0]. DeviceAddress );
Effecthclient = new effecthclient ();
Guid mGUID = Guid. Parse ("fa87c0d0-afac-11de-8a39-0800200c9a66 ");
Descrithclient. Connect (devices [0]. DeviceAddress, mGUID); // Connect the client to the address. This is a blocking thread and requires a response from the server.
Deleethread = new Thread (ReceiveMethod );
Deleethread. Start ();
Return true;
}
The following describes how a mobile phone can receive a connection request from a PC:
View Code
1 if (connected)
2 {
3 return;
4}
5 try
6 {
7 mBThServer = mydomainthadapter
8. listenUsingRfcommWithServiceRecord (NAME_SECURE,
9 MY_UUID_SECURE );
10} catch (IOException e)
11 {
12 // TODO Auto-generated catch block
13 e. printStackTrace ();
14}
15
16 try
17 {
18 mBTHSocket = mBThServer. accept ();
19} catch (IOException e)
20 {
21 // TODO Auto-generated catch block
22 e. printStackTrace ();
23}
24 try
25 {
26 mBufferedReader = new BufferedReader (new InputStreamReader (
27 mBTHSocket. getInputStream ()));
28} catch (IOException e1)
29 {
30 // TODO Auto-generated catch block
31 e1.printStackTrace ();
32} // get the input and output streams
33 try
34 {
35 mPrintStream = new PrintStream (
36 mBTHSocket. getOutputStream (), true );
37 connected = true;
38} catch (IOException e)
39 {
40 // TODO Auto-generated catch block
41 e. printStackTrace ();
42}
To send data via mobile phone, run the following code:
MPrintStream. write (buff );
} Catch (IOException e)
{
// TODO Auto-generated catch block
E. printStackTrace ();
} // Send it to the server
MPrintStream. flush ();
Accept code on PC:
while (isConnecting)
{
try
{
Stream peerStream = bluetoothClient.GetStream();
peerStream.Read(buffer, 0, PACKETLENGTH);
//dataprocess();
}
catch (Exception ex)
{
isConnecting = false;
MessageBox.Show(ex.ToString());
}
Pay attention to the following small issues, but these problems have delayed me for a long time:
Inthehand.net. personal is a library that must be used on the PC end. But note that the library function version is about kb when I start using the dll. If the compilation is correct, an error will be reported during running, the system prompts that the dll cannot be found. But later I thought about it and found another dll with the same name, around kb. After changing it, everything will be OK!
The Bluetooth hardware permission of the mobile phone device must be enabled, otherwise it cannot run.
I also want to build a Bluetooth HID device for my mobile phone, but this does not seem to work. Because the methods provided by this library are not adequate for the underlying layer... in short, I want to study it again.
You are welcome to discuss any questions.