Foreword:
There are many ways to implement two-player battles in mobile games, such as:
- Online battles (requires a server to forward client requests, the method used by various large-scale mobile games)
- Split-screen battles (top and bottom split-screen on one phone, a typical example is the two-player mode in Fruit Ninja)
- Bluetooth connectivity battles (connecting via Bluetooth, a typical example is the online version of SanguoKill)
First, online battles are not considered (high cost, expensive servers...), and split-screen battles have too many limitations (playing on one phone with two people is very inconvenient).
Therefore, Bluetooth connectivity battles are a good choice: low cost and feasible (SanguoKill is the best example, proving at least that it's possible).
I. Resource Collection
Searching for "Android development Bluetooth connectivity" yielded a lot of useful information. After filtering, I found that most information is related to BluetoothChat. Upon investigation, I discovered that BluetoothChat is an official Demo, which makes things much easier.
I downloaded the source code first and happened to find a senior's source code analysis, which saved a lot of trouble. I've briefly organized the resources as follows: (The first two are from the web, and the last two are freshly prepared)
- Official Demo Source Code
- Senior's Source Code Analysis pdf preview
- apk file (suitable for version 2.3.5 to 4.2.2)
- Source code with detailed comments
II. Demo Testing
After getting the Demo, first confirm it's working:
- Import into Eclipse; red crosses appear. Modify the SDK version in AndroidManifest.xml, and the red crosses disappear.
- Connect the phone, install, and run. The program terminates abnormally. Checking the Log, the error message is:
you can not combine custom titles with other title. - Finally, changing the resource files solved this problem. The details of the problem and the solution are as follows:
- Testing successful; online chat on actual devices works fine.
Problems you might encounter when using the Demo:
- What is the cause of the
you can not combine custom titles with other titleerror and how to solve it?
Solution: Change the theme of the corresponding activity in the XML to @android:style/Theme
Note: If the activity in the XML does not have a theme tag, you should add a theme tag and set it to the value above.
Running effect on actual devices is as follows:
III. Bluetooth Connectivity Process
- Declare Bluetooth-related permissions (detailed information about permissions can be found in the senior's source code analysis, so I won't repeat it here).
- Obtain the local Bluetooth adapter (BluetoothAdapter is the Bluetooth interface provided by the system; operate the local Bluetooth through the adapter).
- Scan for connectable external Bluetooth devices (obtain information about external devices by receiving broadcast messages returned by the Bluetooth module).
- Obtain the MAC address of the external device.
- Establish a Socket connection through the MAC address (once you have a Socket connection, it's easy; the following work is no different from a general Socket chat program).
- Send and receive messages through the Socket connection.
Simply put, the connectivity process consists of the steps above. After understanding and analyzing the source code (referencing the senior's source code analysis), I added more readable comments, such as:
IV. Summary
Source code analysis is a time-consuming and labor-intensive task, but if you have the time, such effort is definitely worth it. It's much better than simple copy-modify. In the process of analyzing others' code, we can gain these things:
- Implementation principles (e.g., the division of labor and interaction of various components used to implement functionality, rather than just simply understanding the function of code blocks).
- Coding standards (e.g., at first you might be very curious why methods, classes, and layers are separated...).
- Coding style (naming rules for variables, methods, classes, packages; indentation style...).
- Tips/Common sense (e.g., to avoid the work of deleting debugging information before release, you can define a flag variable indicating the debugging stage, wrap all code that outputs debugging information in an if-block, and just change the flag variable to false before release).

No comments yet. Be the first to share your thoughts.