diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 736c7b5..0ea455b 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -9,6 +9,8 @@ diff --git a/.idea/misc.xml b/.idea/misc.xml index 75fdab9..9076de5 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,8 @@ + + + diff --git a/.idea/modules.xml b/.idea/modules.xml index 52dca4a..790947c 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -4,6 +4,8 @@ + + diff --git a/.idea/vcs.xml b/.idea/vcs.xml index def6a6a..9d32e50 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -2,6 +2,7 @@ + diff --git a/app/app.iml b/app/app.iml index 22352c9..44fe74c 100644 --- a/app/app.iml +++ b/app/app.iml @@ -79,12 +79,14 @@ + + diff --git a/app/build.gradle b/app/build.gradle index 1f6a070..e3b7cfc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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') } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index cada22e..761dece 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,9 @@ + + + @@ -16,6 +20,7 @@ + diff --git a/app/src/main/java/de/jakob/nicolas/rcontroll/MainActivity.java b/app/src/main/java/de/jakob/nicolas/rcontroll/MainActivity.java index 05e0d28..2bc9316 100644 --- a/app/src/main/java/de/jakob/nicolas/rcontroll/MainActivity.java +++ b/app/src/main/java/de/jakob/nicolas/rcontroll/MainActivity.java @@ -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); } } @@ -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(); + } + } + } + } + + + } } diff --git a/app/src/main/java/de/jakob/nicolas/rcontroll/SimpleActivity.java b/app/src/main/java/de/jakob/nicolas/rcontroll/SimpleActivity.java new file mode 100644 index 0000000..b92ccdc --- /dev/null +++ b/app/src/main/java/de/jakob/nicolas/rcontroll/SimpleActivity.java @@ -0,0 +1,132 @@ +/* + * Copyright 2014 Akexorcist + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package de.jakob.nicolas.rcontroll; + +import android.app.Activity; +import android.bluetooth.BluetoothAdapter; +import android.content.Intent; +import android.os.Bundle; +import android.view.View; +import android.view.View.OnClickListener; +import android.widget.Button; +import android.widget.Toast; + +import app.akexorcist.bluetotohspp.library.BluetoothSPP; +import app.akexorcist.bluetotohspp.library.BluetoothSPP.BluetoothConnectionListener; +import app.akexorcist.bluetotohspp.library.BluetoothSPP.OnDataReceivedListener; +import app.akexorcist.bluetotohspp.library.BluetoothState; +import app.akexorcist.bluetotohspp.library.DeviceList; + +public class SimpleActivity extends Activity { + BluetoothSPP bt; + + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_simple); + + bt = new BluetoothSPP(this); + + if(!bt.isBluetoothAvailable()) { + Toast.makeText(getApplicationContext() + , "Bluetooth is not available" + , Toast.LENGTH_SHORT).show(); + finish(); + } + + bt.setOnDataReceivedListener(new OnDataReceivedListener() { + public void onDataReceived(byte[] data, String message) { + Toast.makeText(SimpleActivity.this, message, Toast.LENGTH_SHORT).show(); + } + }); + + bt.setBluetoothConnectionListener(new BluetoothConnectionListener() { + public void onDeviceConnected(String name, String address) { + Toast.makeText(getApplicationContext() + , "Connected to " + name + "\n" + address + , Toast.LENGTH_SHORT).show(); + } + + public void onDeviceDisconnected() { + Toast.makeText(getApplicationContext() + , "Connection lost", Toast.LENGTH_SHORT).show(); + } + + public void onDeviceConnectionFailed() { + Toast.makeText(getApplicationContext() + , "Unable to connect", Toast.LENGTH_SHORT).show(); + } + }); + + Button btnConnect = (Button)findViewById(R.id.btnConnect); + btnConnect.setOnClickListener(new OnClickListener(){ + public void onClick(View v){ + if(bt.getServiceState() == BluetoothState.STATE_CONNECTED) { + bt.disconnect(); + } else { + Intent intent = new Intent(getApplicationContext(), DeviceList.class); + startActivityForResult(intent, BluetoothState.REQUEST_CONNECT_DEVICE); + } + } + }); + } + + public void onDestroy() { + super.onDestroy(); + bt.stopService(); + } + + 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 setup() { + Button btnSend = (Button)findViewById(R.id.btnSend); + btnSend.setOnClickListener(new OnClickListener(){ + public void onClick(View v){ + bt.send("1", true); + } + }); + } + + public void onActivityResult(int requestCode, int resultCode, Intent data) { + if(requestCode == BluetoothState.REQUEST_CONNECT_DEVICE) { + if(resultCode == Activity.RESULT_OK) + bt.connect(data); + } else if(requestCode == BluetoothState.REQUEST_ENABLE_BT) { + if(resultCode == Activity.RESULT_OK) { + bt.setupService(); + bt.startService(BluetoothState.DEVICE_OTHER); + setup(); + } else { + Toast.makeText(getApplicationContext() + , "Bluetooth was not enabled." + , Toast.LENGTH_SHORT).show(); + finish(); + } + } + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_simple.xml b/app/src/main/res/layout/activity_simple.xml new file mode 100644 index 0000000..6cc3330 --- /dev/null +++ b/app/src/main/res/layout/activity_simple.xml @@ -0,0 +1,38 @@ + + + + +