Advertisement
xerpi

NXTDroid alpha 2

Oct 12th, 2012
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.15 KB | None | 0 0
  1. package com.xerpi.nxtdroid;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.util.Set;
  7. import java.util.UUID;
  8.  
  9. import android.os.Bundle;
  10. import android.app.Activity;
  11. import android.bluetooth.BluetoothAdapter;
  12. import android.bluetooth.BluetoothSocket;
  13. import android.bluetooth.BluetoothDevice;
  14. import android.content.Intent;
  15. import android.text.method.ScrollingMovementMethod;
  16. import android.view.View;
  17. import android.view.View.OnClickListener;
  18. import android.widget.Button;
  19. import android.widget.SeekBar;
  20. import android.widget.SeekBar.OnSeekBarChangeListener;
  21. import android.widget.TextView;
  22.  
  23. public class MainActivity extends Activity implements OnClickListener, OnSeekBarChangeListener
  24. {
  25.     //Constants
  26.         private static final int REQUEST_ENABLE_BT = 1;
  27.     //Variables
  28.         int seekBarDurationValue = 50, seekBarFrequencyValue = 50;
  29.         public boolean btEnabled, nxtConnected;
  30.         public TextView tvLog, tvMAC;
  31.         public SeekBar seekBarFrequency, seekBarDuration;
  32.         public Button   sendButton, stopButton, connectButton;
  33.         public BluetoothAdapter btAdapter;
  34.         public BluetoothSocket nxtSocket;
  35.         public OutputStream nxtOutputStream;
  36.         public InputStream nxtInputStream;
  37.         public Set<android.bluetooth.BluetoothDevice> btDevices;
  38.         public BluetoothDevice nxtDevice;
  39.         public String nxtMAC;
  40.         public UUID nxtUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
  41.    
  42.     @Override
  43.     public void onCreate(Bundle savedInstanceState)
  44.     {
  45.         super.onCreate(savedInstanceState);
  46.         setContentView(R.layout.activity_main);
  47.         //Init
  48.             tvLog = (TextView)(findViewById(R.id.textViewLog));
  49.             tvMAC = (TextView)(findViewById(R.id.textViewMAC));
  50.             tvLog.setMovementMethod(ScrollingMovementMethod.getInstance());
  51.             seekBarFrequency = (SeekBar)(findViewById(R.id.seekBarFrequency));
  52.             seekBarDuration = (SeekBar)(findViewById(R.id.seekBarDuration));
  53.             sendButton = (Button)(findViewById(R.id.buttonSend));
  54.             stopButton = (Button)(findViewById(R.id.buttonStop));
  55.             connectButton = (Button)(findViewById(R.id.buttonConnect));
  56.             sendButton.setOnClickListener(this);
  57.             stopButton.setOnClickListener(this);
  58.             connectButton.setOnClickListener(this);
  59.             seekBarDuration.setOnSeekBarChangeListener(this);
  60.             seekBarFrequency.setOnSeekBarChangeListener(this);
  61.         //BT init
  62.             btAdapter = BluetoothAdapter.getDefaultAdapter();
  63.             btEnabled = btAdapter.isEnabled();
  64.             nxtConnected = false;
  65.         // 
  66.             tvLog_append_text("NXTDroid inited.\n");
  67.     }
  68.     @Override
  69.     public void onStart()
  70.     {
  71.         super.onStart();
  72.         if(nxtConnected == true)
  73.         {
  74.             //If NXT is already connected, don't do anything
  75.             return;
  76.         }
  77.         enableBt();
  78.         if(btEnabled == false)
  79.         {
  80.             return;
  81.         }
  82.         btDevices = btAdapter.getBondedDevices();
  83.         if(btDevices.size() > 0)
  84.         {
  85.             nxtMAC = ((BluetoothDevice)(btDevices.toArray()[1])).getAddress();
  86.             tvMAC.setText(nxtMAC);
  87.         }
  88.        
  89.     }
  90.    
  91.     @Override
  92.     public void onActivityResult(int mRequestCode, int mResultCode, Intent mDataIntent)
  93.     {
  94.         switch (mRequestCode)
  95.         {
  96.         case REQUEST_ENABLE_BT:
  97.             if(mResultCode == Activity.RESULT_OK)
  98.             {
  99.                 btEnabled = true;
  100.                 tvLog_append_text("Bluetooth enabled.\n");
  101.             }
  102.             else
  103.             {
  104.                 tvLog_append_text("Could not enable BT.\n");
  105.                 btEnabled = false;
  106.             }
  107.             break;
  108.         default:
  109.             break;
  110.         }
  111.     }
  112.     public void scrollToBottom()
  113.     {
  114.         //scrollView.fullScroll(scrollView.FOCUS_DOWN);
  115.     }
  116.    
  117.     public void tvLog_append_text(String newText)
  118.     {
  119.         tvLog.setText(tvLog.getText() + newText);
  120.         //tvLog.scrollTo(0, scrollView.getBottom());
  121.         //scrollToBottom();
  122.     }
  123.     public void enableBt()
  124.     {
  125.         if(btEnabled == false)
  126.         {
  127.             tvLog_append_text("Enabling bluetooth...\n");
  128.             Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  129.             startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
  130.         }
  131.     }
  132.    
  133.     public void connectNXT() throws IOException
  134.     {
  135.         tvLog_append_text("Connecting to the NXT...");
  136.         if(btEnabled == false)
  137.         {
  138.             tvLog_append_text("BT not enabled.\n");
  139.             return;
  140.         }      
  141.         if(nxtConnected == true)
  142.         {
  143.             tvLog_append_text("already connected ¬¬\n");
  144.             return;
  145.         }
  146.  
  147.         nxtDevice = btAdapter.getRemoteDevice(nxtMAC);
  148.         if(nxtDevice == null)
  149.         {
  150.             tvLog_append_text("could not connect.\n");
  151.             return;        
  152.         }
  153.         nxtSocket = nxtDevice.createRfcommSocketToServiceRecord(nxtUUID);
  154.         nxtSocket.connect();
  155.         nxtInputStream = nxtSocket.getInputStream();
  156.         nxtOutputStream = nxtSocket.getOutputStream();
  157.         tvLog_append_text("Connected.\n"); 
  158.         nxtConnected = true;
  159.     }
  160.  
  161.     public void disconnectNXT() throws IOException
  162.     {
  163.         if(nxtConnected == false)
  164.         {
  165.             tvLog_append_text("It's already disconnected.\n");
  166.             return;
  167.         }
  168.         try {
  169.             nxtOutputStream.close();
  170.         } catch (IOException e) {
  171.             e.printStackTrace();
  172.         }
  173.         try {
  174.             nxtInputStream.close();
  175.         } catch (IOException e) {
  176.             e.printStackTrace();
  177.         }
  178.         try {
  179.             nxtSocket.close();
  180.         } catch (IOException e) {
  181.             e.printStackTrace();
  182.         }
  183.         //nxtDevice.ACTION_ACL_DISCONNECTED;
  184.         //btAdapter.disable();
  185.         nxtConnected = false;
  186.         tvLog_append_text("Disconnected.\n");
  187.     }
  188.    
  189.     public void sendTone(short frequency, short duration)
  190.     {
  191.         if(nxtConnected == false)
  192.         {
  193.             tvLog_append_text("NXT not connected.\n");
  194.             return;
  195.         }
  196.         try
  197.         {
  198.             tvLog_append_text("Sending tone: \n" + "->Frequency: " + frequency + "Hz\n->Duration: " + duration + "ms\n");
  199.             byte[] tone = {(byte)0x80, 0x03, ShortLO(frequency), ShortHI(frequency),
  200.                             ShortLO(duration), ShortHI(duration)};
  201.             nxtOutputStream.write(tone.length);
  202.             nxtOutputStream.write(tone.length >> 8);
  203.             nxtOutputStream.write(tone);
  204.         }
  205.         catch (IOException e) {
  206.             e.printStackTrace();
  207.         }
  208.     }
  209.    
  210.     public void onClick(View v)
  211.     {
  212.         switch(v.getId())
  213.         {
  214.         case R.id.buttonConnect:
  215.             try {
  216.                 connectNXT();
  217.             } catch (IOException e) {
  218.                 e.printStackTrace();
  219.             }          
  220.             break;
  221.         case R.id.buttonSend:
  222.                 sendTone((short) (mapValue(seekBarFrequencyValue, 100, 10000)),
  223.                         (short)mapValue(seekBarDurationValue, 100, 3000));
  224.        
  225.             break;
  226.         case R.id.buttonStop:
  227.             try {
  228.                 disconnectNXT();
  229.             } catch (IOException e) {
  230.                 e.printStackTrace();
  231.             }
  232.         default:
  233.             break;
  234.         }
  235.     }
  236.    
  237.     public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUse) {
  238.         switch(seekBar.getId())
  239.         {
  240.         case R.id.seekBarDuration:
  241.             seekBarDurationValue = progress;
  242.             break;
  243.         case R.id.seekBarFrequency:
  244.             seekBarFrequencyValue = progress;
  245.             break;
  246.         default:
  247.             break; 
  248.         }
  249.     }
  250.    
  251.     public void onStartTrackingTouch(SeekBar arg0) {
  252.        
  253.     }
  254.     public void onStopTrackingTouch(SeekBar arg0) {
  255.        
  256.     }
  257.    
  258.     public int mapValue(int cNum, int maxC, int max)
  259.     {
  260.         return (cNum*max)/maxC;
  261.     }
  262.    
  263.     public byte ShortLO(short num)
  264.     {
  265.         return (byte)(num & 0xFF);
  266.     }
  267.     public byte ShortHI(short num)
  268.     {
  269.         return (byte)(num>>8);
  270.     }  
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement