Programmatically connect to a chromecast route: do I need a permission? - java

I want to connect to chromecast route programmatically, just as posted here: Is it possible to programmatically connect to a chromecast route?
When doing so (calling mMediaRouter.selectRoute(aRouteInfo)), I get an exception java.lang.IllegalStateException: Null media projection manager.
However, when selecting my chromecast receiver via the chromecast button from the popup list, then disconnecting and then calling mMediaRouter.selectRoute(aRouteInfo) again, it works fine.
I assume I have to ask for some kind of permission before. Anyone knows what to do in this case?
The exception will be thrown in this line ...
mProjection = ((MediaProjectionManager) context.getSystemService(Context.MEDIA_PROJECTION_SERVICE)).getMediaProjection(permissionsResultCode, permissionsData);
... the parameter permissionData (Intent) seems to be incomplete.

I found the (newbie)problem. I just had to create the Intent by myself and react on this within "onActivityResult".

Related

How to fall back an outbound call to the SIM account from a custom Android phone account?

I implemented a custom PhoneAccount by extending ConnectionService and normally this account is set as default.
I search for a solution to send outgoing calls over the SIM account if inside onCreateOutgoingConnection the code detects the alternate service is not available or fails to start the call.
I wanted to use ConnectionService.createRemoteOutgoingConnection() but I didn't find a way to create a Connection from the returned RemoteConnection

check automatically for new message android

I am trying to create a simple android messaging app. So basically let us assume that user A sends a message to user B. I would like that user B receives a notification when the message is received. I know how to create the notification and all. But basically I would like to know how the user B constantly checks if a new message has been received even when he is out of the app, and then that would trigger the notification and subsequent actions.
Thank you
You have to setup an unbound background service.
With this you can constantly make pull-requests to your server or get push-notifications from your server and display notifications.
https://developer.android.com/training/run-background-service/create-service.html
You can use Event Bus lib for this purpose. When new message will be received it will create and event and then you can receive that event event and do other operations.
If you are talking about text messages then you will have to create a BroadcastReceiver
http://developer.android.com/reference/android/content/BroadcastReceiver.html
You can see my answer here for SMS Receiver
Verify sms text message by app before it displayed to user
Don't forget to give permissions in your app's manifest.
Hope it helps.

Can i programatically detect if there are any Chromecast devices on the current WiFi network?

Is it possible to detect if there are any Chromecast devices on the current WiFi network. I've seen that there's a Cast SDK but I couldn't find anything about searching for devices. Having never worked with this SDK before, it is possible that I may have overlooked it.
Chromecast devices are discoverable using avahi with the type _googlecast._tcp
$ avahi-browse -r _googlecast._tcp
+ eth0 IPv4 Living Room _googlecast._tcp local
= eth0 IPv4 Living Room _googlecast._tcp local
hostname = [Living\032Room.local]
address = [192.168.1.100]
port = [8009]
txt = ["st=0" "fn=Living Room" "ca=5" "ic=/setup/icon.png" "md=Chromecast" "ve=02" "id=c832a30b81ab84a706c82745438fcd64"]
You can perform discovery without using the cast button; you need to use media router from v7 support library (which needs the v7 appcompat support as well) and then get an instance of the media router from your code, define a selector (which is basically a filter to possibly narrow down the devices that you are interested in) and then add a callback to start discovery. As devices are discovered (asynchronously), your callbacks will be called. Take a look at this sample project, specially this class which does exactly what you want.
You can use SSDP (Simple Service Discovery Protocol) in your local network with an ST (Service Type) of:
urn:dial-multiscreen-org:service:dial:1
Your SSDP Query should look like this:
M-SEARCH * HTTP/1.1\r\n
Host: 239.255.255.250:1900\r\n
MAN: "ssdp:discover"\r\n
MX: 3\r\n
ST: urn:dial-multiscreen-org:service:dial:1\r\n\r\n
You will get a list of all devices (IP and Port) that support those service type (like Chromecast does). Then you could pull a xml file from this IP:8008 to check if it really is a Chromecast device:
http://IP:8008/ssdp/device-desc.xml
The Response will look like this:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<specVersion>
<major>1</major>
<minor>0</minor>
</specVersion>
<URLBase>http://192.168.0.0:8008</URLBase>
<device>
<deviceType>urn:dial-multiscreen-org:device:dial:1</deviceType>
<friendlyName>Chromecast</friendlyName>
<manufacturer>Google Inc.</manufacturer>
<modelName>Eureka Dongle</modelName>
<UDN>uuid:3c7f2a1e-dbd2-f94d-b456-816555a9d1f9</UDN>
<iconList>
<icon>
<mimetype>image/png</mimetype>
<width>98</width>
<height>55</height>
<depth>32</depth>
<url>/setup/icon.png</url>
</icon>
</iconList>
<serviceList>
<service>
<serviceType>urn:dial-multiscreen-org:service:dial:1</serviceType>
<serviceId>urn:dial-multiscreen-org:serviceId:dial</serviceId>
<controlURL>/ssdp/notfound</controlURL>
<eventSubURL>/ssdp/notfound</eventSubURL>
<SCPDURL>/ssdp/notfound</SCPDURL>
</service>
</serviceList>
</device>
</root>
More info about it here:
http://wolfpaulus.com/jounal/mac/chromecasting/
If you want to stream something to your Chromecast, you can get information about the ChromeCast App by calling:
http://IP:8008/apps/ChromeCast
Following the recommended way of Google you don't need to scan for Chromecast devices to provide content from an Android app. You can add a cast button to your Activity by extending it with ActionBarActivity and then adding in onCreateOptionsMenu() an ActionBar option item with
app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
as action.
Here you can find a complete sample on how to send to a Chromecast stick. If you want to scan manually for any reason, you can use the Android MediaRouters API, quote from the docs:
Cast device discovery may be performed using the Android MediaRouter
APIs in the Android Support Library, with compatibility back to
Android 2.1.
A very simple solution is to use com.youview.tinydnssd.DiscoverResolver. Here my implementation:
DiscoverResolver resolver = new DiscoverResolver(ctx, "_googlecast._tcp.", new DiscoverResolverHandler());
In DiscoverResolverHandler() you can handle the result.

