Android: Use progress dialog till a new activity loads up












0















I am trying to connect to a bluetooth device(BLE HC05 module) using my app, to do some IOT functionality



Below snippet shows the code which is used to go to a new activity when user clicks on a particular device to connect to.



This is in BluetoothActivity.java



lvPairedDevices.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

final Intent intent = new Intent(BluetoothActivity.this, TemperatureActivity.class);
intent.putExtra("Bluetooth Device Address", mBTPairedDevices.get(position).getAddress());
startActivity(intent);

}
});


As it is mentioned, I am sending the Bluetooth device's MAC Address. Using it an attempt to connect to the device is made.



This is in TemperatureActivity.java



Intent intent = getIntent();
Bundle extras = intent.getExtras();
deviceAddress = extras.getString("Bluetooth Device Address");
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
device = bluetoothAdapter.getRemoteDevice(deviceAddress);

try {
btSocket = device.createInsecureRfcommSocketToServiceRecord(myUUID);
bluetoothAdapter.cancelDiscovery();
btSocket.connect();

if(btSocket.isConnected())
Toast.makeText(TemperatureActivity.this,"Connected to " + device.getName() + " successfully", Toast.LENGTH_SHORT).show();
else
Toast.makeText(TemperatureActivity.this,"Couldn't connect to " + device.getName() + " properly", Toast.LENGTH_SHORT).show();

outStream = btSocket.getOutputStream();
connectedThread = new ConnectedThread(btSocket);
connectedThread.start();
} catch (IOException e) {
Log.d(TAG, "onResume: Bluetooth device could not be connected using RFCOMM");
Toast.makeText(TemperatureActivity.this, "Connection to device " + device.getName() + " using RFCOMM has failed", Toast.LENGTH_SHORT).show();
finish();
}


According to the above code, if the device is offline or has declined to connect, an exception will arise.



With the current situation, is there any possibility of using Progress dialog in the BluetoothActivity.java and stop it in both cases(bluetooth connection is established successfully or not) in TemperatureActivity.java



I will go to TemperatureActivity with the device's MAC address, if it is successful in connecting to the device it should stay in that page(TemperatureActivity) otherwise, return to the original page (BluetoothActivity) saying "Couldn't connect to the device"



I searched a lot, could not find an answer. Thanks in advance.










share|improve this question

























  • what are you really trying to achieve here? you don't want to start TemperatureActivity if the connection is not successful?

    – takecare
    Nov 18 '18 at 0:10











  • I will go to TemperatureActivity with the device's MAC address, if it is successful in connecting to the device it should stay in that page(TemperatureActivity) otherwise, return to the original page (BluetoothActivity) saying "Couldn't connect to the device"

    – KIRAN C NAYAK
    Nov 18 '18 at 15:24











  • it looks like you want to display your progress dialog in TemperatureActivity then, whilst the device is trying to connect. if unsuccessful you finish() TemperatureActivity.

    – takecare
    Nov 19 '18 at 9:41
















0















I am trying to connect to a bluetooth device(BLE HC05 module) using my app, to do some IOT functionality



Below snippet shows the code which is used to go to a new activity when user clicks on a particular device to connect to.



This is in BluetoothActivity.java



lvPairedDevices.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

final Intent intent = new Intent(BluetoothActivity.this, TemperatureActivity.class);
intent.putExtra("Bluetooth Device Address", mBTPairedDevices.get(position).getAddress());
startActivity(intent);

}
});


As it is mentioned, I am sending the Bluetooth device's MAC Address. Using it an attempt to connect to the device is made.



This is in TemperatureActivity.java



Intent intent = getIntent();
Bundle extras = intent.getExtras();
deviceAddress = extras.getString("Bluetooth Device Address");
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
device = bluetoothAdapter.getRemoteDevice(deviceAddress);

try {
btSocket = device.createInsecureRfcommSocketToServiceRecord(myUUID);
bluetoothAdapter.cancelDiscovery();
btSocket.connect();

if(btSocket.isConnected())
Toast.makeText(TemperatureActivity.this,"Connected to " + device.getName() + " successfully", Toast.LENGTH_SHORT).show();
else
Toast.makeText(TemperatureActivity.this,"Couldn't connect to " + device.getName() + " properly", Toast.LENGTH_SHORT).show();

outStream = btSocket.getOutputStream();
connectedThread = new ConnectedThread(btSocket);
connectedThread.start();
} catch (IOException e) {
Log.d(TAG, "onResume: Bluetooth device could not be connected using RFCOMM");
Toast.makeText(TemperatureActivity.this, "Connection to device " + device.getName() + " using RFCOMM has failed", Toast.LENGTH_SHORT).show();
finish();
}


