Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #7

Merged
merged 2 commits into from
Feb 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,6 @@

public class BluetoothActivity extends Activity {

private final static UUID uuid = UUID.fromString("fc5ffc49-00e3-4c8b-9cf1-6b72aad1001a");
// Create a BroadcastReceiver for ACTION_FOUND
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
// Whenever a remote Bluetooth device is found
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a ListView
adapter.add(bluetoothDevice.getName() + "\n"
+ bluetoothDevice.getAddress());
}
}
};
private BluetoothAdapter bluetoothAdapter;
private ToggleButton toggleButton;
private ListView listview;
Expand All @@ -49,6 +34,18 @@ public void onReceive(Context context, Intent intent) {
public static BluetoothDevice mBluetoothDevice = null;
public static BluetoothSocket mBluetoothSocket = null;


private final static UUID uuid = UUID.fromString("fc5ffc49-00e3-4c8b-9cf1-6b72aad1001a");
private final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
adapter.add(bluetoothDevice.getName() + "\n" + bluetoothDevice.getAddress());
}
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -57,132 +54,94 @@ protected void onCreate(Bundle savedInstanceState) {
toggleButton = (ToggleButton) findViewById(R.id.toggleButton);

listview = (ListView) findViewById(R.id.listView);
// ListView Item Click Listener
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item value
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String itemValue = (String) listview.getItemAtPosition(position);

String MAC = itemValue.substring(itemValue.length() - 17);

BluetoothDevice bluetoothDevice = bluetoothAdapter.getRemoteDevice(MAC);

// Initiate a connection request in a separate thread
ConnectingThread t = new ConnectingThread(bluetoothDevice);
t.start();
}
});

adapter = new ArrayAdapter
(this,android.R.layout.simple_list_item_1);
adapter = new ArrayAdapter (this,android.R.layout.simple_list_item_1);
listview.setAdapter(adapter);

bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}

public void onToggleClicked(View view) {

adapter.clear();

ToggleButton toggleButton = (ToggleButton) view;

if (bluetoothAdapter == null) {
// Device does not support Bluetooth
Toast.makeText(getApplicationContext(), "Oop! Your device does not support Bluetooth",
Toast.makeText(getApplicationContext(), "Oops! Your device does not support Bluetooth",
Toast.LENGTH_SHORT).show();
toggleButton.setChecked(false);
} else {

if (toggleButton.isChecked()){ // to turn on bluetooth
if (!bluetoothAdapter.isEnabled()) {
// A dialog will appear requesting user permission to enable Bluetooth
Intent enableBluetoothIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetoothIntent, ENABLE_BT_REQUEST_CODE);
} else {
Toast.makeText(getApplicationContext(), "Your device has already been enabled." +
"\n" + "Scanning for remote Bluetooth devices...",
Toast.LENGTH_SHORT).show();
// To discover remote Bluetooth devices
discoverDevices();
// Make local device discoverable by other devices
makeDiscoverable();
}
} else { // Turn off bluetooth

}
else{
toggleButton.setChecked(!bluetoothAdapter.isEnabled());
if (bluetoothAdapter.isEnabled()) {
bluetoothAdapter.disable();
adapter.clear();
Toast.makeText(getApplicationContext(), "Your device is now disabled.",
Toast.LENGTH_SHORT).show();
Toast.makeText(BluetoothActivity.this, "Bluetooth disabled", Toast.LENGTH_SHORT).show();
}
else{
///bluetoothAdapter.enable(); //This does not ask for user to permit connection
Intent enableBluetoothIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBluetoothIntent, ENABLE_BT_REQUEST_CODE);
Toast.makeText(BluetoothActivity.this, "Bluetooth Enabled", Toast.LENGTH_SHORT).show();
}
}
}

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

if (requestCode == ENABLE_BT_REQUEST_CODE) {

// Bluetooth successfully enabled!
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(getApplicationContext(), "Ha! Bluetooth is now enabled." +
"\n" + "Scanning for remote Bluetooth devices...",
Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "Bluetooth enabled." + "\n" + "Scanning for peers", Toast.LENGTH_SHORT).show();

// To discover remote Bluetooth devices
discoverDevices();

// Make local device discoverable by other devices
makeDiscoverable();

// Start a thread to create a server socket to listen
// for connection request
ListeningThread t = new ListeningThread();
t.start();

} else { // RESULT_CANCELED as user refused or failed to enable Bluetooth
Toast.makeText(getApplicationContext(), "Bluetooth is not enabled.",
Toast.LENGTH_SHORT).show();

// Turn off togglebutton
}
else {
Toast.makeText(getApplicationContext(), "Bluetooth is not enabled.", Toast.LENGTH_SHORT).show();
toggleButton.setChecked(false);
}
} else if (requestCode == DISCOVERABLE_BT_REQUEST_CODE){

}
else if (requestCode == DISCOVERABLE_BT_REQUEST_CODE){
if (resultCode == DISCOVERABLE_DURATION){
Toast.makeText(getApplicationContext(), "Your device is now discoverable by other devices for " +
DISCOVERABLE_DURATION + " seconds",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Fail to enable discoverability on your device.",
Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "Your device is now discoverable for " + DISCOVERABLE_DURATION + " seconds", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getApplicationContext(), "Fail to enable discoverable mode.", Toast.LENGTH_SHORT).show();
}
}
}

