0
votes

Is there a way to autoconnect my phone to an paired device without having to activate bluetooth every time? I want to create an app that works in the background and connect to a paired device even if the bluetooth is off.

if(device.getBondState()==device.BOND_BONDED){

Log.d(TAG,device.getName());
//BluetoothSocket mSocket=null;
try {
    mSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e1) {
    // TODO Auto-generated catch block
    Log.d(TAG,"socket not created");
    e1.printStackTrace();
}
try{
    mSocket.connect();
}
catch(IOException e){
    try {
        mSocket.close();
        Log.d(TAG,"Cannot connect");
    } catch (IOException e1) {
        Log.d(TAG,"Socket not closed");
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}

This code connects the device to a already paired device but the user needs to turn on the bluetooth every time to use my app. I need to know if there is a way to listen if the device that I'm paired to is present and connect to it without turning on the bluetooth every time?

1
What code have you tried? Or are you simply asking how to use your device? - Joshua Drake
I have no idea from where start coding but i know that there is a way to auto connect to the paired device - moa101
Per our guidelines we do not do recommendations or basic documentation. You'll need to bring some code and the specific error you get when running it. Do you have a hello world app running yet? If not I'd start with a tutorial of that nature. - Joshua Drake
i just do the that in the answer bellow. - moa101
Given your update, I do not see how you can tell if a Bluetooth device is nearby if Bluetooth is off. It would be like trying to turn on a radio whenever a signal is present, but if the radio is off how would you know that a signal is present? - Joshua Drake

1 Answers

0
votes
if(device.getBondState()==device.BOND_BONDED){

    Log.d(TAG,device.getName());
    //BluetoothSocket mSocket=null;
    try {


        mSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        Log.d(TAG,"socket not created");
        e1.printStackTrace();
    }
    try{

        mSocket.connect();

    }
    catch(IOException e){
        try {

            mSocket.close();
            Log.d(TAG,"Cannot connect");
        } catch (IOException e1) {
            Log.d(TAG,"Socket not closed");
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }


    }

This code connect the device to a already paired device but the user need to turn on the bluetooth every time to use my app. i need to know if there is a way to listen if the device that i m paired to are present and connect to it without turning on the bluetooth every time ???