Skip to main content

Analysis of Android Bluetooth Connectivity Demo

Free2015-03-16#Android#Solution#蓝牙联机#BluetoothChat

Recently, a friend and I have been tinkering with a mobile game that includes a two-player battle mode. Our preliminary plan is to use Bluetooth for connectivity. I found the official Demo and related materials, and after analyzing and understanding them, I've added detailed comments. Resources are available for download.

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)

II. Demo Testing

After getting the Demo, first confirm it's working:

  1. Import into Eclipse; red crosses appear. Modify the SDK version in AndroidManifest.xml, and the red crosses disappear.
  2. 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.
  3. Finally, changing the resource files solved this problem. The details of the problem and the solution are as follows:
  4. 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 title error and how to solve it?
Reason: The activity theme defined in the XML is inconsistent with the theme defined in the code, resulting in a title style conflict.
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

  1. 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).
  2. Obtain the local Bluetooth adapter (BluetoothAdapter is the Bluetooth interface provided by the system; operate the local Bluetooth through the adapter).
  3. Scan for connectable external Bluetooth devices (obtain information about external devices by receiving broadcast messages returned by the Bluetooth module).
  4. Obtain the MAC address of the external device.
  5. 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).
  6. 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).

Comments

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

Leave a comment