My Application required bluetooth connectivity. And in the first phase I am trying to open up the standard Activity "Bluetooth Device Picker" to help user scan for new device or chose a device from the list.
The problem is that I am unable to get any working example for the bluetooth device picker. The task is simple. To start an Activity with Intent "android.bluetooth.devicepicker.action.LAUNCH"
And the device picker is opening without any problem.
But the device picker requires four extras and I am unable to figure out the exact parameters for two of the extras listed below.
.putExtra("android.bluetooth.devicepicker.extra.LAUNCH_PACKAGE","com.extreme.controlcenter"
.putExtra("android.bluetooth.devicepicker.extra.DEVICE_PICKER_LAUNCH_CLASS","com.extreme.controlcenter.WelcomeActivity")
What I thought the parameters should be that
*"android.bluetooth.devicepicker.extra.LAUNCH_PACKAGE"*
should have the name of my package, so I passed that only. That is "com.extreme.controlcenter"
The second should be the name of the component that must receive the broadcast that is done after a device is selected. Here I tried putting the name of the class that has the onReceive() function.
But the problem is that the onReceive() function is NOT getting called when a device is picked in device picker!
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
//Device Selected on Device Picker
if("android.bluetooth.devicepicker.action.DEVICE_SELECTED".equals(action)) {
//context.unregisterReceiver(this);
BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Toast.makeText(context, "device" + device.getAddress(), Toast.LENGTH_SHORT).show();
String MAC = device.getAddress();
//Log.d("my", MAC);
Intent intent2 = new Intent(WelcomeActivity.this, ControlActivity.class);
intent2.putExtra(EXTRA_DEVICE_ADDRESS, MAC);
startActivity(intent2);
}
};
I have created an Intent filter and registered a receive in the onCreate() of the main Activity
// Register the BroadcastReceiver
IntentFilter filter = new IntentFilter("android.bluetooth.devicepicker.action.DEVICE_SELECTED");
registerReceiver(mReceiver, filter);
One thing is that if I don't provide those two extras, the Broadcast event is received successfully. But that code only runs on my TAB, but same is crashing in cell phone. So I think providing those two extras are mandatory.
Thanks in Advance !
"com.extreme.controlcenter.WelcomeActivity" in your EXTRAs needs to be a BroadcastReceiver class such as MyBroadcastReceiver.class.getName(). I also have it declared in the manifest inside the tags
Related
I'm using an Android foreground Service to download files and I'm showing the progress of the download to the user in a Notification.
I also have a progress bar in a fragment in the app.
I'm using a broadcast Receiver to communicate the progress of the download happenning in the service to my fragment.
My problem is that the onReceive of my BroadcastReceiver is not trigger above Android 11 bu works perfectly fine on android 10, What had change about broadcast reveiver since Android 11 ?
On my fragment i'm declaring my broadcast receiver :
private final BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if ("SHOW_PROGRESS".equals(intent.getAction())) {
showProgress(intent.getIntExtra("progress", 0));
}
}
};
And registering it in OnCreateView :
IntentFilter filter = new IntentFilter();
filter.addAction("SHOW_PROGRESS");
getContext().registerReceiver(receiver, filter);
From my service I'm calling it :
Intent broadcastIntent = new Intent().setAction("SHOW_PROGRESS").putExtra("progress", downLoadState.getGlobalPercent());
getApplicationContext().sendBroadcast(broadcastIntent);
I have nothing in my manifest since my service don't interacts with device components
I have an app that requires the use of Bluetooth to send data like 4-5 numbers and 2-3 texts between devices but i am new to java and the guide from google developers page is a little difficult for me to understand.
In order to make it easier for anyone whiling to help me i will write down in steps what i have done so far.
1. I have asked for permissions in manifest:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
2. I got the default adapter in my activity:
final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
3. I created a button that when clicked the device scans for other devices:
bt_scan.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//first checks if bluettoth is enabled
if (!mBluetoothAdapter.isEnabled()) {
//if not it enables it
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
// Register for broadcasts when a device is discovered.
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter);
}
});
4. When a device is found, get the device information:
// Create a BroadcastReceiver for ACTION_FOUND.
private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothDevice.ACTION_FOUND.equals(action)) {
// Discovery has found a device. Get the BluetoothDevice
// object and its info from the Intent.
BluetoothDevice device =
intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
String deviceHardwareAddress = device.getAddress(); // MAC address
}
}
};
5. On destroy unregister the ACTION_FOUND receiver:
#Override
protected void onDestroy() {
super.onDestroy();
// Don't forget to unregister the *ACTION_FOUND* receiver.
unregisterReceiver(mReceiver);
}
6. I have created a second button that when clicked it enables discoverability (which must be clicked first from device A in order for scan button in device B, finds the device A):
bt_enable.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra( BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION,300);
startActivity(discoverableIntent);
}
});
Questions :
A. In step 3 what REQUEST_ENABLE_BT is for?
B. Is this all i need to establish a connection without any problems between two devices?
C. If i want to connect more than two devices, is there anything else i must add?
Q. What is REQUEST_ENABLE_BT for?
Let me try to comment the code
// If bluetooth is not switched on
if ( ! mBluetoothAdapter.isEnabled() ) {
// Create a system request to enable bluetooth
Intent enableBtIntent = new Intent( BluetoothAdapter.ACTION_REQUEST_ENABLE );
// Queue the request to pops the bluetooth dialogue
startActivityForResult( enableBtIntent, REQUEST_ENABLE_BT );
}
// Standby for device found, Bluetooth may still be disabled at this point
IntentFilter filter = new IntentFilter( BluetoothDevice.ACTION_FOUND );
registerReceiver( mReceiver, filter );
// Now that we are done, bluetooth dialogue will popup
// When it's done Android calls our onActivityResult
// (because we queued it with startActivityForResult).
Q. Is this all I need to establish a connection?
I think there are two more more steps you need to do:
A. Find the device, between step 3 and 4.
Just enabling Bluetooth does not mean it'll automatically scan nearby devices, because scanning cost battery and degrades existing connections.
But with BLUETOOTH_ADMIN permission your app can start and stop the scan.
You can also get previously paired device without scan.
Start Scan: BluetoothAdapter.startDiscovery() This will call ACTION_FOUND for each device found, which fit your code.
List of paired devices: BluetoothAdapter.getBondedDevices() Previously paired Bluetooths can be found in this Set without scanning or BLUETOOTH_ADMIN, and can be connected directly. Whether the device is actually online is unknown until successful or failure, though.
B. Establish the data connection, between 4 and 5.
A device connection is like connecting a network cable.
The computers are physically connected with IP, but your programs still need to open sockets for actual data transfer.
Connection: BluetoothDevice.createRfcommSocketToServiceRecord(uuid) Creates a socket from which data can be go through InputStream and OutputStream.
Don't forget to stop the scan: BluetoothAdapter.cancelDiscovery()
You seems to want to make an M2M messaging app, so I've skipped Bluetooth Low Energy (BLE).
Q. If I want to connect more than two devices, is that all?
As far as "multiple connection" part is concerned, no, you don't need to do anything special to connect to multiple devices at the same time.
The "management" is arguably the hard part, though.
For example you'd normally want to keep a list of the devices, and have a UI that let user selectively disconnect a device, all of which must respond to unexpected disconnections or switching off bluetooth (by user, by another app, or by the system such as low-power mode).
Depends on what you actually want to do with the bluetooth devices, you may need code to remember each one's state too.
Again with error handling.
So, do expect the final code will be a bit more developed.
I would like to answer Question A
A. In step 3 what REQUEST_ENABLE_BT is for?
private static final int REQUEST_ENABLE_BT = 1;
REQUEST_ENABLE_BT is simply an integer value which identifies your request. When you receive the result Intent, the callback onActivityResult(int requestCode, int resultCode, Intent data) provides the same request code to you. In this way you can handle results from other activites
Suppose you have Activity A from where you have requested to enable bluetooth
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
When device's bluetooth gets enable, a method onActivityResult(int requestCode, int resultCode, Intent data) will get invoked on Activity A. This method onActivityResult() is a callback method. This method will pass back the request code to the calling activity (Activity A) as soon as bluetooth gets enable. You have to override this method in your Activity A to handle results.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == REQUEST_ENABLE_BT)
Log.d("BLUETOOTH", "Successfully enabled");
}
I'm writing here because I'm facing a probleme that I could not resolve even after many researches and tries.
I'm currently developing an Android Library which consists only of java classes and fragment. The problem is I need to send Local Notifications to the user, and clicking on the notifications should send the user back to the activity where he was. At this point, my library sends the notifications just fine. But the click on the notification doesn't have any action.
In my notification reciver class (which extends the BroadcastReceiver class), when the notification appears, I create a Pending Intent but I don't know what I can give as parameters to send the user to the activity. I tried using intent filters but it give me no results
So how can I have the notification sending back the user to the application ? The best would be if I was able to have the notification sending back the user to the activity where the notification is created (but it's a fragment so...)
In an usual app, I would've an intent sending back the user to an activity class but my library needs to have only fragments.
Maybe there is no problem and the solution is easy since I'm new to notifications
If someone here have an idea thanks for helping me ! :D
And if my problem isn't clear (Because of my bad english as an example) don't hesitate to ask me to add informations ^^
**Edit from 29 April : **
I managed to achieve it by giving to my broadcast pending intent the canonical name of my class using :
mContext.getClass().getCanonicalName();
Once in my broadcast receiver class I just get the class from the name of the sending class :
Class<?> activityClass = null;
try {
activityClass = Class.forName(stringSourceClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Check out below code...
public BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
try {
String title = context.getString(R.string.app_name);
Intent intent1 = new Intent(context, YourActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),1,intent1,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setContentText("Hello")
.setAutoCancel(false)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notificationBuilder.build());
} catch (Exception e) {
}
}
};
check the Building a notification page:
Intent resultIntent = new Intent(this, ResultActivity.class);
...
// Because clicking the notification opens a new ("special") activity, there's
// no need to create an artificial back stack.
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
just put your activity in resultIntent
how can I have the notification sending back the user to the
application ?
That's pretty simple:
1. While creating intent for pending intent call addAction ("action_name") method;
2. In activity you want to call (in manifest file) inside intent-filter tag add <action android:name="action_name>.
Now when your notification try to launch activity it would send intent message to system, which would search activity with proper action and launch it.
P.S. action name must be unique for every application
I am working on a messaging app, it sends user notification when he is on a different activtyon my app or is on another app but if the user is on MessagingActivity.java it just updates the chat history and does not send any notifications which is perfectly fine, but the problem arises when the user is on MessagingActivity.java meanwhile an email or something else happen user leaves the MessagingActivity.java open and checks that app if in the meantime a message comes user does not receive any notifications
public void parseRequest(Bundle extras) {
if (extras.containsKey("for") && extras.containsKey("recipientID")) {
if (Integer.parseInt(extras.getString("recipientID")) == M.getID(this)) {
switch (extras.getString("for")) {
case "chat":
if (isRunning("MessagingActivity")) {
Intent intent = new Intent("update_messages_list");
intent.putExtra("data", extras);
sendBroadcast(intent);
} else {
Intent resultIntent = new Intent(this, MessagingActivity.class);
resultIntent.putExtra("conversationID", Integer.parseInt(extras.getString("conversationID")));
resultIntent.putExtra("recipientID", Integer.parseInt(extras.getString("ownerID")));
M.showNotification(getApplicationContext(), resultIntent,
extras.getString("ownerUsername"),
extras.getString("message"),
Integer.parseInt(extras.getString("conversationID")));
}
Let me know how you are checking that your MessageActivity is Running i.e. functioning of isRunning("MessagingActivity") method. If you are setting any global boolean variable for checking this and making isRunning value false in onDestroy() method of that activity then, according to life cycle of Activity it is not called until your activity is finished i.e. in your case user just switching from MessageActivity to Mail .
I am by no means an expert, but you could just set a boolean variable by overriding the Activity's onPause() and onResume() events.
Simply set msgActivityActive to true in onResume(), false in onPause(), and change your call to:
if (isRunning("MessagingActivity") && msgActivityActive)
I'm trying to simply set a proximity later for an area an for testing, I simply added this to the onCreate method of my main activity.
public void onCreate(Bundle bndBundle) {
IntentFilter filter = new IntentFilter(WidgetService.ACTION_STOP_PROXIMITY);
registerReceiver(new ProximityIntentReceiver(), filter);
LocationManager locManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
Intent ittIntent = new Intent(this, ProximityIntentReceiver.class);
ittIntent.putExtra(WidgetService.KEY_STOP_IDENTIFIER, 1000);
PendingIntent pitIntent = PendingIntent.getBroadcast(this, 0, ittIntent, 0);
locManager.addProximityAlert(60.15769, 24.94150, 150, -1, pitIntent);
super.onCreate(bndBundle);
getActionBar().setDisplayHomeAsUpEnabled(false);
}
..and here's the simple receiver class that I'm using
public class ProximityIntentReceiver extends BroadcastReceiver {
private static final int NOTIFICATION_ID = 1000;
#Override
public void onReceive(Context context, Intent intent) {
String key = LocationManager.KEY_PROXIMITY_ENTERING;
Boolean entering = intent.getBooleanExtra(key, false);
if (entering) {
Log.d(getClass().getSimpleName(), "entering");
}
else {
Log.d(getClass().getSimpleName(), "exiting");
}
}
}
I'm testing this on my emulator and when I use the DDMS console to set the co-ordinates of the phone manually, I still don't see the log message.
My manifest file doesn't have any special code. I've added the correct permissions and have the code for a simple activity- no services or anything.
I read through a whole bunch of posts on StacKOverflow but I haven't been able to resolve the issue. Am I missing something in my snippet?
You are registering this receiver dynamically, through registerReceiver(), to have it respond to broadcasts whose action string is WidgetService.ACTION_STOP_PROXIMITY.
However, the actual broadcast you are sending is trying to use an explicit Intent, identifying your receiver class. This does not line up with the IntentFilter that you are using with registerReceiver().
Either:
Register your receiver in the manifest and get rid of registerReceiver(), in which case your explicit Intent will work, or
Use new Intent(WidgetService.ACTION_STOP_PROXIMITY) instead of new Intent(this, ProximityIntentReceiver.class), so your Intent lines up with your IntentFilter
You cannot use explicit Intent objects to send broadcasts to receivers registered via registerReceiver(). An explicit Intent will only work with a manifest-registered receiver.
make sure you type in the right coordinates. in DDMS they're reversed, longitude first, then latitude