Reconnect to a device after device crashed

I have the following problem with the libusb-java and some self-made devices.
It could happen that such a device disconnects from the USB Port because it drains to much power (as an example: i have a USB-LED Light which needs sometimes more than 500mA).
In this case the USB Controller will reset the device and the device will startup normaly again.
Now i cant really detect such a problem except for trying to reinit the device on every Exception. But thats not working...
On Every Exception i call my init Method again, which looks like this:
private void initDevice() {
USB.init();
this.dev = USB.getDevice(idVendor, idProduct);
}
The Problem with that is, this runs without any problem, but the i get this error message when i want to send new data:
LibusbJava.controlMsg: error sending control message: Protocol error
How do i can reinit the device? Do i have to reset the bus or something?
You will need to call open() on the device - it is new for the system.

Android SMS API

I know that the SMS content provider is not part of the public API (at least not documented), but if I understand correctly it's still possible to use many of the SMS features as long as you know how to use the API(?).
E.g it's pretty straightforward to insert an SMS into your inbox:
ContentValues values = new ContentValues();
values.put("address", "+457014921911");
contentResolver.insert(Uri.parse("content://sms"), values);
Unfortunately this does not trigger the standard "new-SMS-in-your-inbox" notification. Is it possible to trigger this manually?
Edit: AFAIK the "standard mail application (Messaging)" in Android is listening for incoming SMSes using the android.permission.RECEIVE_SMS permission. And then, when a new SMS has arrived, a status bar notification is inserted with a "special" notification id.
So one solution to my problem (stated above) could be to find, and send the correct broadcast intent; something like "NEW SMS HAS ARRIVED"-intent.
Edit: Downloaded a third party messaging application (chompsms) from Android market. This application satisfies my needs better. When i execute the code above the chompsms notice the new sms and shows the "standard status bar notification". So I would say that the standard Android Messaging application is not detecting sms properly? Or am I wrong?
Unfortunately the code responsible for these notifications is hidden in the messaging application. The class MessagingNotification has a static method updateAllNotifications that you could call using a PathClassLoader and reflection:
PathClassLoader c = new PathClassLoader("/system/app/Mms.apk", getClassLoader());
Class.forName("com.android.mms.util.ContactInfoCache", true, c)
.getMethod("init", Context.class).invoke(null, context);
Class.forName("com.android.mms.transaction.MessagingNotification", true, c)
.getMethod("updateAllNotifications", Context.class).invoke(null, context);
This is obviously a very bad idea for several reasons but I can't think of another way to do what you described.
Could you trigger a PUSH notification after the SMS?
Thread: Does Android support near real time push notification?
Maybe you should replace
content://sms
with
content://sms/inbox

Categories

Resources