Samsung Galaxy Tab 3 to NXT via Bluetooth (Help Wanted)

Post your NXJ projects, project ideas, etc here!

Moderators: 99jonathan, roger, imaqine

Samsung Galaxy Tab 3 to NXT via Bluetooth (Help Wanted)

Postby ChromeAxl » Fri Feb 21, 2014 12:42 am

Hello, I'm relatively new to Java and leJOS but I'm starting to catch on to some of the quirks. :)

There's a project I'm working on, the first goal is to get my Tab 3 to connect to the NXT via Bluetooth. Once that is accomplished I intend to have my app on my Tab 3 include buttons each equipped with a unique message to send to the NXT. The NXT will then read each message it receives and, say it received the string "4" will execute a task programmed only to execute should that number be received. Thus, when complete, you will initiate both the NXT and the Tablet, connect via Bluetooth, and press each button for each desired effect. I can go into further detail as to what actions are to be accomplished and the theme if desired, but I figure that it is currently irrelevant to this post.

Now I've looked vigorously for someone who has done this before, and I've found several people with similar projects but with little-to-no code given that I may learn from. I work best out of visual tutorials, but at this point I'll take any personalized assistance possible. I'm looking to see if anybody can assist in at the BARE MINIMUM to help with connecting my Tab 3 to the NXT via Bluetooth. If I can accomplish that, and get data sent between the two the way I described above, then the rest I could always handle on my own if that's as far as people are willing to help me with. This project needs to be completed by the end of the 2nd week of March (2014) so I have some time.

I have downloaded the samples for Android-leJOS, including the BTSend.java file but I am unfamiliar as to how to integrate that file into a standard .apk with which it may initiate contact with the NXJ.

For reference, the BTSend.java file as is for Android (with my project name already in place).

Code: Select all
[textarea rows="10" cols="30"]
package com.example.androidproject;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import lejos.android.LeJOSDroid.CONN_TYPE;
import lejos.pc.comm.NXTConnector;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

public class BTSend extends Thread {
   static final String TAG = "BTSend";
   private NXTConnector conn;
   Handler mUIMessageHandler;
   DataOutputStream dos;
   DataInputStream dis;

   public BTSend(Handler mUIMessageHandler) {

      super();
      this.mUIMessageHandler = mUIMessageHandler;
   }

   public void closeConnection() {
      try {
         Log.d(TAG, "BTSend run loop finished and closing");

         dis.close();
         dos.close();
         conn.getNXTComm().close();
      } catch (Exception e) {
      } finally {
         dis = null;
         dos = null;
         conn = null;
      }
   }

   @Override
   public void run() {
      Log.d(TAG, "BTSend run");
      Looper.prepare();
      
      conn = LeJOSDroid.connect(CONN_TYPE.LEJOS_PACKET);

      dos = conn.getDataOut();
      dis = conn.getDataIn();
      int x;
      for (int i = 0; i < 100; i++) {

         try {
            dos.writeInt((i * 30000));
            dos.flush();
            LeJOSDroid.sendMessageToUIThread("sent:" + i * 30000);
            yield();
            x = dis.readInt();
            if (x > 0) {

               LeJOSDroid.displayToastOnUIThread("got: " + x);
            }
            Log.d(TAG, "sent:" + i * 30000 + " got:" + x);
            LeJOSDroid.sendMessageToUIThread("sent:" + i * 30000 + " got:" + x);
            yield();
         } catch (IOException e) {
            Log.e(TAG, "Error ... ", e);

         }

      }

      closeConnection();
      Looper.loop();
      Looper.myLooper().quit();
      LeJOSDroid.sendMessageToUIThread("");//clear
      LeJOSDroid.displayToastOnUIThread("BTSend finished it's run");
   }

}



Now the standard .apk I have currently is merely a tutorial I followed off the Android Developer site. (Link Provided)
https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&sqi=2&ved=0CCUQFjAA&url=https%3A%2F%2Fdeveloper.android.com%2Ftraining%2Fbasics%2Ffirstapp%2Findex.html%3Fhl%3Dcn&ei=hp4GU-z9NrK70AHbsIGYDw&usg=AFQjCNF5GCHFsC30dSeHz_R3o8htmVA9Hg&sig2=Ip038WDaXPHX6oQmKOW_2g&bvm=bv.61725948,d.dmQ


Name: AndroidProject
Package: com.example.androidproject

The Code in Full is divided into 2 classes currently:

MainActivity.java
Code: Select all
package com.example.androidproject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends Activity {

   public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }
   
   public void sendMessage (View view) {
      Intent intent = new Intent(this, DisplayMessageActivity.class);
      EditText editText = (EditText) findViewById(R.id.edit_message);
      String message = editText.getText().toString();
      intent.putExtra(EXTRA_MESSAGE, message);
      startActivity(intent);
   }

}


DisplayMessageActivity.java
Code: Select all
package com.example.androidproject;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;

public class DisplayMessageActivity extends Activity {

   @SuppressLint("NewApi")
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      
      // Get the message from the intent
      Intent intent = getIntent();
      String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
      
      // Create the text view
      TextView textView = new TextView(this);
      textView.setTextSize(40);
      textView.setText(message);
      
      // Set the text view as the activity layout      
      setContentView(textView);
      // Show the Up button in the action bar.
      
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
         getActionBar().setDisplayHomeAsUpEnabled(true);
      }


   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      case android.R.id.home:
         // This ID represents the Home or Up button. In the case of this
         // activity, the Up button is shown. Use NavUtils to allow users
         // to navigate up one level in the application structure. For
         // more details, see the Navigation pattern on Android Design:
         //
         // http://developer.android.com/design/patterns/navigation.html#up-vs-back
         //
         NavUtils.navigateUpFromSameTask(this);
         return true;
      }
      return super.onOptionsItemSelected(item);
   }

}


I do not at this point in time have an NXJ Program written to accept Bluetooth commands. Presumably it would be a spinoff of "BTReceive" for NXJ.

Thank you for your time, and I await eagerly for anyone interested in helping me. In the mean time I will continue to pursue this knowledge of my own volition. :D
ChromeAxl
New User
 
Posts: 1
Joined: Thu Feb 20, 2014 5:36 pm

Return to NXJ Projects

Who is online

Users browsing this forum: Yahoo [Bot] and 3 guests

more stuff