Skip to content

Commit

Permalink
Insert Library Joystick
Browse files Browse the repository at this point in the history
  • Loading branch information
ncioj10 committed Jan 12, 2015
1 parent 61b4aa3 commit 305a8e0
Show file tree
Hide file tree
Showing 35 changed files with 2,144 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions app/app.iml
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,14 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
</content>
<orderEntry type="jdk" jdkName="Android API 21 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="appcompat-v7-21.0.3" level="project" />
<orderEntry type="library" exported="" name="support-v4-21.0.3" level="project" />
<orderEntry type="library" exported="" name="support-annotations-21.0.3" level="project" />
<orderEntry type="module" module-name="library" exported="" />
</component>
</module>

3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ android {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:21.0.3'
compile project(':library')
}
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.jakob.nicolas.rcontroll" >

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"

android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>

</manifest>
154 changes: 147 additions & 7 deletions app/src/main/java/de/jakob/nicolas/rcontroll/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
package de.jakob.nicolas.rcontroll;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import app.akexorcist.bluetotohspp.library.BluetoothSPP;
import app.akexorcist.bluetotohspp.library.BluetoothState;
import app.akexorcist.bluetotohspp.library.DeviceList;

public class MainActivity extends ActionBarActivity {

private static final int REQUEST_ENABLE_BT = 0;
public BluetoothSPP bt;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.add(R.id.container, new BluetoothFragment())
.commit();

bt = new BluetoothSPP(this);
}
}

Expand Down Expand Up @@ -51,16 +64,143 @@ public boolean onOptionsItemSelected(MenuItem item) {
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public static class BluetoothFragment extends Fragment {

private static final String LOG_BT_FRAG = "Button Fragment";
public BluetoothSPP bt;
EditText editText;
Button btnSend;
Intent lastConnected;


public BluetoothFragment() {


public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_main, container, false);

Button buttonConnect = (Button) rootView.findViewById(R.id.button_connect);
bt = new BluetoothSPP(getActivity());



editText = (EditText) rootView.findViewById(R.id.editText);
buttonConnect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(bt.getServiceState() == BluetoothState.STATE_CONNECTED) {
bt.disconnect();
} else {
Intent intent = new Intent(getActivity().getApplicationContext(), DeviceList.class);
startActivityForResult(intent, BluetoothState.REQUEST_CONNECT_DEVICE);
}
}
});

bt.setOnDataReceivedListener(new BluetoothSPP.OnDataReceivedListener() {
public void onDataReceived(byte[] data, String message) {
Toast.makeText(getActivity().getApplicationContext(), message, Toast.LENGTH_SHORT).show();

}
});

bt.setBluetoothConnectionListener(new BluetoothSPP.BluetoothConnectionListener() {
public void onDeviceConnected(String name, String address) {
Toast.makeText(getActivity().getApplicationContext()
, "Connected to " + name + "\n" + address
, Toast.LENGTH_SHORT).show();
}

public void onDeviceDisconnected() {
Toast.makeText(getActivity().getApplicationContext()
, "Connection lost", Toast.LENGTH_SHORT).show();
}

public void onDeviceConnectionFailed() {
Toast.makeText(getActivity().getApplicationContext()
, "Unable to connect", Toast.LENGTH_SHORT).show();
}
});


return rootView;


}

public void onStart() {
super.onStart();
if (!bt.isBluetoothEnabled()) {
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(intent, BluetoothState.REQUEST_ENABLE_BT);
} else {
if (!bt.isServiceAvailable()) {
bt.setupService();
bt.startService(BluetoothState.DEVICE_OTHER);
setup();
}
}


}

public void onResume(){
super.onResume();


}

public void onPause(){
super.onPause();
bt.stopService();

}


public void setup() {
btnSend = (Button) getActivity().findViewById(R.id.button_send);
btnSend.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
bt.send(editText.getText().toString(), true);
Log.e("ButtonSEND", "BUTTON SEND PRESSED" + editText.getText().toString());
}
});

}

@Override
public void onActivityResult(int requestCode, int resultCode,
Intent data) {

if (requestCode == BluetoothState.REQUEST_CONNECT_DEVICE) {
if (resultCode == Activity.RESULT_OK) {
if (bt != null && data != null) {
bt.connect(data);
lastConnected = data;
}


} else if (requestCode == BluetoothState.REQUEST_ENABLE_BT) {
if (resultCode == Activity.RESULT_OK) {
bt.setupService();
bt.startService(BluetoothState.DEVICE_OTHER);

} else {
Toast.makeText(getActivity().getApplicationContext()
, "Bluetooth was not enabled."
, Toast.LENGTH_SHORT).show();
getActivity().finish();
}
}
}
}



}
}
Loading

0 comments on commit 305a8e0

Please sign in to comment.