Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.receive.bluereceive;
- import android.app.ActionBar;
- import android.app.AlertDialog;
- import android.app.FragmentTransaction;
- import android.bluetooth.BluetoothAdapter;
- import android.content.DialogInterface;
- import android.content.DialogInterface.OnClickListener;
- import android.content.Intent;
- import android.os.Bundle;
- import android.os.CountDownTimer;
- import android.support.v4.app.Fragment;
- import android.support.v4.app.FragmentManager;
- import android.support.v4.app.FragmentPagerAdapter;
- import android.support.v4.view.ViewPager;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.Toast;
- public class Main extends android.support.v4.app.FragmentActivity implements ActionBar.TabListener {
- // Name of the connected device
- private String mConnectedDeviceName = null;
- /**
- * Default Bluetooth adapter on the device.
- */
- private final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- // Member object for Bluetooth Command Service
- //private BluetoothCommandService mCommandService = null;
- /**
- * Magic number used to request connection of the devices
- */
- private static final int REQUEST_CONNECT_DEVICE = 1;
- public final int BT_ENABLE_TIME = 35;
- public final int BT_TIME_BTTIME = 1000 * BT_ENABLE_TIME;
- public CountDownTimer BTCountDown;
- public final int REQUEST_ENABLE_BT = 2;
- private final static String PREFIX = "BT_";
- private final String TAG = Main.class.getName();
- AppSectionsPagerAdapter mAppSectionsPagerAdapter;
- ViewPager mViewPager;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main_activity);
- final ActionBar actionBar = getActionBar();
- actionBar.setHomeButtonEnabled(false);
- actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
- if(mBluetoothAdapter == null) {
- Log.e(TAG, "No Bluetooth Adapter available. Exiting...");
- this.finish();
- }
- mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
- mViewPager = (ViewPager) findViewById(R.id.pager);
- mViewPager.setAdapter(mAppSectionsPagerAdapter);
- mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
- @Override
- public void onPageSelected(int position) {
- // When swiping between different app sections, select the corresponding tab.
- // We can also use ActionBar.Tab#select() to do this if we have a reference to the
- // Tab.
- actionBar.setSelectedNavigationItem(position);
- }
- });
- for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
- // Create a tab with text corresponding to the page title defined by the adapter.
- // Also specify this Activity object, which implements the TabListener interface, as the
- // listener for when this tab is selected.
- actionBar.addTab(
- actionBar.newTab()
- .setText(mAppSectionsPagerAdapter.getPageTitle(i))
- .setTabListener(this));
- }
- // If the adapter is null, then Bluetooth is not supported
- if (mBluetoothAdapter == null) {
- Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
- finish();
- return;
- }
- }
- protected void onStart() {
- super.onStart();
- if (!mBluetoothAdapter.isEnabled()) {
- Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- startActivity(enableIntent);
- ((Button) findViewById(R.id.bt_server_start)).setEnabled(false);
- ((Button) findViewById(R.id.bt_server_stop)).setEnabled(false);
- }
- }
- protected void onPause() {
- super.onPause();
- ((Button) findViewById(R.id.bt_server_start)).setEnabled(false);
- ((Button) findViewById(R.id.bt_server_stop)).setEnabled(false);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu items for use in the action bar
- MenuInflater inflater = getMenuInflater();
- inflater.inflate(R.menu.option_menu, menu);
- return super.onCreateOptionsMenu(menu);
- }
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case R.id.scan:
- // Launch the DeviceListActivity to see devices and do scan
- Intent serverIntent = new Intent(this, DeviceList.class);
- startActivity(serverIntent);
- return true;
- case R.id.discoverable:
- if (!mBluetoothAdapter.getName().startsWith(PREFIX))
- mBluetoothAdapter.setName(PREFIX + mBluetoothAdapter.getName());
- if(mBluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
- BTDiscoverable();
- } else if (mBluetoothAdapter.getScanMode() == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE)
- ((Button) findViewById(R.id.bt_server_start)).setEnabled(true);
- return true;
- }
- return false;
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data)
- {
- if (requestCode == REQUEST_ENABLE_BT)
- {
- if(resultCode != RESULT_CANCELED)
- {
- ((Button) findViewById(R.id.bt_server_start)).setEnabled(true);
- BTCountDown = new BTTimer(BT_TIME_BTTIME,BT_TIME_BTTIME);
- BTCountDown.start();
- }
- }
- }
- @Override
- public void onBackPressed() {
- new AlertDialog.Builder(this)
- .setTitle("Really Exit?")
- .setMessage("Are you sure you want to exit?")
- .setNegativeButton(android.R.string.no, null)
- .setPositiveButton(android.R.string.yes, new OnClickListener() {
- public void onClick(DialogInterface arg0, int arg1) {
- Main.super.onBackPressed();
- }
- }).create().show();
- }
- public void BTDiscoverable() {
- Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
- discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, BT_ENABLE_TIME);
- startActivityForResult(discoverableIntent, REQUEST_ENABLE_BT);
- }
- @Override
- public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
- }
- @Override
- public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
- // When the given tab is selected, switch to the corresponding page in the ViewPager.
- mViewPager.setCurrentItem(tab.getPosition());
- }
- @Override
- public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
- }
- public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
- public AppSectionsPagerAdapter(FragmentManager fm) {
- super(fm);
- }
- @Override
- public Fragment getItem(int i) {
- switch (i) {
- case 0:
- // The first section of the app is the most interesting -- it offers
- // a launchpad into the other demonstrations in this example application.
- return new MainSectionFragment();
- case 1:
- // The first section of the app is the most interesting -- it offers
- // a launchpad into the other demonstrations in this example application.
- return new ReceiveSectionFragment();
- case 2:
- // The first section of the app is the most interesting -- it offers
- // a launchpad into the other demonstrations in this example application.
- return new SendSectionFragment();
- default:
- // The other sections of the app are dummy placeholders.
- return new MainSectionFragment();
- }
- }
- @Override
- public int getCount() {
- return 3;
- }
- @Override
- public CharSequence getPageTitle(int position) {
- int pos = position;
- switch (pos) {
- case 0:
- return "Main";
- case 1:
- return "Receive";
- case 2:
- return "Send";
- default:
- return "Section";
- }
- }
- }
- public static class MainSectionFragment extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View rootView = inflater.inflate(R.layout.main_activity, container, false);
- return rootView;
- }
- }
- public static class ReceiveSectionFragment extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View rootView = inflater.inflate(R.layout.receive, container, false);
- rootView.findViewById(R.id.bt_server_start)
- .setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent intent = new Intent(getActivity(), Receive.class);
- startActivity(intent);
- }
- });
- return rootView;
- }
- }
- public static class SendSectionFragment extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View rootView = inflater.inflate(R.layout.send, container, false);
- return rootView;
- }
- }
- public class BTTimer extends CountDownTimer {
- public BTTimer (long startTime, long interval) {
- super(startTime, interval);
- }
- @Override
- public void onFinish() {
- ((Button) findViewById(R.id.bt_server_start)).setEnabled(false);
- ((Button) findViewById(R.id.bt_server_stop)).setEnabled(false);
- }
- @Override
- public void onTick(long millisUntilFinished) {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement