Advertisement
Guest User

Main

a guest
Dec 17th, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.50 KB | None | 0 0
  1. package com.receive.bluereceive;
  2.  
  3. import android.app.ActionBar;
  4. import android.app.AlertDialog;
  5. import android.app.FragmentTransaction;
  6. import android.bluetooth.BluetoothAdapter;
  7. import android.content.DialogInterface;
  8. import android.content.DialogInterface.OnClickListener;
  9. import android.content.Intent;
  10. import android.os.Bundle;
  11. import android.os.CountDownTimer;
  12. import android.support.v4.app.Fragment;
  13. import android.support.v4.app.FragmentManager;
  14. import android.support.v4.app.FragmentPagerAdapter;
  15. import android.support.v4.view.ViewPager;
  16. import android.util.Log;
  17. import android.view.LayoutInflater;
  18. import android.view.Menu;
  19. import android.view.MenuInflater;
  20. import android.view.MenuItem;
  21. import android.view.View;
  22. import android.view.ViewGroup;
  23. import android.widget.Button;
  24. import android.widget.Toast;
  25.  
  26.  
  27. public class Main extends android.support.v4.app.FragmentActivity implements ActionBar.TabListener {
  28.    
  29.     // Name of the connected device
  30.     private String mConnectedDeviceName = null;
  31.     /**
  32.      * Default Bluetooth adapter on the device.
  33.      */
  34.     private final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  35.    
  36.     // Member object for Bluetooth Command Service
  37.     //private BluetoothCommandService mCommandService = null;
  38.    
  39.     /**
  40.      * Magic number used to request connection of the devices
  41.      */
  42.     private static final int REQUEST_CONNECT_DEVICE = 1;
  43.    
  44.     public final int BT_ENABLE_TIME = 35;
  45.    
  46.     public final int BT_TIME_BTTIME = 1000 * BT_ENABLE_TIME;
  47.    
  48.     public CountDownTimer BTCountDown;
  49.    
  50.     public final int REQUEST_ENABLE_BT = 2;
  51.    
  52.     private final static String PREFIX = "BT_";
  53.    
  54.     private final String TAG = Main.class.getName();
  55.    
  56.     AppSectionsPagerAdapter mAppSectionsPagerAdapter;
  57.    
  58.     ViewPager mViewPager;
  59.    
  60.     @Override
  61.     public void onCreate(Bundle savedInstanceState) {
  62.         super.onCreate(savedInstanceState);
  63.         setContentView(R.layout.main_activity);
  64.         final ActionBar actionBar = getActionBar();
  65.         actionBar.setHomeButtonEnabled(false);
  66.         actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  67.        
  68.         if(mBluetoothAdapter == null) {
  69.             Log.e(TAG, "No Bluetooth Adapter available. Exiting...");
  70.             this.finish();
  71.         }
  72.        
  73.         mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());        
  74.    
  75.         mViewPager = (ViewPager) findViewById(R.id.pager);
  76.         mViewPager.setAdapter(mAppSectionsPagerAdapter);
  77.         mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
  78.             @Override
  79.             public void onPageSelected(int position) {
  80.                 // When swiping between different app sections, select the corresponding tab.
  81.                 // We can also use ActionBar.Tab#select() to do this if we have a reference to the
  82.                 // Tab.
  83.                 actionBar.setSelectedNavigationItem(position);
  84.             }
  85.         });
  86.        
  87.         for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
  88.             // Create a tab with text corresponding to the page title defined by the adapter.
  89.             // Also specify this Activity object, which implements the TabListener interface, as the
  90.             // listener for when this tab is selected.
  91.             actionBar.addTab(
  92.                     actionBar.newTab()
  93.                             .setText(mAppSectionsPagerAdapter.getPageTitle(i))
  94.                             .setTabListener(this));
  95.         }
  96.        
  97.         // If the adapter is null, then Bluetooth is not supported
  98.         if (mBluetoothAdapter == null) {
  99.             Toast.makeText(this, "Bluetooth is not available", Toast.LENGTH_LONG).show();
  100.             finish();
  101.             return;
  102.         }
  103.     }
  104.    
  105.     protected void onStart() {
  106.         super.onStart();
  107.         if (!mBluetoothAdapter.isEnabled()) {
  108.             Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
  109.             startActivity(enableIntent);
  110.             ((Button) findViewById(R.id.bt_server_start)).setEnabled(false);
  111.             ((Button) findViewById(R.id.bt_server_stop)).setEnabled(false);
  112.         }
  113.     }
  114.    
  115.     protected void onPause() {
  116.         super.onPause();
  117.         ((Button) findViewById(R.id.bt_server_start)).setEnabled(false);
  118.         ((Button) findViewById(R.id.bt_server_stop)).setEnabled(false);
  119.     }
  120.    
  121.     @Override
  122.     public boolean onCreateOptionsMenu(Menu menu) {
  123.         // Inflate the menu items for use in the action bar
  124.         MenuInflater inflater = getMenuInflater();
  125.         inflater.inflate(R.menu.option_menu, menu);
  126.        
  127.        
  128.         return super.onCreateOptionsMenu(menu);
  129.     }
  130.    
  131.     public boolean onOptionsItemSelected(MenuItem item) {
  132.         switch (item.getItemId()) {
  133.         case R.id.scan:
  134.             // Launch the DeviceListActivity to see devices and do scan
  135.             Intent serverIntent = new Intent(this, DeviceList.class);
  136.             startActivity(serverIntent);
  137.             return true;
  138.         case R.id.discoverable:
  139.             if (!mBluetoothAdapter.getName().startsWith(PREFIX))
  140.                 mBluetoothAdapter.setName(PREFIX + mBluetoothAdapter.getName());
  141.             if(mBluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
  142.                 BTDiscoverable();
  143.             } else if (mBluetoothAdapter.getScanMode() == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE)
  144.                 ((Button) findViewById(R.id.bt_server_start)).setEnabled(true);
  145.             return true;
  146.         }
  147.         return false;
  148.     }
  149.    
  150.     @Override
  151.     protected void onActivityResult(int requestCode, int resultCode, Intent data)
  152.     {
  153.         if (requestCode == REQUEST_ENABLE_BT)
  154.         {
  155.             if(resultCode != RESULT_CANCELED)
  156.             {
  157.                 ((Button) findViewById(R.id.bt_server_start)).setEnabled(true);
  158.                 BTCountDown = new BTTimer(BT_TIME_BTTIME,BT_TIME_BTTIME);
  159.                 BTCountDown.start();
  160.             }
  161.         }
  162.     }
  163.    
  164.     @Override
  165.     public void onBackPressed() {
  166.         new AlertDialog.Builder(this)
  167.             .setTitle("Really Exit?")
  168.             .setMessage("Are you sure you want to exit?")
  169.             .setNegativeButton(android.R.string.no, null)
  170.             .setPositiveButton(android.R.string.yes, new OnClickListener() {
  171.  
  172.                 public void onClick(DialogInterface arg0, int arg1) {
  173.                     Main.super.onBackPressed();
  174.                 }
  175.             }).create().show();
  176.     }
  177.    
  178.     public void BTDiscoverable() {
  179.         Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
  180.         discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, BT_ENABLE_TIME);
  181.         startActivityForResult(discoverableIntent, REQUEST_ENABLE_BT);
  182.     }
  183.    
  184.      @Override
  185.     public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
  186.         }
  187.  
  188.         @Override
  189.     public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
  190.             // When the given tab is selected, switch to the corresponding page in the ViewPager.
  191.             mViewPager.setCurrentItem(tab.getPosition());
  192.         }
  193.  
  194.         @Override
  195.     public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
  196.         }
  197.    
  198.      public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {
  199.  
  200.             public AppSectionsPagerAdapter(FragmentManager fm) {
  201.                 super(fm);
  202.             }
  203.  
  204.             @Override
  205.             public Fragment getItem(int i) {
  206.                 switch (i) {
  207.                 case 0:
  208.                     // The first section of the app is the most interesting -- it offers
  209.                     // a launchpad into the other demonstrations in this example application.
  210.                     return new MainSectionFragment();
  211.                    
  212.                 case 1:
  213.                     // The first section of the app is the most interesting -- it offers
  214.                     // a launchpad into the other demonstrations in this example application.
  215.                     return new ReceiveSectionFragment();
  216.  
  217.                 case 2:
  218.                     // The first section of the app is the most interesting -- it offers
  219.                     // a launchpad into the other demonstrations in this example application.
  220.                     return new SendSectionFragment();
  221.                    
  222.                 default:
  223.                     // The other sections of the app are dummy placeholders.
  224.                     return new MainSectionFragment();
  225.                 }
  226.             }
  227.  
  228.             @Override
  229.             public int getCount() {
  230.                 return 3;
  231.             }
  232.  
  233.             @Override
  234.             public CharSequence getPageTitle(int position) {
  235.                 int pos = position;
  236.                 switch (pos) {
  237.                 case 0:
  238.                     return "Main";
  239.                 case 1:
  240.                     return "Receive";
  241.                 case 2:
  242.                     return "Send";
  243.                 default:
  244.                     return "Section";
  245.                 }
  246.                    
  247.             }
  248.         }
  249.      
  250.      public static class MainSectionFragment extends Fragment {
  251.  
  252.             @Override
  253.             public View onCreateView(LayoutInflater inflater, ViewGroup container,
  254.                     Bundle savedInstanceState) {
  255.                 View rootView = inflater.inflate(R.layout.main_activity, container, false);
  256.                 return rootView;
  257.             }
  258.         }
  259.      
  260.      public static class ReceiveSectionFragment extends Fragment {
  261.  
  262.             @Override
  263.             public View onCreateView(LayoutInflater inflater, ViewGroup container,
  264.                     Bundle savedInstanceState) {
  265.                 View rootView = inflater.inflate(R.layout.receive, container, false);
  266.  
  267.                 rootView.findViewById(R.id.bt_server_start)
  268.                 .setOnClickListener(new View.OnClickListener() {
  269.                     @Override
  270.                     public void onClick(View view) {
  271.                         Intent intent = new Intent(getActivity(), Receive.class);
  272.                         startActivity(intent);
  273.                     }
  274.                 });
  275.                
  276.                 return rootView;
  277.             }
  278.         }
  279.      
  280.      public static class SendSectionFragment extends Fragment {
  281.  
  282.             @Override
  283.             public View onCreateView(LayoutInflater inflater, ViewGroup container,
  284.                     Bundle savedInstanceState) {
  285.                 View rootView = inflater.inflate(R.layout.send, container, false);
  286.                 return rootView;
  287.             }
  288.         }
  289.      
  290.      
  291.      public class BTTimer extends CountDownTimer {
  292.           public BTTimer (long startTime, long interval) {
  293.            super(startTime, interval);
  294.           }
  295.  
  296.           @Override
  297.           public void onFinish() {
  298.               ((Button) findViewById(R.id.bt_server_start)).setEnabled(false);
  299.               ((Button) findViewById(R.id.bt_server_stop)).setEnabled(false);
  300.           }
  301.  
  302.           @Override
  303.           public void onTick(long millisUntilFinished) {
  304.              
  305.           }
  306.     }
  307.    
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement