Manifest Permissions
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /><uses-permission android:name="android.permission.BLUETOOTH" />
Main activity
import android.app.Activity;import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private BluetoothAdapter mBluetoothAdapter = null;
private static final int REQUEST_ENABLE_BT = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
//register BroadcastReceiver here
registerReceiver(new BTStateChangedBroadcastReceiver(),
new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onStart() {
super.onStart();
// If BT is not on, request that it be enabled.
// setupChat() will then be called during onActivityResult
if (!mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
// Otherwise, setup the chat session
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_ENABLE_BT:
// When the request to enable Bluetooth returns
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(this, R.string.bt_enabled, Toast.LENGTH_SHORT).show();
} else {
// User did not enable Bluetooth or an error occurred
Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show();
finish();
}
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
BroadCast Receiver :
This is needed in order to monitor status
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,
context.getResources().getString(R.string.bluetooth_alert),
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;
}
}
}
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,
context.getResources().getString(R.string.bluetooth_alert),
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;
}
}
}
No comments:
Post a Comment