protected void discoverDevices(){
// To scan for remote Bluetooth devices
if (bluetoothAdapter.startDiscovery()) {
Toast.makeText(getApplicationContext(), "Discovering other bluetooth devices...",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Discovery failed to start.",
Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "Discovering peers", Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getApplicationContext(), "Discovery failed to start.", Toast.LENGTH_SHORT).show();
}
}

protected void makeDiscoverable(){
// Make local device discoverable
Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, DISCOVERABLE_DURATION);
startActivityForResult(discoverableIntent, DISCOVERABLE_BT_REQUEST_CODE);
}

@Override
protected void onResume() {
super.onResume();
// Register the BroadcastReceiver for ACTION_FOUND
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
this.registerReceiver(broadcastReceiver, filter);
}
Expand All @@ -193,25 +152,6 @@ protected void onPause() {
this.unregisterReceiver(broadcastReceiver);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.bluetooth, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

public synchronized void connected(BluetoothSocket socket, BluetoothDevice device) {

mBluetoothDevice = device;
Expand All @@ -229,45 +169,43 @@ public ListeningThread() {
try {
temp = bluetoothAdapter.listenUsingRfcommWithServiceRecord(getString(R.string.app_name), uuid);

} catch (IOException e) {
}
catch (IOException e) {
e.printStackTrace();
}
bluetoothServerSocket = temp;
}

public void run() {
BluetoothSocket bluetoothSocket;
// This will block while listening until a BluetoothSocket is returned
// or an exception occurs
while (true) {
try {
bluetoothSocket = bluetoothServerSocket.accept();
} catch (IOException e) {
}
catch (IOException e) {
break;
}
// If a connection is accepted
if (bluetoothSocket != null) {

runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), "A connection has been accepted.",
Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "A connection has been accepted.", Toast.LENGTH_SHORT).show();
}
});

connected(bluetoothSocket,bluetoothSocket.getRemoteDevice());

try {
bluetoothServerSocket.close();
} catch (IOException e) {
}
catch (IOException e) {
e.printStackTrace();
}
break;
}
}
}

// Cancel the listening socket and terminate the thread
public void cancel() {
try {
bluetoothServerSocket.close();
Expand All @@ -289,25 +227,25 @@ public ConnectingThread(BluetoothDevice device) {
// Get a BluetoothSocket to connect with the given BluetoothDevice
try {
temp = bluetoothDevice.createRfcommSocketToServiceRecord(uuid);
} catch (IOException e) {
}
catch (IOException e) {
e.printStackTrace();
}
bluetoothSocket = temp;
}

public void run() {
// Cancel discovery as it will slow down the connection
bluetoothAdapter.cancelDiscovery();

try {
// This will block until it succeeds in connecting to the device
// through the bluetoothSocket or throws an exception
bluetoothSocket.connect();
} catch (IOException connectException) {
}
catch (IOException connectException) {
connectException.printStackTrace();
try {
bluetoothSocket.close();
} catch (IOException closeException) {
}
catch (IOException closeException) {
closeException.printStackTrace();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public class CanvasViewDouble2P extends View { //you have to create a new java f
Context ctx;
float canvasSide,cellSide;
DatabaseHandler dbh;

boolean touchEnabled = true;

BluetoothSocket bluetoothSocket;
Expand Down Expand Up @@ -116,6 +115,7 @@ else if (a[row][col] == 2) {
}
}
}
check();
}

@Override
Expand Down Expand Up @@ -153,10 +153,10 @@ public boolean onTouchEvent(MotionEvent event) {
}
else{
try {
connectedThread=null;
bluetoothSocket.close();
connectedThread.stop();
} catch (Exception e) {
Log.d(TAG,"exception " + e.getMessage());
Log.e(TAG,"exception " + e.getMessage());
}
TwoDevice2P.act_2p.finish();
}
Expand All @@ -167,15 +167,16 @@ public boolean onTouchEvent(MotionEvent event) {
}

public void showAlert(String str){
touchEnabled = true;
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
{
try {
connectedThread=null;
bluetoothSocket.close();
connectedThread.stop();
} catch (Exception e) {
Log.d(TAG, "exception " + e.getMessage());
}
Expand Down Expand Up @@ -453,17 +454,14 @@ public void run() {
}
}

/**
* Write to the connected OutStream.
*
* @param buffer The bytes to write
*/
public void write(byte[] buffer) {
try {
Log.d(TAG, "Writing ");
mmOutStream.write(buffer);
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
if(!oncewin && !oncedrawen) {
try {
Log.d(TAG, "Writing ");
mmOutStream.write(buffer);
} catch (IOException e) {
Log.e(TAG, "Exception during write", e);
}
}
}

Expand Down