Skip to content

Instantly share code, notes, and snippets.

@ageldama
Forked from dvas0004/aquariumDroid.java
Created April 24, 2016 20:01
Show Gist options
  • Save ageldama/9444f609944cbfeedfff7bd6bccf4708 to your computer and use it in GitHub Desktop.
Save ageldama/9444f609944cbfeedfff7bd6bccf4708 to your computer and use it in GitHub Desktop.
aquariumDroid.java - android companion app to aquaPi.py
package me.davidvassallo.aquarium;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.UUID;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
BluetoothSocket mmSocket;
BluetoothDevice mmDevice = null;
final byte delimiter = 33;
int readBufferPosition = 0;
public void sendBtMsg(String msg2send){
//UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard SerialPortService ID
UUID uuid = UUID.fromString("94f39d29-7d6d-437d-973b-fba39e49d4ee"); //Standard SerialPortService ID
try {
mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
if (!mmSocket.isConnected()){
mmSocket.connect();
}
String msg = msg2send;
//msg += "\n";
OutputStream mmOutputStream = mmSocket.getOutputStream();
mmOutputStream.write(msg.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Handler handler = new Handler();
final TextView myLabel = (TextView) findViewById(R.id.btResult);
final Button tempButton = (Button) findViewById(R.id.tempButton);
final Button lightOnButton = (Button) findViewById(R.id.lightOn);
final Button lightOffButton = (Button) findViewById(R.id.lightOff);
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
final class workerThread implements Runnable {
private String btMsg;
public workerThread(String msg) {
btMsg = msg;
}
public void run()
{
sendBtMsg(btMsg);
while(!Thread.currentThread().isInterrupted())
{
int bytesAvailable;
boolean workDone = false;
try {
final InputStream mmInputStream;
mmInputStream = mmSocket.getInputStream();
bytesAvailable = mmInputStream.available();
if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable];
Log.e("Aquarium recv bt","bytes available");
byte[] readBuffer = new byte[1024];
mmInputStream.read(packetBytes);
for(int i=0;i<bytesAvailable;i++)
{
byte b = packetBytes[i];
if(b == delimiter)
{
byte[] encodedBytes = new byte[readBufferPosition];
System.arraycopy(readBuffer, 0, encodedBytes, 0, encodedBytes.length);
final String data = new String(encodedBytes, "US-ASCII");
readBufferPosition = 0;
//The variable data now contains our full command
handler.post(new Runnable()
{
public void run()
{
myLabel.setText(data);
}
});
workDone = true;
break;
}
else
{
readBuffer[readBufferPosition++] = b;
}
}
if (workDone == true){
mmSocket.close();
break;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
// start temp button handler
tempButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on temp button click
(new Thread(new workerThread("temp"))).start();
}
});
//end temp button handler
//start light on button handler
lightOnButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on temp button click
(new Thread(new workerThread("lightOn"))).start();
}
});
//end light on button handler
//start light off button handler
lightOffButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on temp button click
(new Thread(new workerThread("lightOff"))).start();
}
});
// end light off button handler
if(!mBluetoothAdapter.isEnabled())
{
Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetooth, 0);
}
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if(pairedDevices.size() > 0)
{
for(BluetoothDevice device : pairedDevices)
{
if(device.getName().equals("raspberrypi-0")) //Note, you will need to change this to match the name of your device
{
Log.e("Aquarium",device.getName());
mmDevice = device;
break;
}
}
}
}
@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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment