Saturday, 30 April 2016

BLUETOOTH IN ANDROID

  1. package com.example.bluetooth;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.view.Menu;  
  5. import android.app.Activity;  
  6. import android.bluetooth.BluetoothAdapter;  
  7. import android.content.Context;  
  8. import android.content.Intent;  
  9. import android.os.Bundle;  
  10. import android.util.Log;  
  11. import android.view.View;  
  12. import android.widget.Button;  
  13. import android.widget.TextView;  
  14. import android.widget.Toast;  
  15.   
  16. public class MainActivity extends Activity {  
  17.       private static final int REQUEST_ENABLE_BT = 0;  
  18.       private static final int REQUEST_DISCOVERABLE_BT = 0;  
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  23.     final TextView out=(TextView)findViewById(R.id.out);  
  24.     final Button button1 = (Button) findViewById(R.id.button1);  
  25.     final Button button2 = (Button) findViewById(R.id.button2);  
  26.     final Button button3 = (Button) findViewById(R.id.button3);  
  27.     final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();  
  28.     if (mBluetoothAdapter == null) {  
  29.        out.append("device not supported");  
  30.     }  
  31.     button1.setOnClickListener(new View.OnClickListener() {  
  32.         public void onClick(View v) {  
  33.             if (!mBluetoothAdapter.isEnabled()) {  
  34.                 Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);  
  35.                 startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);  
  36.             }  
  37.         }  
  38.     });  
  39.     button2.setOnClickListener(new View.OnClickListener() {  
  40.      @Override  
  41.         public void onClick(View arg0) {  
  42.             if (!mBluetoothAdapter.isDiscovering()) {  
  43.                   //out.append("MAKING YOUR DEVICE DISCOVERABLE");  
  44.                    Toast.makeText(getApplicationContext(), "MAKING YOUR DEVICE DISCOVERABLE",  
  45.              Toast.LENGTH_LONG);  
  46.   
  47.                 Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);  
  48.                 startActivityForResult(enableBtIntent, REQUEST_DISCOVERABLE_BT);  
  49.                     
  50.             }  
  51.         }  
  52.     });  
  53.     button3.setOnClickListener(new View.OnClickListener() {  
  54.         @Override  
  55.         public void onClick(View arg0) {     
  56.             mBluetoothAdapter.disable();  
  57.             //out.append("TURN_OFF BLUETOOTH");  
  58.             Toast.makeText(getApplicationContext(), "TURNING_OFF BLUETOOTH", Toast.LENGTH_LONG);  
  59.            
  60.             }  
  61.     });  
  62. }  
  63.   
  64.     @Override  
  65.     public boolean onCreateOptionsMenu(Menu menu) {  
  66.         // Inflate the menu; this adds items to the action bar if it is present.  
  67.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  68.         return true;  
  69.     }  
  70.   

No comments:

Post a Comment

Earn money online through app sign up

Hey, I just transferred some money to a friend using Chillr. Its super quick and easy. Check it out. https://m.chillr.in/invite Hey! I...