Jun 22, 2014

Implement BroadcastReceiver to monitor Bluetooth state changed

Last example show how to Enable Bluetooth using Intent of BluetoothAdapter.ACTION_REQUEST_ENABLE. In this example, we implement a new class BTStateChangedBroadcastReceiver, extends BroadcastReceiver, to monitor Bluetooth state changed.


BTStateChangedBroadcastReceiver.java
package com.example.androidbluetooth;

import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class BTStateChangedBroadcastReceiver extends BroadcastReceiver {

 @Override
 public void onReceive(Context context, Intent intent) {
  int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 
    -1);
  
  switch(state){
  case BluetoothAdapter.STATE_CONNECTED:
   Toast.makeText(context, 
    "BTStateChangedBroadcastReceiver: STATE_CONNECTED", 
    Toast.LENGTH_SHORT).show();
   break;
  case BluetoothAdapter.STATE_CONNECTING:
   Toast.makeText(context, 
    "BTStateChangedBroadcastReceiver: STATE_CONNECTING", 
    Toast.LENGTH_SHORT).show();
   break;
  case BluetoothAdapter.STATE_DISCONNECTED:
   Toast.makeText(context, 
    "BTStateChangedBroadcastReceiver: STATE_DISCONNECTED", 
    Toast.LENGTH_SHORT).show();
   break;
  case BluetoothAdapter.STATE_DISCONNECTING:
   Toast.makeText(context, 
    "BTStateChangedBroadcastReceiver: STATE_DISCONNECTING", 
    Toast.LENGTH_SHORT).show();
   break;
  case BluetoothAdapter.STATE_OFF:
   Toast.makeText(context, 
    "BTStateChangedBroadcastReceiver: STATE_OFF", 
    Toast.LENGTH_SHORT).show();
   break;
  case BluetoothAdapter.STATE_ON:
   Toast.makeText(context, 
    "BTStateChangedBroadcastReceiver: STATE_ON", 
    Toast.LENGTH_SHORT).show();
   break;
  case BluetoothAdapter.STATE_TURNING_OFF:
   Toast.makeText(context, 
    "BTStateChangedBroadcastReceiver: STATE_TURNING_OFF", 
    Toast.LENGTH_SHORT).show();
   break;
  case BluetoothAdapter.STATE_TURNING_ON:
   Toast.makeText(context, 
    "BTStateChangedBroadcastReceiver: STATE_TURNING_ON", 
    Toast.LENGTH_SHORT).show();
   break; 
  }
 }

}

Call registerReceiver(...) to register new BTStateChangedBroadcastReceiver(), to the intent of BluetoothAdapter.ACTION_STATE_CHANGED.
package com.example.androidbluetooth;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
 
 TextView textInfo;
 Button buttonEnableBT;
 BluetoothAdapter bluetoothAdapter;
 
 final static int REQUEST_ENABLE_BT = 1;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  textInfo = (TextView)findViewById(R.id.info);
  buttonEnableBT = (Button)findViewById(R.id.enablebt);
  
  bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
  if (bluetoothAdapter == null) {
   textInfo.setText("BlueTooth not supported in this device");
   buttonEnableBT.setEnabled(false);
  }else{
   if (bluetoothAdapter.isEnabled()) {
    buttonEnableBT.setEnabled(false);
    textInfo.setText("BlueTooth enabled");
   }else{
    buttonEnableBT.setEnabled(true);
    textInfo.setText("BlueTooth disabled, click button to turn on BlueTooth.");
   }
   
   //register BroadcastReceiver
   registerReceiver(new BTStateChangedBroadcastReceiver(), 
    new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
  }
  
  buttonEnableBT.setOnClickListener(new OnClickListener(){

   @Override
   public void onClick(View v) {
    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
       startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
   }});

 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  
  if(requestCode == REQUEST_ENABLE_BT){
   if(resultCode==RESULT_OK){
    Toast.makeText(MainActivity.this, "BlueTooth Turned On", Toast.LENGTH_LONG).show();
   }else{
    Toast.makeText(MainActivity.this, "Cancelled", Toast.LENGTH_LONG).show();
   }
  }
  
  if (bluetoothAdapter.isEnabled()) {
   buttonEnableBT.setEnabled(false);
   textInfo.setText("BlueTooth enabled");
  }else{
   buttonEnableBT.setEnabled(true);
   textInfo.setText("BlueTooth disabled, click button to turn on BlueTooth.");
  }
  
 }

}

activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.androidbluetooth.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com" />
    <TextView
        android:id="@+id/info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/enablebt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Enable BlueTooth" />

</LinearLayout>

Permission of "android.permission.BLUETOOTH" is needed in AndroidManifest.xml.
<uses-permission android:name="android.permission.BLUETOOTH"/>


1 comment:

Infolinks In Text Ads