Advertisement
arnarg

Bluemote

Jan 19th, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.47 KB | None | 0 0
  1. package com.granra.bluemote;
  2.  
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.bluetooth.BluetoothAdapter;
  6. import android.content.Intent;
  7. import android.support.v4.app.Fragment;
  8. import android.view.Menu;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.TextView;
  12.  
  13. public class MainActivity extends Activity {
  14.     Fragment fragment;
  15.     int REQUEST_ENABLE_BT = 1;
  16.     BluetoothAdapter btAdapter;
  17.     TextView tvMessage;
  18.     Button bSettings, bEnableBT;
  19.  
  20.     @Override
  21.     protected void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.activity_main);
  24.         initialize();
  25.         checkBluetooth();
  26.  
  27.     }
  28.  
  29.     private void initialize() {
  30.         // TODO Auto-generated method stub
  31.         tvMessage = (TextView) findViewById(R.id.tvMessage);
  32.         bSettings = (Button) findViewById(R.id.bSettings);
  33.         bEnableBT = (Button) findViewById(R.id.bEnableBT);
  34.         bSettings.setVisibility(View.GONE);
  35.         bEnableBT.setVisibility(View.GONE);
  36.         bSettings.setOnClickListener(btnOnClickListener);
  37.         bEnableBT.setOnClickListener(btnOnClickListener);
  38.         btAdapter = BluetoothAdapter.getDefaultAdapter();
  39.     }
  40.  
  41.     private void checkBluetooth() {
  42.         // TODO Auto-generated method stub
  43.         if (btAdapter == null) {
  44.             tvMessage.setText("No Bluetooth adapter available");
  45.         } else {
  46.             if (!btAdapter.isEnabled()) {
  47.                 tvMessage.setText("Bluetooth is not enabled");
  48.                 bEnableBT.setVisibility(View.VISIBLE);
  49.             } else {
  50.                 connectBluetooth();
  51.             }
  52.         }
  53.     }
  54.  
  55.     private void connectBluetooth() {
  56.         // TODO Auto-generated method stub
  57.        
  58.     }
  59.  
  60.     @Override
  61.     public boolean onCreateOptionsMenu(Menu menu) {
  62.         // Inflate the menu; this adds items to the action bar if it is present.
  63.         getMenuInflater().inflate(R.menu.activity_main, menu);
  64.         return true;
  65.     }
  66.  
  67.     Button.OnClickListener btnOnClickListener = new Button.OnClickListener() {
  68.         public void onClick(View v) {
  69.             if (v == bEnableBT) {
  70.                 Intent enableBtIntent = new Intent(
  71.                         BluetoothAdapter.ACTION_REQUEST_ENABLE);
  72.                 startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
  73.             }
  74.         }
  75.     };
  76.  
  77.     @Override
  78.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  79.         // TODO Auto-generated method stub
  80.         super.onActivityResult(requestCode, resultCode, data);
  81.         if (resultCode == RESULT_OK) {
  82.             connectBluetooth();
  83.         }
  84.     }
  85.  
  86.     @Override
  87.     protected void onResume() {
  88.         // TODO Auto-generated method stub
  89.         super.onResume();
  90.         checkBluetooth();
  91.     }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement