Analysis of Android Bluetooth Online Demo
There are many ways to achieve dual-player combat for mobile games, such as online combat (a server is required to forward client requests and various large mobile games, A typical example is the dual game of Fruit Cutting) Online Bluetooth combat (via Bluetooth, a typical example is the Three Kingdoms Kill online version) First, online combat is not considered (high cost, expensive server ..), however, split-screen combat has too many limitations (it is too inconvenient for two people to play with one mobile phone). Therefore, Bluetooth online combat is a good option, with a low cost and achievable effect (Three Kingdoms Kill is the best example, at least proved this is feasible) 1. search for "android development Bluetooth online" by resource collection and find a lot of useful information. After filtering, most of the information is found to be related to thchat. After learning about it, I found thchat is an official Demo, this makes it easy to get the source code down first, and find the source code parsing of the predecessors by the way, saving a lot of things, a simple arrangement, the resources are as follows: (the first two are from the network, the last two are freshly released) official Demo source code for the predecessors of the source code parsing pdf preview apk file (applicable to 2.3. 5 to 4.2.2) source code with detailed comments 2. in the Demo test, obtain the Demo and then click Import Eclipse. If a Red Cross occurs, modify AndroidManifest. SDK version in xml, the Red Cross does not connect to the mobile phone, installation and running, the program ends abnormally, check the Log, the error message is you can not combine custom titles with other title. Finally, the resource file is changed to solve this problem. The problem details and solutions are as follows: test successful, real machine online chat is okay. problems that may occur during Demo use: you can not combine custom titles with other title error cause and how to solve it? Cause: the theme of the activity defined in xml is inconsistent with the theme defined in the Code, that is, the title style conflict solution: change the theme of the activity in xml to @ android: style/Theme Note: if the activity in xml does not have the theme tag, add the theme tag and set it to the preceding value. The running effect of the real machine is as follows: 3. the Bluetooth online process declares Bluetooth-related permissions (for details about permissions, refer to the source code parsing of the predecessors and do not go into details here). Obtaining the local Bluetooth adapter is the Bluetooth interface provided by the system, use an adapter to operate on the local Bluetooth device) scan the externally accessible bluetooth device (obtain information about the external device by receiving the broadcast message returned by the bluetooth module) obtain the MAC address of an external device and establish a Socket connection through the MAC address (with the Socket connection, it is easy to do, the following work is no different from the general Socket chat program) simply put, the online process is the above steps. After the author analyzes the source code (referring to the source code parsing of the predecessors), it adds more readable comments, example: 4. summing up source code analysis is a time-consuming task, but if you have time, this is definitely worth it, much better than simply copy-modify, in the process of analyzing others' code, we can get these things: implementation principle (for example, the division of labor and interaction of each component of the function, rather than simply understanding the functions of the code block) coding specifications (for example, at the beginning, I would be curious why we need to classify methods, classes, and layers ...) encoding style (naming rules for variables, methods, classes, and packages, indentation style ...) tips/common sense (for example, to avoid debugging information deletion before release, you can define an identifier variable to indicate that the debugging is in the debugging phase and wrap all the code that outputs the debugging information in the if block, you only need to change the identifier variable to false before publishing)