0

I am connecting to a HC-06 Bluetooth module which works only as a client.

When there is coverage and the first time, the devices pair and connect. However, after there is no coverage, that is the module is far away from phone, the connection drops and user have to connect to the module again.

Can I make so that the phone will automatically connect to the paired module when it is back in coverage again?

Can you give an example how can I achieve this?

1 Answers1

1

here is the simplify sample.

service which is run in background and broadcast receiver take an action new device founded.

public class MyService extends Service {


        //private BluetoothSocket mmSocket;  

        @Override
        public void onCreate() {     

         LocalBroadcastManager.getInstance(getBaseContext()).registerReceiver(receiver, new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED));

            super.onCreate();
        }



        @Override
        public void onDestroy() {


            super.onDestroy();

           }

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {   


        return super.onStartCommand(intent, flags, startId);


        }

        @Override
        public IBinder onBind(Intent arg0) {        
            return null;
        }



 final BroadcastReceiver     receiver = new BroadcastReceiver(){

                @Override
                public void onReceive(Context arg0, Intent arg1) {
                    // TODO Auto-generated method stub
                    // TODO Auto-generated method stub


                }};

}

u can also take a look at here

Community
  • 1
  • 1
sakir
  • 3,391
  • 8
  • 34
  • 50