Skip to content

Commit

Permalink
added more checking points ( if bluetooth is enabled before doing an…
Browse files Browse the repository at this point in the history
… action )
  • Loading branch information
georkost4 committed Jul 2, 2016
1 parent e1e628c commit 203ad89
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 50 deletions.
12 changes: 0 additions & 12 deletions BTproject/.idea/inspectionProfiles/Project_Default.xml

This file was deleted.

7 changes: 0 additions & 7 deletions BTproject/.idea/inspectionProfiles/profiles_settings.xml

This file was deleted.

20 changes: 1 addition & 19 deletions BTproject/.idea/misc.xml

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

Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ protected void onCreate(Bundle savedInstanceState) {
init();
registerReceivers();
fillPairedDevicesListView();


}

private void fillPairedDevicesListView() {
Expand Down Expand Up @@ -98,7 +96,19 @@ private void init()

}

private void btnSearchDevicesClicked(View v) { btAdapter.startDiscovery(); } // Start discovering servicce to find new device
private void btnSearchDevicesClicked(View v)
{
// Check if bluetooth is enabled
if (btAdapter.isEnabled()) {
// Start discovering servicce to find new device
btAdapter.startDiscovery();
}
else
{
// Display error message
Toast.makeText(getApplicationContext(),"Enable bluetooth to start searching",Toast.LENGTH_SHORT).show();
}
}


private void registerReceivers()
Expand Down Expand Up @@ -168,7 +178,6 @@ public void onReceive(Context context, Intent intent) {




private void devicesArrayAdapterClicked(AdapterView<?> parent, View view, int position, long id)
{
Log.d(TAG,"deviceClicked");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,8 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);

initialize();
setUpBluetooth();




setUpBluetooth();
Log.e(TAG,"----On Create------");
}

Expand All @@ -65,9 +62,8 @@ private void startListeningForConnectionsAsServer() {
bluetoothAsServer.start();
}

private void setUpBluetooth()
public void setUpBluetooth()
{
btAdapter = BluetoothAdapter.getDefaultAdapter();

if(btAdapter != null)
{
Expand All @@ -83,10 +79,11 @@ private void setUpBluetooth()

private void initialize() {
setTitle("Main Panel");
btAdapter = BluetoothAdapter.getDefaultAdapter();
btnFindDevices = (Button) findViewById(R.id.btnFind);
btnFindDevices.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { startActivityForResult(new Intent(MainActivity.this, FindDevice.class),Constants.FIND_DEVICE_REQUEST); }
public void onClick(View v) { btnFindDevicesClicked(new Intent(MainActivity.this, FindDevice.class), Constants.FIND_DEVICE_REQUEST);}
});
btnSendData = (Button) findViewById(R.id.btnSend);
btnSendData.setOnClickListener(new View.OnClickListener() {
Expand Down Expand Up @@ -114,6 +111,18 @@ public void onClick(View v) {
myHandler = new chatMessageHandler(MainActivity.this);
}

private void btnFindDevicesClicked(Intent intent, int findDeviceRequest)
{
// Check for bluetooth if enabled before continuing
if (btAdapter.isEnabled()) startActivityForResult(intent, findDeviceRequest);
// Else display error until bluetooth is enabled
else
{
Toast.makeText(getApplicationContext(), "Enable Bluetooth to continue", Toast.LENGTH_SHORT).show();
setUpBluetooth();
}
}

public synchronized void doUpdateChatListView(Message msg)
{
// Get the bundle with
Expand Down Expand Up @@ -149,7 +158,6 @@ private void btnCheckConnectivityClicked()
}
case Constants.CONNECTION_ESTABLISHED_AS_SERVER:
{

String str = String.valueOf(BluetoothAsServer.getSocket().isConnected());
Toast.makeText(this, str, Toast.LENGTH_LONG).show();
break;
Expand Down Expand Up @@ -199,6 +207,11 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG,"Bluetooth enabled");
startListeningForConnectionsAsServer();
}
else
{
//Show error message and exit the application
Toast.makeText(getApplicationContext(),"You must enable bluetooth to continue",Toast.LENGTH_SHORT).show();
}
break;
case Constants.REQUEST_DISCOVERABILITY_BT:
if(resultCode == RESULT_OK) Log.d(TAG,"Bluetooth discoverable");
Expand Down

0 comments on commit 203ad89

Please sign in to comment.