I've got a short question. How can I update a TextField without using an AsyncTask? In my Activity class I've got a function like this:
private void CheckBlueToothState(){
if (bluetoothAdapter == null){
status.setText("Bluetooth NOT support.");
}else{
if (bluetoothAdapter.isEnabled()){
if(bluetoothAdapter.isDiscovering()){
status.setText("Bluetooth is currently in device discovery process.");
}else{
status.setText("Bluetooth is Enabled");
search.setEnabled(true);
}
}else{
status.setText("Bluetooth is NOT Enabled");
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
}
Which checks if device got Bluetooth, is it enabled or disabled, you hopefully got the picture. So over to my question once again. How can I dynamically change the textfield status if I turn of the bluetooth, and vice versa?
Register BroadcastReceiver with intent action BluetoothAdapter.ACTION_STATE_CHANGED and move your text updating code into onReceive method.
Example code could be found here.
Related
I would like the device names to show up in the textView of my app when they are found during scanning but nothing shows up.
I see the bluetooth is successfully enabled and my other device is visible but nothing ever shows up.
bluetoothAdapter = (BluetoothAdapter.getDefaultAdapter());
bluetoothAdapter.enable();
bluetoothAdapter.startDiscovery();
BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//Finding devices
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Get the BluetoothDevice object from the Intent
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// Add the name and address to an array adapter to show in a ListView
textView.append(device.getAddress());
}
}
};
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
I know the above code was kinda intended for use in a listView but would it not still work in textView?
I think i have found the problem here.
What's happening is there is a problem when you call bluetoothAdapter.startDiscovery(); right after bluetoothAdapter.enable();
The adapter apparently needs a few seconds to get everything set up before you start doing stuff with it.
So i put a little delay in between those like so:
bluetoothAdapter.enable();
try {
Thread.sleep(3000);//3 seconds
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
bluetoothAdapter.startDiscovery();
And now everything seems to work.
I use this code to start bluetooth:
BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
if(btAdapter == null)
returns; /no bluetooth
if(btAdapter.isEnabled() == false)
{ Toast("Bluetooth off. Starting it...");
if(btAdapter.enable() == false)
Toast("Error enabling bluetooth.");
}
It should be very simple. Just gets and adapter and if it is not enabled then I start it.
The problem is that isEnabled() returns false when bluetooth is actualy ON (it should return true) And calling to enable() returns false so it shows "Error enabling bluetooth." I guess because it was already ON. After that my bluetooth symbols (in the status bar) is gone.
Any hint?
Of course I have the permissions.
ensure you have the permissions correct in the manifest file
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
You can't enable Bluetooth without the user's confirmation. You need to do it like this,
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter != null) {
// Device supports Bluetooth
if (!mBluetoothAdapter.isEnabled()) {
// Bluetooth isn't enabled, so enable it.
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
A dialog will appear requesting user permission to enable Bluetooth, as shown below.
If the user responds "Yes," the system will begin to enable Bluetooth and focus will return to your application once the process completes (or fails).
To check Bluetooth state, ON programmatically:
Add Following Permission : -
android.permission.BLUETOOTH
Use Following Function For Enable BLUETOOTH:-
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
private void turnOn() {
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBtIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(enableBtIntent);
}
}
I'd like to know if there is any way I can simply check whether a Bluetooth device is currently connected - I don't want to use a broadcast receiver - just check the state. I can't seem find out how this is done.
I currently have a listener that does listen to state changes with Bluetooth, and changes an internal variable accordingly - but, even though it sounds weird just saying it, it actually seems to miss the Bluetooth device disconnect broadcast sometimes. what I would like to do is run an additional check to see if the device really is still connected, or if the broadcast was missed...
so, how do I do this?
Thanks for reading/helping!
I use this to check the state of Bluetooth. I don't know how to check if is currently connected to another device but I think this can be a start point.
private void CheckBlueToothState() {
if (bluetoothAdapter == null) {
//stateBluetooth.setText("Bluetooth NOT support");
} else {
if (bluetoothAdapter.isEnabled()) {
if (bluetoothAdapter.isDiscovering()) {
//stateBluetooth.setText("Bluetooth is currently in device discovery process.");
} else {
//stateBluetooth.setText("Bluetooth is Enabled.");
}
} else {
//stateBluetooth.setText("Bluetooth is NOT Enabled!");
Intent enableBtIntent = new Intent(
BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
}
I am developing an app that is going to use a ToggleButton to enable and disable BlueTooth. I manage to make the ToggelButton turn on and off BlueTooth, but I can not make it check if BlueTooth is turned on and off. The problem is that if you turn BlueTooth on or off from another location, you may turn off BlueTooth when you actually want to turn it on. Here is my code so far:
public void onClick(View v) {
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if(adapter != null) {
if(adapter.getState() == BluetoothAdapter.STATE_ON) {
adapter.disable();
} else if (adapter.getState() == BluetoothAdapter.STATE_OFF){
adapter.enable();
} else {
//State.INTERMEDIATE_STATE;
}
}
}
How can I make it be checked when BlueTooth is on and unchecked when BlueTooth is off?
You will have to register a broadcast receiver with the following intent filter:
"android.bluetooth.intent.action.BLUETOOTH_STATE_CHANGED".
Whenever you receive this broadcast you should recheck for actual bluetooth state.
Check out Google's PowerWidget implementation.
The receiver with appropriate intent-filters is registered in the manifest.
I am trying to figure out how to implement an event listener (unsure if this is the proper term.) I want the service, once my app is launched, to listen for the phones power status. I am uncertain to as how android handles this situation so don't really know what to search for. I've been working with this code that uses a broadcast receiver:
BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
unregisterReceiver(this);
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
if (plugged == BatteryManager.BATTERY_PLUGGED_AC) {
// on AC power
Toast.makeText(getApplicationContext(), "AC POWER", Toast.LENGTH_LONG).show();
} else if (plugged == BatteryManager.BATTERY_PLUGGED_USB) {
// on USB power
Toast.makeText(getApplicationContext(), "USB POWER", Toast.LENGTH_LONG).show();
startActivity(alarmClockIntent);
} else if (plugged == 0) {
Toast.makeText(getApplicationContext(), "On Battery", Toast.LENGTH_LONG).show();
} else {
// intent didnt include extra info
}
}
};
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(receiver, filter);
The code works fine. When I open my app it will toast what the current status of the phone power is.
Here is what I am trying to do:
When the user launches the app, it is effectively turning on the service
The user can go about using the phone, but once it is plugged in, my service will catch that and use the code above
How do I adapt this code to achieve the objectives above?
You could keep the listener on for the battery status by removing the line
unregisterReceiver(this);
This way, the app will continue to listen to power status change in the background even though that the app is not running in the foreground. Note that at some point, you might still want to unregister your receiver. You probably want to allow the user to control that via settings.
One other note, your code contains starting activity in the receiver in below code:
else if (plugged == BatteryManager.BATTERY_PLUGGED_USB) {
// on USB power
Toast.makeText(getApplicationContext(), "USB POWER", Toast.LENGTH_LONG).show();
startActivity(alarmClockIntent);
}
If your activity is in the background then it can't start another activity. See this SO Question - how to start activity when the main activity is running in background?, the accepted answer has suggestion on how to handle situation that requires starting activity from the background