According to the above code, if the device is offline or has declined to connect, an exception will arise.



With the current situation, is there any possibility of using Progress dialog in the BluetoothActivity.java and stop it in both cases(bluetooth connection is established successfully or not) in TemperatureActivity.java



I will go to TemperatureActivity with the device's MAC address, if it is successful in connecting to the device it should stay in that page(TemperatureActivity) otherwise, return to the original page (BluetoothActivity) saying "Couldn't connect to the device"



I searched a lot, could not find an answer. Thanks in advance.










share|improve this question

























  • what are you really trying to achieve here? you don't want to start TemperatureActivity if the connection is not successful?

    – takecare
    Nov 18 '18 at 0:10











  • I will go to TemperatureActivity with the device's MAC address, if it is successful in connecting to the device it should stay in that page(TemperatureActivity) otherwise, return to the original page (BluetoothActivity) saying "Couldn't connect to the device"

    – KIRAN C NAYAK
    Nov 18 '18 at 15:24











  • it looks like you want to display your progress dialog in TemperatureActivity then, whilst the device is trying to connect. if unsuccessful you finish() TemperatureActivity.

    – takecare
    Nov 19 '18 at 9:41














0












0








0








I am trying to connect to a bluetooth device(BLE HC05 module) using my app, to do some IOT functionality



Below snippet shows the code which is used to go to a new activity when user clicks on a particular device to connect to.



This is in BluetoothActivity.java



lvPairedDevices.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

final Intent intent = new Intent(BluetoothActivity.this, TemperatureActivity.class);
intent.putExtra("Bluetooth Device Address", mBTPairedDevices.get(position).getAddress());
startActivity(intent);

}
});


As it is mentioned, I am sending the Bluetooth device's MAC Address. Using it an attempt to connect to the device is made.



This is in TemperatureActivity.java



Intent intent = getIntent();
Bundle extras = intent.getExtras();
deviceAddress = extras.getString("Bluetooth Device Address");
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
device = bluetoothAdapter.getRemoteDevice(deviceAddress);

try {
btSocket = device.createInsecureRfcommSocketToServiceRecord(myUUID);
bluetoothAdapter.cancelDiscovery();
btSocket.connect();

if(btSocket.isConnected())
Toast.makeText(TemperatureActivity.this,"Connected to " + device.getName() + " successfully", Toast.LENGTH_SHORT).show();
else
Toast.makeText(TemperatureActivity.this,"Couldn't connect to " + device.getName() + " properly", Toast.LENGTH_SHORT).show();

outStream = btSocket.getOutputStream();
connectedThread = new ConnectedThread(btSocket);
connectedThread.start();
} catch (IOException e) {
Log.d(TAG, "onResume: Bluetooth device could not be connected using RFCOMM");
Toast.makeText(TemperatureActivity.this, "Connection to device " + device.getName() + " using RFCOMM has failed", Toast.LENGTH_SHORT).show();
finish();
}


According to the above code, if the device is offline or has declined to connect, an exception will arise.



With the current situation, is there any possibility of using Progress dialog in the BluetoothActivity.java and stop it in both cases(bluetooth connection is established successfully or not) in TemperatureActivity.java



I will go to TemperatureActivity with the device's MAC address, if it is successful in connecting to the device it should stay in that page(TemperatureActivity) otherwise, return to the original page (BluetoothActivity) saying "Couldn't connect to the device"



I searched a lot, could not find an answer. Thanks in advance.










share|improve this question
















I am trying to connect to a bluetooth device(BLE HC05 module) using my app, to do some IOT functionality



Below snippet shows the code which is used to go to a new activity when user clicks on a particular device to connect to.



This is in BluetoothActivity.java



lvPairedDevices.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

final Intent intent = new Intent(BluetoothActivity.this, TemperatureActivity.class);
intent.putExtra("Bluetooth Device Address", mBTPairedDevices.get(position).getAddress());
startActivity(intent);

}
});


As it is mentioned, I am sending the Bluetooth device's MAC Address. Using it an attempt to connect to the device is made.



This is in TemperatureActivity.java



Intent intent = getIntent();
Bundle extras = intent.getExtras();
deviceAddress = extras.getString("Bluetooth Device Address");
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
device = bluetoothAdapter.getRemoteDevice(deviceAddress);

