Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.xerpi.nxtdroid;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.Set;
- import java.util.UUID;
- import android.os.Bundle;
- import android.app.Activity;
- import android.bluetooth.BluetoothAdapter;
- import android.bluetooth.BluetoothSocket;
- import android.bluetooth.BluetoothDevice;
- import android.content.Intent;
- import android.text.method.ScrollingMovementMethod;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.SeekBar;
- import android.widget.SeekBar.OnSeekBarChangeListener;
- import android.widget.TextView;
- public class MainActivity extends Activity implements OnClickListener, OnSeekBarChangeListener
- {
- //Constants
- private static final int REQUEST_ENABLE_BT = 1;
- //Variables
- int seekBarDurationValue = 50, seekBarFrequencyValue = 50;
- public boolean btEnabled, nxtConnected;
- public TextView tvLog, tvMAC;
- public SeekBar seekBarFrequency, seekBarDuration;
- public Button sendButton, stopButton, connectButton;
- public BluetoothAdapter btAdapter;
- public BluetoothSocket nxtSocket;
- public OutputStream nxtOutputStream;
- public InputStream nxtInputStream;
- public Set<android.bluetooth.BluetoothDevice> btDevices;
- public BluetoothDevice nxtDevice;
- public String nxtMAC;
- public UUID nxtUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //Init
- tvLog = (TextView)(findViewById(R.id.textViewLog));
- tvMAC = (TextView)(findViewById(R.id.textViewMAC));
- tvLog.setMovementMethod(ScrollingMovementMethod.getInstance());
- seekBarFrequency = (SeekBar)(findViewById(R.id.seekBarFrequency));
- seekBarDuration = (SeekBar)(findViewById(R.id.seekBarDuration));
- sendButton = (Button)(findViewById(R.id.buttonSend));
- stopButton = (Button)(findViewById(R.id.buttonStop));
- connectButton = (Button)(findViewById(R.id.buttonConnect));
- sendButton.setOnClickListener(this);
- stopButton.setOnClickListener(this);
- connectButton.setOnClickListener(this);
- seekBarDuration.setOnSeekBarChangeListener(this);
- seekBarFrequency.setOnSeekBarChangeListener(this);
- //BT init
- btAdapter = BluetoothAdapter.getDefaultAdapter();
- btEnabled = btAdapter.isEnabled();
- nxtConnected = false;
- //
- tvLog_append_text("NXTDroid inited.\n");
- }
- @Override
- public void onStart()
- {
- super.onStart();
- if(nxtConnected == true)
- {
- //If NXT is already connected, don't do anything
- return;
- }
- enableBt();
- if(btEnabled == false)
- {
- return;
- }
- btDevices = btAdapter.getBondedDevices();
- if(btDevices.size() > 0)
- {
- nxtMAC = ((BluetoothDevice)(btDevices.toArray()[1])).getAddress();
- tvMAC.setText(nxtMAC);
- }
- }
- @Override
- public void onActivityResult(int mRequestCode, int mResultCode, Intent mDataIntent)
- {
- switch (mRequestCode)
- {
- case REQUEST_ENABLE_BT:
- if(mResultCode == Activity.RESULT_OK)
- {
- btEnabled = true;
- tvLog_append_text("Bluetooth enabled.\n");
- }
- else
- {
- tvLog_append_text("Could not enable BT.\n");
- btEnabled = false;
- }
- break;
- default:
- break;
- }
- }
- public void scrollToBottom()
- {
- //scrollView.fullScroll(scrollView.FOCUS_DOWN);
- }
- public void tvLog_append_text(String newText)
- {
- tvLog.setText(tvLog.getText() + newText);
- //tvLog.scrollTo(0, scrollView.getBottom());
- //scrollToBottom();
- }
- public void enableBt()
- {
- if(btEnabled == false)
- {
- tvLog_append_text("Enabling bluetooth...\n");
- Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
- }
- }
- public void connectNXT() throws IOException
- {
- tvLog_append_text("Connecting to the NXT...");
- if(btEnabled == false)
- {
- tvLog_append_text("BT not enabled.\n");
- return;
- }
- if(nxtConnected == true)
- {
- tvLog_append_text("already connected ¬¬\n");
- return;
- }
- nxtDevice = btAdapter.getRemoteDevice(nxtMAC);
- if(nxtDevice == null)
- {
- tvLog_append_text("could not connect.\n");
- return;
- }
- nxtSocket = nxtDevice.createRfcommSocketToServiceRecord(nxtUUID);
- nxtSocket.connect();
- nxtInputStream = nxtSocket.getInputStream();
- nxtOutputStream = nxtSocket.getOutputStream();
- tvLog_append_text("Connected.\n");
- nxtConnected = true;
- }
- public void disconnectNXT() throws IOException
- {
- if(nxtConnected == false)
- {
- tvLog_append_text("It's already disconnected.\n");
- return;
- }
- try {
- nxtOutputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- nxtInputStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- nxtSocket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- //nxtDevice.ACTION_ACL_DISCONNECTED;
- //btAdapter.disable();
- nxtConnected = false;
- tvLog_append_text("Disconnected.\n");
- }
- public void sendTone(short frequency, short duration)
- {
- if(nxtConnected == false)
- {
- tvLog_append_text("NXT not connected.\n");
- return;
- }
- try
- {
- tvLog_append_text("Sending tone: \n" + "->Frequency: " + frequency + "Hz\n->Duration: " + duration + "ms\n");
- byte[] tone = {(byte)0x80, 0x03, ShortLO(frequency), ShortHI(frequency),
- ShortLO(duration), ShortHI(duration)};
- nxtOutputStream.write(tone.length);
- nxtOutputStream.write(tone.length >> 8);
- nxtOutputStream.write(tone);
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void onClick(View v)
- {
- switch(v.getId())
- {
- case R.id.buttonConnect:
- try {
- connectNXT();
- } catch (IOException e) {
- e.printStackTrace();
- }
- break;
- case R.id.buttonSend:
- sendTone((short) (mapValue(seekBarFrequencyValue, 100, 10000)),
- (short)mapValue(seekBarDurationValue, 100, 3000));
- break;
- case R.id.buttonStop:
- try {
- disconnectNXT();
- } catch (IOException e) {
- e.printStackTrace();
- }
- default:
- break;
- }
- }
- public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUse) {
- switch(seekBar.getId())
- {
- case R.id.seekBarDuration:
- seekBarDurationValue = progress;
- break;
- case R.id.seekBarFrequency:
- seekBarFrequencyValue = progress;
- break;
- default:
- break;
- }
- }
- public void onStartTrackingTouch(SeekBar arg0) {
- }
- public void onStopTrackingTouch(SeekBar arg0) {
- }
- public int mapValue(int cNum, int maxC, int max)
- {
- return (cNum*max)/maxC;
- }
- public byte ShortLO(short num)
- {
- return (byte)(num & 0xFF);
- }
- public byte ShortHI(short num)
- {
- return (byte)(num>>8);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement