Bluetooth Part I — Enabling Bluetooth

Michael Xiong
Jan 12, 2018 · 4 min read
import android.bluetooth.BluetoothAdapter;
private BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
protected boolean setUpBT(){
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
Toast.makeText(this, "This device does not support Bluetooth", Toast.LENGTH_SHORT).show();
return false;
}
else if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
Toast.makeText(this, "Please enable Bluetooth NOW!!!!!", Toast.LENGTH_SHORT).show();
return false;
}
else{
Toast.makeText(this, "Yay", Toast.LENGTH_SHORT).show();
return true;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_ENABLE_BT){
Log.d("Working:", "Bluetooth enabled");
Toast.makeText(this, "Bluetooth enabled", Toast.LENGTH_SHORT).show();
}
}
if(!(mBluetoothAdapter.getScanMode() == BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE ) && setUpBT()) {
Intent discoverableIntent =
new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, REQUEST_ENABLE_DIS);
startActivityForResult(discoverableIntent, REQUEST_ENABLE_DIS);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == REQUEST_ENABLE_DIS){
Log.d("Working", "Made discoverable");
Toast.makeText(this, "Made discoverable", Toast.LENGTH_SHORT).show();
discoverable = true;
}
else if(resultCode == RESULT_CANCELED){
Toast.makeText(this, "Discoverability rejected", Toast.LENGTH_SHORT).show();
}
}
filter = new IntentFilter(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED);
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Working", "Broadcast received");
if(intent.getAction().equals(BluetoothAdapter.ACTION_SCAN_MODE_CHANGED)){
if(discoverable) {
Log.d("Working", "Connection session ended");
codeDisplay.setText("Your session to connect has timed out");
discoverable = false;
}
else{
Log.d("Working", "Connection session started");
}
}
// else if(intent.getAction().equals("ENABLE_DIS")){
// Toast.makeText(context, "broadcast received", Toast.LENGTH_SHORT).show();
// }
}
};
registerReceiver(broadcastReceiver, filter);
@Override
protected void onDestroy() {
if (broadcastReceiver != null) {
unregisterReceiver(broadcastReceiver);
broadcastReceiver = null;
}
super.onDestroy();
}

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade