I developed an app with android studio, I am leaving it below. I am also leaving all the code I used you in case you want to make any change.

In case you want to program it by yourself, remember you must have a background image.

If you download the app, you won’t have to do nothing like this 🙂

Graphics:



android:width="4dp"
android:color="#FFF" />






android:width="4dp"
android:color="#000" />






android:width="4dp"
android:color="#649D50 " />






android:width="4dp"
android:color="#9F6C6C" />



/>


android:width="1dp"
android:color="#000" />





android:width="5dp"
android:color="#C11814" />


android:bottom="20dp"
android:left="20dp"
android:right="20dp"
android:top="20dp" />




android:width="4dp"
android:color="#58A8A1" />



/>

xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity"
android:background="#CC85D5FB"
>

android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="164dp"
android:layout_above="@+id/restart"
android:layout_below="@+id/text"
android:layout_marginTop="30dp"
android:layout_marginBottom="14dp"
android:gravity="center"
android:layout_margin="10dp"
android:background="@drawable/listview"
android:divider="@android:color/black"
android:dividerHeight="1dp"
android:fontFamily="casual"
/>

android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="83dp"
android:layout_below="@+id/btsituation"
android:layout_alignParentEnd="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="20dp"
android:background="@drawable/welcome"
android:fontFamily="casual"
android:gravity="center"
android:textColor="#FFF"
android:text="WELCOME"
android:textSize="50dp"
android:textStyle="bold" />

Android Manifest:


package="com.anonimoconhache.dontloseme">

android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">











Main Activity:

package com.anonimoconhache.dontloseme;

import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.ListActivity;

import android.app.PendingIntent;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.nfc.Tag;
import android.os.AsyncTask;
import android.os.Bundle;

import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;

import android.widget.ListView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

import java.util.Set;
import java.util.UUID;

public class MainActivity extends ListActivity{
private ArrayAdapter mArrayAdapter;
private BluetoothAdapter mBluetoothAdapter;
private BluetoothSocket btSocket;
private ArrayList btDeviceArray = new ArrayList();
private ConnectAsyncTask connectAsyncTask;
private BluetoothAdapter mBTAdapter;
private Context thiscontext=this;
private MediaPlayer mediaPlayer;
private Button restar;

static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

TextView text, btsituation, title;
Intent HabilitarBT;
int requestCodeBT;
int stop; // variable
int music;// variable
int musicon;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { // IF : PEDIR PERMISO DE BT
if(requestCode==requestCodeBT){
if(resultCode==RESULT_OK){ //SI SE ACEPTA BT
Toast.makeText(thiscontext, "BT habilitado", Toast.LENGTH_SHORT).show();
reset();
}
else{ // SI NO SE ACEPTA BT
Toast.makeText(this, "Habilita bluetooth para abrir la app", Toast.LENGTH_SHORT).show();
finish();
}
}
}

@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mBTAdapter = BluetoothAdapter.getDefaultAdapter();
mArrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1);
setListAdapter(mArrayAdapter);
connectAsyncTask = new ConnectAsyncTask();
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
requestCodeBT=1;
music=0;
stop=0;
musicon=0;

text = (TextView)findViewById(R.id.text);
restar = (Button)findViewById(R.id.restart);
btsituation= (TextView) findViewById(R.id.btsituation);
title=(TextView)findViewById(R.id.title);

bluetoothrequest();
play();

sto();

Set pariedDevices = mBluetoothAdapter.getBondedDevices();
if (pariedDevices.size() > 0) { // SI SE ENCUENTRAN DISPOSITIVOS BT
for (BluetoothDevice device : pariedDevices) {
mArrayAdapter.add(device.getName() + "n" + device.getAddress());
btDeviceArray.add(device);
}
}
}
private void sto() { // SI SE PULSA STOP
restar.setOnClickListener(new View.OnClickListener() { //SI PULSO STOP
@Override
public void onClick(View view) { //SI PULSAN STOP
if(stop==1) {
mediaPlayer.stop();
finish();
startActivity(getIntent());
}
else if (stop==0){
Toast.makeText(thiscontext, "No music on", Toast.LENGTH_SHORT).show();

}
}
});
}

public void stomusic(){
mediaPlayer.stop();
}

private void bluetoothrequest() {
if (mBluetoothAdapter == null) { //SI NO HAY ADAPTADOR BT
Toast.makeText(this, "Tu dispositivo no acepta bluetooth", Toast.LENGTH_SHORT).show();
btsituation.setText("Tu dispositivo no acepta bluetooth");
btsituation.setTextColor(Color.RED);
finish();
} else {
if (!mBluetoothAdapter.isEnabled()) { //SI NO ESTA BT HABILITADO
HabilitarBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(HabilitarBT, requestCodeBT);
btsituation.setText("BT DISABLE");
btsituation.setTextColor(Color.BLACK);
btsituation.setBackgroundResource(R.drawable.bt_enable);
} else { // SI SE ACEPTA
btsituation.setText("BT ENABLE");
btsituation.setTextColor(Color.WHITE);
btsituation.setBackgroundResource(R.drawable.bt_disable);
}
}
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) { // SI SE CLICA EN ALGUN ELEMENTO DE LA LISTA
if(music==0){
if(mBluetoothAdapter.isEnabled()){ //SI ESTÁ ACTIVADO BT
BluetoothDevice device = btDeviceArray.get(position);
connectAsyncTask.execute(device);
}
else {
bluetoothrequest();
}
}
else if(music==1){ // SI NO ESTÁ ACTIVADO BT
Toast.makeText(this, "Pulsa STOP", Toast.LENGTH_SHORT).show();
}
}

private class ConnectAsyncTask extends AsyncTask{ // CONECTARSE BT
private BluetoothSocket mmSocket;
private BluetoothDevice mmDevice;
@SuppressLint("WrongThread")
@Override
protected BluetoothSocket doInBackground(BluetoothDevice... device) {
mmDevice = device[0];
try {
String mmUUID = "00001101-0000-1000-8000-00805F9B34FB";
mmSocket = mmDevice.createInsecureRfcommSocketToServiceRecord(UUID.fromString(mmUUID));
try {
mmSocket.connect();
text.setText("CONECTED");
text.setTextColor(Color.parseColor("#FFFFFF"));
text.setBackgroundResource(R.drawable.conected);
stomusic();
}
catch (Exception e){ }
}
catch (Exception e) {}
return mmSocket;
}

@Override
protected void onPostExecute(BluetoothSocket result) {
btSocket = result;
OutputStream mmOutStream = null;
try {
if (btSocket.isConnected()) { //MIENTRAS ESTE CONECTADO A ALGUN DISPOSITIVO BT
while (btSocket.isConnected()) {
mmOutStream = btSocket.getOutputStream();
mmOutStream.write(new String("1").getBytes()); //MANDAR DATO
music();
}
}
}
catch (IOException e) { // CUANDO NO ESTE CONECTADO A UN DISPOSITIVO BT
Toast.makeText(getBaseContext(), "Do not lose me", Toast.LENGTH_LONG).show();
text.setText("DISCONECTED");
text.setTextColor(Color.parseColor("#FFFFFF"));
text.setBackgroundResource(R.drawable.disconected);
play();

}
}
}

private void reset() {
stomusic();
finish();
startActivity(getIntent());

}
public void play(){ // SONIDO EN LOOP
mediaPlayer= MediaPlayer.create(this, R.raw.song);
mediaPlayer.setLooping(true);
mediaPlayer.start();
}
public void musicasonando(){
musicon=1;

}
public void music(){
music=1;
stop=1;

}
}

Leave a Reply