Mostly i am a server guy. But i had a dream to learn and write android applications. Fortunately i got a chance to write an application recently. it was for my friend. A single screen application for a demo to open and close gate via Bluetooth.
He gave me a Bluetooth module and an arduino micro controller for testing.
* Bluetooth module(sunrom.com)
* Bluetooth module connected to arduino
I learned anroid application development from offical documentaion(http://developer.android.com/training/index.html).
There is a pretty cool Bluetooth interfacing API in android http://developer.android.com/guide/topics/connectivity/bluetooth.html
There is only a single activity, because it was a single screen app. Source code is available on the github (https://github.com/faisalp4p/GateRunnr). Code is self explanatory, i am putting some code here.
public class ControllerActivity extends ActionBarActivity { protected BluetoothAdapter ba; private Set<BluetoothDevice> pairedDevices; private BluetoothSocket sock; private OutputStream bt_stream; private BluetoothDevice device_to_connect; private byte[] OPEN_SIGNAL; private byte[] CLOSE_SIGNAL; private byte[] STOP_SIGNAL; int REQUEST_ENABLE_BT = 0; boolean bluetooth_denied = false; @Override protected void onCreate(Bundle savedInstanceState) { device_to_connect = null; bt_stream = null; sock = null; super.onCreate(savedInstanceState); setContentView(R.layout.activity_controller); getSupportActionBar().hide(); Typeface roboto_thin = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Thin.ttf"); Button open_button = (Button)findViewById(R.id.open_button); Button close_button = (Button)findViewById(R.id.close_button); Button stop_button = (Button)findViewById(R.id.stop); open_button.setTypeface(roboto_thin); close_button.setTypeface(roboto_thin); stop_button.setTypeface(roboto_thin); TextView header = (TextView)findViewById(R.id.header); TextView sub_header = (TextView)findViewById(R.id.sub_header); Typeface pirulen = Typeface.createFromAsset(getAssets(), "fonts/pirulen.ttf"); header.setTypeface(pirulen); sub_header.setTypeface(pirulen); OPEN_SIGNAL = "o".getBytes(); CLOSE_SIGNAL = "c".getBytes(); STOP_SIGNAL = "s".getBytes(); } @Override public void onResume() { super.onResume(); ba = BluetoothAdapter.getDefaultAdapter(); //getting bluetooth adapter if (ba == null) { //Device not support bluetooth Toast.makeText(getApplication(), "Device not support bluetooth", Toast.LENGTH_LONG).show(); } if (!ba.isEnabled()) { Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableIntent, REQUEST_ENABLE_BT); } else { if (!bluetooth_denied) setupConnection(); } } public void setupConnection() { pairedDevices = ba.getBondedDevices(); for(BluetoothDevice bt: pairedDevices) { if(bt.getName().equals("linvor")) { // replace "linvor" with your bluetooth name device_to_connect = bt; System.out.println("Linvor found"); break; } } if (device_to_connect != null) { try { sock = device_to_connect.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")); } catch (IOException e) { Toast.makeText(getApplication(), "Socket cannot create", Toast.LENGTH_LONG).show(); System.out.println("Socket creation failed"); sock = null; //sock cannot create } if (sock != null) { ba.cancelDiscovery(); try { sock.connect(); System.out.println("Socket connection established"); bt_stream = sock.getOutputStream(); } catch (IOException e) { System.out.println("Socket connection cannot create"); Toast.makeText(getApplication(), "blue tooth connot connect", Toast.LENGTH_LONG).show(); System.out.println(e.toString()); //hello } } } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_ENABLE_BT) { if(resultCode == RESULT_OK) { setupConnection(); } if(resultCode == RESULT_CANCELED) { bluetooth_denied = true; Toast.makeText(getApplication(), "Please enable bluetooth", Toast.LENGTH_LONG).show(); finish(); } } } @Override public void onPause() { super.onStop(); if (sock != null) { if (sock.isConnected()) { try { sock.close(); } catch (IOException e) { System.out.println("Socket cannot close " + e.toString()); } } bt_stream = null; } } @Override public void onStop() { super.onStop(); if (sock != null) { if (sock.isConnected()) { try { sock.close(); } catch (IOException e) { System.out.println("Socket cannot close " + e.toString()); } } bt_stream = null; } } public void openClicked(View v) { if (sock != null) { if(sock.isConnected()) { try { bt_stream.write(OPEN_SIGNAL); System.out.println("Open signal send"); } catch (IOException e) { System.out.println("IO error in socket " + e.toString()); } } } //Toast.makeText(getApplication(), "Open Clicked", Toast.LENGTH_LONG).show(); } public void closeClicked(View v) { if (sock != null) { if(sock.isConnected()) { try { bt_stream.write(CLOSE_SIGNAL); } catch (IOException e) { System.out.println("IO error in socket " + e.toString()); } } } } public void onStopSignal(View v) { if (sock != null) { if(sock.isConnected()) { try { bt_stream.write(STOP_SIGNAL); } catch (IOException e) { System.out.println("IO error in socket " + e.toString()); } } } } }