Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7960435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:45:12+00:00 2026-06-04T04:45:12+00:00

I am making a game for Android, in which two devices connect and then

  • 0

I am making a game for Android, in which two devices connect and then trade a small amount of data, then disconnect and use that data. I started with the Bluetooth Chat example. At this point, I have it mostly working, but some (most) times, one of the devices never receives any data. This is the same app on both devices, so the send and receive code for both is the same.
I am connecting like this:

    public BluetoothClientThread(BluetoothDevice get_device,boolean secure){
        BluetoothSocket temp=null;
        device=get_device;
        socket_type=secure?"Secure":"Insecure";

        try{
            if(secure){
                temp=device.createRfcommSocketToServiceRecord(UUID_SECURE);
            }
            else{
                temp=device.createRfcommSocketToServiceRecord(UUID_INSECURE);
            }
        }
        catch(IOException e){
            StorageManager.error_log_add(context,TAG,"create() on "+socket_type+" client socket failed.",e);
        }

        socket=temp;
    }

    public void run(){
        bluetooth_adapter.cancelDiscovery();

        try{
            socket.connect();
        }
        catch(IOException e){
            StorageManager.error_log_add(context,TAG,"Connection failed.",e);

            try{
                socket.close();
            }
            catch(IOException ioe){
                StorageManager.error_log_add(context,TAG,"close() on "+socket_type+" client socket failed during connection failure.",ioe);
            }

            connection_failed();

            return;
        }

        synchronized(BluetoothService.this){
            bluetooth_client_thread=null;
        }

        connected(socket,device,socket_type,false);
    }

Once connected, I grab the socket’s getInputStream() and getOutputStream().
I then use the following code to read input on the socket:

    public void run(){
        String packet="";

        while(true){
            try{
                int get_byte=input.read();
                char get_char=(char)get_byte;

                if(get_char!='\u0003'){
                    StorageManager.error_log_add(context,"BluetoothService","Adding packet raw data: \""+get_char+"\"",null);
                    packet+=get_char;
                }
                else{
                    StorageManager.error_log_add(context,"BluetoothService","Packet received:\n"+packet,null);
                    handler.obtainMessage(Activity_Battle_Menu.HANDLER_READ,packet).sendToTarget();
                    packet="";
                }

                if(done && they_done){
                    handler.obtainMessage(Activity_Battle_Menu.HANDLER_READY).sendToTarget();
                }
            }
            catch(IOException e){
                StorageManager.error_log_add(context,TAG,"Connection lost.",e);

                connection_lost();

                break;
            }
        }
    }

It reads data one byte at a time, appending the bytes to a packet String. When the ETX character is reached (which I stick on the end of everything when I send it), a complete packet has been received, and is passed to the activity via handler, where it is acted upon.
Here is my writing code:

    public void write(byte[] buffer){
        try{
            StorageManager.error_log_add(context,"BluetoothService","Writing data:\n"+new String(buffer),null);

            output.write(buffer);
        }
        catch(IOException e){
            StorageManager.error_log_add(context,TAG,"write() on "+socket_type+" connected socket failed.",e);
        }
    }

As you can see, I write to a log when a data “packet” is sent, when a byte is received, and when a whole “packet” has been received.

I’m not sure if any of this is relevant, but:
I am testing this with a Nook Color with Cyanogenmod 7.1 (Android 2.3.7) and Android x86 running in VirtualBox (Android 2.3.5). My app’s minSdkVersion is 8 (Android 2.2). For the Android running on my computer, I’m using a Rocketfish USB Bluetooth adapter.

There are supposed to be two packets of data sent by each instance.
First, a data packet holding all of the needed game data is sent.
When the other instance’s game data is received, the app sets its own boolean done to true.
It also sends a “done” packet to the other instance.
Thus, once both instances are done, and know the other is done, we know all data has been sent and received, and they both disconnect/stop their Bluetooth threads and act on the data.

Since I have it logging on both devices when data is sent and received, the logs tell me a story of what is going on.

-First, I start both games and go to the Bluetooth activity (obviously).

-Next, I connect to the Nook instance from the emulated instance.

-The emulated instance sends its game data packet.

-The Nook instance receives the emulated instance’s game data, and sends its own (sometimes these two are switched in order, just because they happen asynchronously).

-Since it received a game data packet, the Nook instance now sends its “done” packet.

-That is it. The emulated instance never receives either packet. It never even receives a single byte, and the input.read() just blocks, waiting for input that never arrives (like it should).

So that is my problem. It seems that one of two things is happening:

  1. The data never gets sent by output.write() on the Nook instance, even though it does not throw any exception, and the log verifies that it gets called.

  2. The data never gets received by input.read() on the emulated instance. Maybe it gets lost in transmission?

As far as I can tell, my code is rock solid. The problem seems to be Bluetooth-related. Maybe one or both Android instance has a screwy Bluetooth setup?

I also want to add that every rare once in a while (maybe 1/15 times), all of the packets are sent and received properly, and the game proceeds as it should. So whatever is wrong only USUALLY happens, but not always.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:45:14+00:00Added an answer on June 4, 2026 at 4:45 am

    I got access to another Android device with Bluetooth. I’ve since tested my game, and it connects and the devices trade data correctly. I’ve found several other bugs with my program that occur after the Bluetooth stuff, but that is neither here nor there!

    The Nook Color from before and an HTC Status are what worked together. I’m officially concluding, then, that I have a faulty Bluetooth adapter on my computer. The code posted above works nicely enough.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a game for android which needs to play a small beep
I am making an android game that should delete some falling images from the
I am making an android game that should display a message that you have
I'm making a simple 2d game for the android platform, which works perfectly from
I am making a game on android which will have three threads. These will
I'm making my first Android game which is going to be a 3D arcade-ish
I'm making an android maths game which gives users the some questions to answer
We are in the making of an Android app, a quiz game, which we
I am making game for android using libgdx my requirement is that when character
We create non-game programs that run on mobile devices; iPhones, Windows Mobile, Android, Palm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.