Question: Hello, I am building an app on Android Studio and I need help dealing with bluetooth. So the whole point of the app is to

Hello, I am building an app on Android Studio and I need help dealing with bluetooth. So the whole point of the app is to play an alarm once the app unpairs with a bluetooth device. Here is what I have now. The alarm is a button that plays music when pressed. I am thinking of having the app play the button once it unpairs with the bluetooth device automatically but i have no clue on how to approach this. Here is my MainActivity.java

package

import android.media.MediaPlayer; import android.widget.Button; import android.widget.TextView; import android.bluetooth.BluetoothAdapter; import java.util.ArrayList; import android.graphics.Color; import android.view.View; import android.app.Activity; import android.widget.Toast; import android.content.Context; import android.content.BroadcastReceiver; import android.content.DialogInterface; import android.os.Bundle; import android.bluetooth.BluetoothDevice; import android.app.ProgressDialog; import java.util.Set; import android.content.Intent; import android.content.IntentFilter; public class MainActivity extends Activity { private TextView mStatusTv; private Button mActivateBtn; private Button mPairedBtn; private Button mScanBtn; private ProgressDialog mProgressDlg; private ArrayList mDeviceList = new ArrayList(); private BluetoothAdapter mBluetoothAdapter; MediaPlayer mySound; // variable for the sound file in the raw folder @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // super is used to call the parents constructor (Inheritance) setContentView(R.layout.activity_main); mySound = MediaPlayer.create(this, R.raw.ringtone); // this creates the sound file called "ringtone" mStatusTv = (TextView) findViewById(R.id.tv_status); mActivateBtn = (Button) findViewById(R.id.btn_enable); mPairedBtn = (Button) findViewById(R.id.btn_view_paired); mScanBtn = (Button) findViewById(R.id.btn_scan); // these are the button variables created in the app mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); mProgressDlg = new ProgressDialog(this); mProgressDlg.setMessage("Scanning..."); // the progress button generates a scanning text field upon activation mProgressDlg.setCancelable(false); mProgressDlg.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); mBluetoothAdapter.cancelDiscovery(); } }); if (mBluetoothAdapter == null) { showUnsupported(); } else { mPairedBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Set pairedDevices = mBluetoothAdapter.getBondedDevices(); if (pairedDevices == null || pairedDevices.size() == 0) { showToast("No Paired Devices Found"); } else { ArrayList list = new ArrayList(); list.addAll(pairedDevices); Intent intent = new Intent(MainActivity.this, DeviceListActivity.class); intent.putParcelableArrayListExtra("device.list", list); startActivity(intent); } } }); mScanBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { mBluetoothAdapter.startDiscovery(); } }); mActivateBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mBluetoothAdapter.isEnabled()) { mBluetoothAdapter.disable(); showDisabled(); } else { Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(intent, 1000); } } }); if (mBluetoothAdapter.isEnabled()) { showEnabled(); } else { showDisabled(); } } IntentFilter filter = new IntentFilter(); filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED); filter.addAction(BluetoothDevice.ACTION_FOUND); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED); filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); registerReceiver(mReceiver, filter); } @Override public void onPause() { if (mBluetoothAdapter != null) { if (mBluetoothAdapter.isDiscovering()) { mBluetoothAdapter.cancelDiscovery(); } } super.onPause(); mySound.release(); // this super function is used to stop the sound once the application closes } @Override public void onDestroy() { unregisterReceiver(mReceiver); super.onDestroy(); } private void showEnabled() { mStatusTv.setText("Bluetooth is On"); // enables bluetooth within the smart phone mStatusTv.setTextColor(Color.BLUE); // text is blue to indicate bluetooth is active mActivateBtn.setText("Disable"); mActivateBtn.setEnabled(true); mPairedBtn.setEnabled(true); mScanBtn.setEnabled(true); } private void showDisabled() { mStatusTv.setText("Bluetooth is Off"); // turns off the bluetooth mStatusTv.setTextColor(Color.RED); // color is red to indicate bluetooth is off mActivateBtn.setText("Enable"); mActivateBtn.setEnabled(true); mPairedBtn.setEnabled(false); mScanBtn.setEnabled(false); } private void showUnsupported() { mStatusTv.setText("Bluetooth is unsupported by this device"); mActivateBtn.setText("Enable"); mActivateBtn.setEnabled(false); mPairedBtn.setEnabled(false); mScanBtn.setEnabled(false); } private void showToast(String message) { Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show(); } private final BroadcastReceiver mReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) { final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR); if (state == BluetoothAdapter.STATE_ON) { showToast("Enabled"); showEnabled(); } } else if (BluetoothAdapter.ACTION_DISCOVERY_STARTED.equals(action)) { mDeviceList = new ArrayList(); mProgressDlg.show(); } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) { mProgressDlg.dismiss(); Intent newIntent = new Intent(MainActivity.this, DeviceListActivity.class); newIntent.putParcelableArrayListExtra("device.list", mDeviceList); startActivity(newIntent); } else if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); mDeviceList.add(device); showToast("Found device " + device.getName()); } } // broadcast receiver is used to activate the bluetooth function and to find nearby devices. This is an universal function }; public void playMusic(View view) { mySound.start(); } // this function plays the sound button from the file . } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!