HC-05 and Bluetooth module and android connectivity error

I am new to arduino ,I have arduino mega and bluetooth module HC-05 and i am trying to open and closed LED ligth from android phone.Android phone connects with the bluetooth module and LED is controlled but after almost few seconds it is disconnected.Can some one guide me please what is the reason that it is disconnected after some time and than i have to restart the bluetooth module.

Inadequate power, maybe?

Don't see much code.........

This is arduino code

char command;
String string;
boolean ledon = false;
#define led 13

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(led,OUTPUT);
}

void loop() {//start loop
// put your main code here, to run repeatedly:

if (Serial.available()>0)
{
string = "" ;
}

while(Serial.available()>0){//start while

command = ((byte) Serial.read());

if (command == ':'){
break;
}
else{
string += command;
}

delay(1);
}//end while

if (string == "TO"){
ledOn();
ledon=true;
Serial.println(string);
}

if (string == "TF"){
ledOff();
ledon = false;
Serial.println(string);
}

if ((string.toInt()>0) && (string.toInt()<=255))
{
if(ledon=true){
analogWrite(led,string.toInt());
Serial.println(string);
delay(10);
}
}

}//end loop

void ledOn(){
analogWrite(led,255);
delay(10);
}

void ledOff(){
analogWrite(led,0);
delay(10);
}

This is Android code

package com.example.azeemabbas.arduino;

import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import java.io.IOException;
import java.util.UUID;

/**

  • Created by azeem abbas on 2/18/2016.
    */
    public class ledControl extends AppCompatActivity{

Button btnOn,btnOff,btnDis;
SeekBar brightness;
TextView lumn;
String address=null;
//public static String EXTRA_ADDRESS = "device_address";
private ProgressDialog progress;
BluetoothAdapter myBluetooth = null;
BluetoothSocket btSocket = null;
private boolean isBtConnected = false;
//SPP UUID. Look for it
static final UUID myUUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
public static String EXTRA_ADDRESS = "device_address";

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

Intent newint = getIntent();
address = newint.getStringExtra(MainActivity.EXTRA_ADDRESS);

setContentView(R.layout.activity_led_control);

btnOn = (Button)findViewById(R.id.button2);
btnOff = (Button)findViewById(R.id.button3);
btnDis = (Button)findViewById(R.id.button4);
brightness = (SeekBar)findViewById(R.id.seekBar);
lumn = (TextView)findViewById(R.id.lumn);

new ConnectBT().execute();

btnOn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
turnOnLed();
}
});

btnOff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
turnOffLed();
}
});

btnDis.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Disconnect();
}
});

brightness.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser==true){
lumn.setText(String.valueOf(progress));

try {
btSocket.getOutputStream().write(String.valueOf(progress).getBytes());
//Disconnect();

} catch (IOException e) {
e.printStackTrace();
}
}
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {

}
});
}

private void Disconnect(){
if (btSocket!=null){
try {
btSocket.close();
} catch (IOException e) {
msg("Error");
}
}
finish();
/* Intent i= new Intent(ledControl.this,Redirection.class);
i.putExtra(EXTRA_ADDRESS,address);
startActivity(i);*/
}

private void turnOffLed(){

if (btSocket!=null) {

try {

msg("turnOFF");
String msg = "TF";
btSocket.getOutputStream().write(msg.toString().getBytes());
//Disconnect();
} catch (IOException e) {
msg("Error");
}
}
}

private void turnOnLed(){

if (btSocket!=null){

try {
msg("turnON");
String msg = "TO";
btSocket.getOutputStream().write(msg.toString().getBytes());
//Disconnect();
} catch (IOException e) {
msg("Error");
}
}
}

// fast way to call Toast
private void msg(String s)
{
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}

private class ConnectBT extends AsyncTask <Void, Void, Void>
{
private boolean ConnectSuccess = true;

@Override
protected void onPreExecute() {
super.onPreExecute();
progress= ProgressDialog.show(ledControl.this,"Connecting....","Please wait!!!");
}

@Override
protected Void doInBackground(Void... devices) {

try {
if (btSocket==null || !isBtConnected){
myBluetooth = BluetoothAdapter.getDefaultAdapter();//get the mobile bluetooth device
BluetoothDevice dispositivo = myBluetooth.getRemoteDevice(address);//connects to the device address and check if it is available
btSocket=dispositivo.createInsecureRfcommSocketToServiceRecord(myUUID);//create a RFCOMM (SPP) connection
BluetoothAdapter.getDefaultAdapter().cancelDiscovery();
btSocket.connect();
}
} catch (IOException e) {
ConnectSuccess = false;
}

return null;
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);

if (!ConnectSuccess){
msg("Connection Failed. Is it a SPP Bluetooth? Try again.");
finish();
}
else {
msg("Connected.");
isBtConnected = true;
}

progress.dismiss();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_led_control, 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();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

}

Debug using Bluetooth Terminal on Android first to make sure your Arduino code works.

The Arduino code is working perfectly fine I have tested it on Serial Monitor.I think there is some issue with android and bluetooth module connectivity.They remain connected for 10 seconds than disconnected.

Android problem. Either

  1. improper connection procedure or

  2. slack android code

Your real goal is unclear, but reinventing the wheel along the way probably doesn't help, so read reply #3 again, and then add "also to make sure your bluetooth is working.".