try {
btSocket = device.createInsecureRfcommSocketToServiceRecord(myUUID);
bluetoothAdapter.cancelDiscovery();
btSocket.connect();

if(btSocket.isConnected())
Toast.makeText(TemperatureActivity.this,"Connected to " + device.getName() + " successfully", Toast.LENGTH_SHORT).show();
else
Toast.makeText(TemperatureActivity.this,"Couldn't connect to " + device.getName() + " properly", Toast.LENGTH_SHORT).show();

outStream = btSocket.getOutputStream();
connectedThread = new ConnectedThread(btSocket);
connectedThread.start();
} catch (IOException e) {
Log.d(TAG, "onResume: Bluetooth device could not be connected using RFCOMM");
Toast.makeText(TemperatureActivity.this, "Connection to device " + device.getName() + " using RFCOMM has failed", Toast.LENGTH_SHORT).show();
finish();
}


According to the above code, if the device is offline or has declined to connect, an exception will arise.



With the current situation, is there any possibility of using Progress dialog in the BluetoothActivity.java and stop it in both cases(bluetooth connection is established successfully or not) in TemperatureActivity.java



I will go to TemperatureActivity with the device's MAC address, if it is successful in connecting to the device it should stay in that page(TemperatureActivity) otherwise, return to the original page (BluetoothActivity) saying "Couldn't connect to the device"



I searched a lot, could not find an answer. Thanks in advance.







java android exception bluetooth






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 '18 at 15:25







KIRAN C NAYAK

















asked Nov 17 '18 at 19:38









KIRAN C NAYAKKIRAN C NAYAK

84




84













  • what are you really trying to achieve here? you don't want to start TemperatureActivity if the connection is not successful?

    – takecare
    Nov 18 '18 at 0:10











  • I will go to TemperatureActivity with the device's MAC address, if it is successful in connecting to the device it should stay in that page(TemperatureActivity) otherwise, return to the original page (BluetoothActivity) saying "Couldn't connect to the device"

    – KIRAN C NAYAK
    Nov 18 '18 at 15:24











  • it looks like you want to display your progress dialog in TemperatureActivity then, whilst the device is trying to connect. if unsuccessful you finish() TemperatureActivity.

    – takecare
    Nov 19 '18 at 9:41



















  • what are you really trying to achieve here? you don't want to start TemperatureActivity if the connection is not successful?

    – takecare
    Nov 18 '18 at 0:10











  • I will go to TemperatureActivity with the device's MAC address, if it is successful in connecting to the device it should stay in that page(TemperatureActivity) otherwise, return to the original page (BluetoothActivity) saying "Couldn't connect to the device"

    – KIRAN C NAYAK
    Nov 18 '18 at 15:24











  • it looks like you want to display your progress dialog in TemperatureActivity then, whilst the device is trying to connect. if unsuccessful you finish() TemperatureActivity.

    – takecare
    Nov 19 '18 at 9:41

















what are you really trying to achieve here? you don't want to start TemperatureActivity if the connection is not successful?

– takecare
Nov 18 '18 at 0:10





what are you really trying to achieve here? you don't want to start TemperatureActivity if the connection is not successful?

– takecare
Nov 18 '18 at 0:10













I will go to TemperatureActivity with the device's MAC address, if it is successful in connecting to the device it should stay in that page(TemperatureActivity) otherwise, return to the original page (BluetoothActivity) saying "Couldn't connect to the device"

– KIRAN C NAYAK
Nov 18 '18 at 15:24





I will go to TemperatureActivity with the device's MAC address, if it is successful in connecting to the device it should stay in that page(TemperatureActivity) otherwise, return to the original page (BluetoothActivity) saying "Couldn't connect to the device"

– KIRAN C NAYAK
Nov 18 '18 at 15:24













it looks like you want to display your progress dialog in TemperatureActivity then, whilst the device is trying to connect. if unsuccessful you finish() TemperatureActivity.

– takecare
Nov 19 '18 at 9:41





it looks like you want to display your progress dialog in TemperatureActivity then, whilst the device is trying to connect. if unsuccessful you finish() TemperatureActivity.

– takecare
Nov 19 '18 at 9:41












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53354847%2fandroid-use-progress-dialog-till-a-new-activity-loads-up%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53354847%2fandroid-use-progress-dialog-till-a-new-activity-loads-up%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()