Android Bluetooth Online demo parsing

Source: Internet
Author: User

Write in front:

There are many ways to implement a two-player game, such as:

    • Online PvP (requires a server responsible for forwarding client requests, various large-scale hand-tour practices)
    • Split screen battle (mobile phone up and down screen, the typical example is to cut the fruit of the two-man battle)
    • Bluetooth online battle (via Bluetooth online, typical example is the Three Kingdoms kill online version)

First of all, online PvP is not considered (high cost, server expensive.) ), and split-screen PvP is too limited (two people to play with a mobile phone, too inconvenient)

So bluetooth online battle is a good choice, low cost, achievable (the Three Kingdoms Kill is the best example, at least to prove that this is feasible)

I. Resource Collection

Search "Android development Bluetooth Online", find a lot of useful information, after screening to find that most of the information is related to Bluetoothchat, understand that Bluetoothchat is an official demo, it is good to run

First the source down, and by the way to find the source of the predecessors of the analysis, a lot of things, a simple collation, the resources are as follows: (the first two from the network, the latter two are freshly baked)

    • Official demo Source
    • Predecessor's source parsing PDF preview
    • APK file (for 2.3.5 to 4.2.2)
    • Source code with detailed comments

Two. Demo Test

Get the demo first to make sure you can use:

    1. Import eclipse, Red Fork, modify SDK version in Androidmanifest.xml, Red Fork is gone.
    2. Connect phone, install run, program abort, check log, error message for you can not combine custom titles with other title
    3. The last change to the resource file solves this problem, and the problem details and solutions are as follows:
    4. Test successful, real-computer online chat no problem

Problems you may encounter with the demo:

    • Can not combine custom titles with other title error causes and how to resolve them?
Reason: The theme of the activity defined in the XML is inconsistent with the theme of the code definition, that is, the title style conflict
Solution: Change the Theme in the corresponding activity in XML to @android:style/theme
Note: If the activity in the XML does not have a theme tag, the theme tag should be added and set to the value above

The real-machine operation effect is as follows:

Three. Bluetooth online process

    1. Declare Bluetooth-related permissions (for more information on the rights of the predecessor of the source parsing, do not repeat here)
    2. Get the native Bluetooth adapter (Bluetoothadapter is a system-provided Bluetooth interface that operates native Bluetooth via the adapter)
    3. Scan for external Bluetooth devices that can be connected (receive information about external devices by receiving broadcast messages returned by the Bluetooth module)
    4. Get the MAC address of an external device
    5. A socket connection is established via the MAC address (a socket connection is good, and the following work is no different from the usual socket chat program)
    6. Send and receive messages through the socket connection

Simply put, the online process is the above steps, the author of the source to understand the analysis (referring to the predecessor of the source parsing), supplemented by more readable comments, such as:

Four. Summary

Source code Analysis is a time-consuming thing, but if there is time, this kind of pay is definitely worth, better than the simple copy-modify, in the analysis of other people's code process, we can get these things:

    • Implementation principles (such as the Division and interaction of the various components used to implement the function, rather than simply understanding the function of the code block)
    • Coding Specifications (for example, it's very curious at first why to divide methods, classify, layering ...) )
    • Coding style (variables, methods, classes, package naming rules, indentation style ...) )
    • Tips/Common sense (for example, in order to avoid the removal of debugging information before the release of the work, you can define an identity variable representation in the debugging phase, all output debugging information in the code wrapped in the If block, only need to change the identity variable to false before publishing)

Android Bluetooth Online demo parsing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.