import apwidgets.*;
//App-Wide (General) Elements
APWidgetContainer General_Container;
APRadioButton General_Setup_RB;
APRadioButton General_Interact_RB;
APRadioGroup General_RadioGroup;
//Setup Mode Elements
APWidgetContainer SetupMode_Container;
APButton SetupMode_ConnectButton;
APButton SetupMode_DisconnectButton;
//Interact Mode Elements
APWidgetContainer InteractMode_Container;
APButton InteractMode_SendButton;
APEditText InteractMode_TypeTB;
APEditText InteractMode_ExposuresTB;
APEditText InteractMode_DelayTB;
/* -------------- Bluetooth Stuff -------------- */
//required for BT enabling on startup
import android.content.Intent;
import android.os.Bundle;
import ketai.net.bluetooth.*;
import ketai.ui.*;
import ketai.net.*;
import oscP5.*;
KetaiBluetooth bt;
String info = "";
KetaiList klist;
PVector remoteMouse = new PVector();
ArrayList<String> devicesDiscovered = new ArrayList();
boolean isConfiguring = true;
String UIText;
//********************************************************************
// The following code is required to enable bluetooth at startup.
//********************************************************************
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bt = new KetaiBluetooth(this);
}
void onActivityResult(int requestCode, int resultCode, Intent data) {
bt.onActivityResult(requestCode, resultCode, data);
}
void setup(){
//App-Wide (General) Elements
General_Container = new APWidgetContainer(this); //create new container for widgets
General_RadioGroup = new APRadioGroup(10, 10); //create a new radiogroup
General_RadioGroup.setOrientation(APRadioGroup.HORIZONTAL);
General_Setup_RB = new APRadioButton("Setup "); //create new radiobutton from label.
General_Interact_RB = new APRadioButton("Interact "); //create new radiobutton from label.
General_RadioGroup.addRadioButton(General_Setup_RB); //place radiobutton in radiogroup
General_RadioGroup.addRadioButton(General_Interact_RB); //place radiobutton in radiogroup
General_Container.addWidget(General_RadioGroup);
//Setup Mode Elements
SetupMode_Container = new APWidgetContainer(this);
SetupMode_ConnectButton = new APButton(5, 200, 900, 100, "Start Connection");
SetupMode_DisconnectButton = new APButton(5, 300, 900, 100, "Stop Connection");
SetupMode_Container.addWidget(SetupMode_ConnectButton);
SetupMode_Container.addWidget(SetupMode_DisconnectButton);
//Interact Mode Elements
InteractMode_Container = new APWidgetContainer(this);
InteractMode_SendButton = new APButton(5, 500, 900, 100, "SendToBT");
InteractMode_TypeTB = new APEditText(5, 150, 350, 100);
InteractMode_ExposuresTB = new APEditText(5, 250, 350, 100);
InteractMode_DelayTB = new APEditText(5, 350, 350, 100);
InteractMode_Container.addWidget(InteractMode_SendButton);
InteractMode_Container.addWidget(InteractMode_TypeTB);
InteractMode_Container.addWidget(InteractMode_ExposuresTB);
InteractMode_Container.addWidget(InteractMode_DelayTB);
//Finishing Touches
General_Setup_RB.setChecked(true); //Setup mode is selected by default
SetupMode_Container.hide();
InteractMode_Container.hide();
}
void draw(){
if(General_Setup_RB.isChecked()){
background(4, 49, 50);
SetupMode_Container.show();
InteractMode_Container.hide();
}
else if(General_Interact_RB.isChecked()){
background(50, 4, 48);
//creates text on screen
textSize(32);
text("Mode: " + InteractMode_TypeTB.getText(), 370, 200);
textSize(32);
text("Exposures: " + InteractMode_ExposuresTB.getText(), 370, 300);
textSize(32);
text("Delay: " + InteractMode_DelayTB.getText(), 370, 400);
SetupMode_Container.hide();
InteractMode_Container.show();
}
}
void onClickWidget(APWidget widget){
if(widget == SetupMode_ConnectButton){
bt.start(); //start listening for BT connections
if (bt.getDiscoveredDeviceNames().size() > 0) //If we have not discovered any devices, try prior paired devices
klist = new KetaiList(this, bt.getDiscoveredDeviceNames());
else if (bt.getPairedDeviceNames().size() > 0)
klist = new KetaiList(this, bt.getPairedDeviceNames());
}
if(widget == SetupMode_ConnectButton){
bt.stop(); //start listening for BT connections
}
if (widget == InteractMode_SendButton){
String m = InteractMode_TypeTB.getText() + ',' + InteractMode_ExposuresTB.getText() + ',' + InteractMode_DelayTB.getText() + '.'; //translates the selection by the user to
print(m.getBytes()+ " , "+ m);
bt.broadcast(m.getBytes()) ;
}
}
void onKetaiListSelection(KetaiList klist) { //Recives your selection
String selection = klist.getSelection();
text(str(bt.connectToDeviceByName(selection)),10,100);
//dispose of list for now
klist = null;
}
void onBluetoothDataEvent(String who, byte[] data) { //Call back method to manage data received
if (isConfiguring)
return;
//KetaiOSCMessage is the same as OscMessage
// but allows construction by byte array
KetaiOSCMessage m = new KetaiOSCMessage(data);
if (m.isValid())
{
if (m.checkAddrPattern("/remoteMouse/"))
{
if (m.checkTypetag("ii"))
{
remoteMouse.x = m.get(0).intValue();
remoteMouse.y = m.get(1).intValue();
}
}
}
}
String getBluetoothInformation(){
String btInfo = "Server Running: ";
btInfo += bt.isStarted() + "\n";
btInfo += "Discovering: " + bt.isDiscovering() + "\n";
btInfo += "Device Discoverable: "+bt.isDiscoverable() + "\n";
btInfo += "\nConnected Devices: \n";
ArrayList<String> devices = bt.getConnectedDeviceNames();
for (String device: devices)
{
btInfo+= device+"\n";
}
return btInfo;
}