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 3951262
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:42:36+00:00 2026-05-20T01:42:36+00:00

Does anybody know, how to build a bluetooth connection between Android and LEGO-Mindstorm-NXT? The

  • 0

Does anybody know, how to build a bluetooth connection between Android and LEGO-Mindstorm-NXT?
The connection between two NXTs works fine. But the other Connection-type likes not so easy.
I am working with the LeJOS Firmware 0.85 and the Android SDK Tools (2.2 Froyo).

  • 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-05-20T01:42:37+00:00Added an answer on May 20, 2026 at 1:42 am

    So i’ve solved it and will show all how does it works, because i’ve seen that a lot of people have problems with that.

    The class includes 4 functions:

    • Bluetooth enable if not enabled before -> enableBT()
    • Connect to 2 NXTs -> connectToNXTs()
    • Write Message to one of the NXTs -> writeMessage(byte msg, String nxt)
    • Read Message from one of the NXTs -> readMessage(String nxt)

    Here is the code for the android device (BT_comm.java):

    package de.joen.android.CubeScan;
    
    
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.util.UUID;
    
    
    import android.bluetooth.BluetoothAdapter;
    import android.bluetooth.BluetoothDevice;
    import android.bluetooth.BluetoothSocket;
    
    import android.util.Log;
    
    public class BT_Comm {
    
      //Target NXTs for communication
      final String nxt2 = "00:16:53:04:52:3A";
      final String nxt1 = "00:16:53:07:AA:F6";
    
      BluetoothAdapter localAdapter;
      BluetoothSocket socket_nxt1, socket_nxt2;
      boolean success = false;
    
      // Enables Bluetooth if not enabled
      public void enableBT(){
        localAdapter = BluetoothAdapter.getDefaultAdapter();
        // If Bluetooth not enable then do it
        if (!localAdapter.isEnabled()) {
          localAdapter.enable();
          while(!(localAdapter.isEnabled()));
        }
      }
    
      // Connect to both NXTs
      public boolean connectToNXTs() {
    
        // Get the BluetoothDevice of the NXT
        BluetoothDevice nxt_2 = localAdapter.getRemoteDevice(nxt2);
        BluetoothDevice nxt_1 = localAdapter.getRemoteDevice(nxt1);
        // Try to connect to the nxt
        try {
          socket_nxt2 = nxt_2.createRfcommSocketToServiceRecord(UUID
              .fromString("00001101-0000-1000-8000-00805F9B34FB"));
    
          socket_nxt1 = nxt_1.createRfcommSocketToServiceRecord(UUID
              .fromString("00001101-0000-1000-8000-00805F9B34FB"));
    
          socket_nxt2.connect();    
          socket_nxt1.connect();      
    
          success = true;
    
        } catch (IOException e) {
          Log.d("Bluetooth","Err: Device not found or cannot connect");
          success=false;
        }
        return success;    
      }
    
    
      public void writeMessage(byte msg, String nxt) throws InterruptedException {
        BluetoothSocket connSock;
    
        // Swith nxt socket
        if (nxt.equals("nxt2")) {
          connSock=socket_nxt2;
        } else if(nxt.equals("nxt1")) {
          connSock = socket_nxt1;
        } else {
          connSock=null;
        }
    
        if (connSock!=null) {
          try {
    
            OutputStreamWriter out = new OutputStreamWriter(connSock.getOutputStream());
            out.write(msg);
            out.flush();
    
            Thread.sleep(1000);
    
          } catch (IOException e) {
            // TODO: Auto-generated catch block
            e.printStackTrace();
          }
        } else {
          // Error
        }
      }
    
      public int readMessage(String nxt) {
        BluetoothSocket connSock;
        int n;
    
        // Swith nxt socket
        if (nxt.equals("nxt2")) {
          connSock=socket_nxt2;
        } else if (nxt.equals("nxt1")) {
          connSock=socket_nxt1;
        } else {
          connSock=null;
        }
    
        if (connSock!=null) {
          try {
    
            InputStreamReader in = new InputStreamReader(connSock.getInputStream());
            n = in.read();
            return n;
    
          } catch (IOException e) {
            // TODO: Auto-generated catch block
            e.printStackTrace();
            return -1;
          }
        } else {
          // Error
          return -1;
        }
      }
    }
    

    To get messages from the Android Smartphone you must have a read call on the NXT-side.
    Here is the code from the NXT-side wich will accept the connection from the Smartphone and read messages from it:

    Boolean isrunning = true;
    
    // Main loop   
    while (true)
    {
      LCD.drawString(waiting,0,0);
      LCD.refresh();
    
      // Listen for incoming connection
    
      NXTConnection btc = Bluetooth.waitForConnection();
    
      btc.setIOMode(NXTConnection.RAW);
    
      LCD.clear();
      LCD.drawString(connected,0,0);
      LCD.refresh();  
    
    
      // The InputStream for read data 
      DataInputStream dis = btc.openDataInputStream();
    
    
      // Loop for read data  
      while (isrunning) {
        Byte n = dis.readByte();
        LCD.clear();
        LCD.drawInt(n, 4, 4);
      }
    
      dis.close();
    
      // Wait for data to drain
      Thread.sleep(100); 
    
      LCD.clear();
      LCD.drawString(closing,0,0);
      LCD.refresh();
    
      btc.close();
    
      LCD.clear();
    }
    

    Hope this will help others…

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

Sidebar

Related Questions

Does anybody know how I can run a gant build which ignores build errors?
Does anybody know or have any documents which I can use to build a
Does anybody know of a Windows FTP client with SOCKS support? I need it
Does anybody know a way to recursively remove all files in a working copy
Does anybody know where I can find a free filter that would make a
Does anybody know how to create a text field using PyGTK that only accepts
Does anybody know how to disable or manipulate the (in most browsers) dashed border
do you know an alternative to jrails? It's more or less outdated (uses jQuery
We are currently using a single command line tool to build our product on
I would like to use pysqlite interface between Python and sdlite database. I have

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.