Reconnect to a device after device crashed - java

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.

Related

Android Webview ignore onReceivedError when app is in background / minimized

I have an Android app that serves content via webview. I've implemented a network connectivity alert so if my users are in a location where they can't get internet, I show a message. This takes place inside the onReceivedError method. It works as expected; if I disable all internet connections, I get a warning message. However, even when internet connection exists, the warning message still shows. If I minimize my app, open the native browser and visit 8-10 links, when I switch back to my app, I am given the network warning even though the device is connected to the network.
This only happens on one of my 6 test devices (my current phone, a Samsung Galaxy S10e. The problem does not occur on 5 different Android devices, both newer and older. I'm just looking for a way to ignore the warning when the app is in the background state. I've searched for an easy way to do this, but I haven't found anything simple that doesn't involve writing a new class.
How can I write a conditional statement to do nothing when the onReceivedError method is triggered while the app is in the background? Here is a portion of my code, along with what I would like to have happen:
#override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
// the part I need help with
if ("app in background"){
// do nothing
} else {
// this is where I have employed my notification, a snackbar
}
}
Thank you for your help!

Making API calls while app is killed/not actively running

So I am making an app that has to check for some information, but I need to be able to do that while the app is killed/not actively running. So basically like Youtube's notifications or something. I am a beginner and watched some tutorials on FCM, which can send a notification while the app is killed, which is fine. However, I need to be able to periodically make API calls, check if a certain condition is true and send the notification if so (all of that while app is not running). I tried googling that and found nothing that can help me. So... any ideas? (No code included since I don't think it's relevant.)
If this condition only affects the display of notifications, you can simply check the condition when you receive a notification through FCM. Here's an example :
class FCMService : FirebaseMessagingService()
{
override fun onMessageReceived(message: RemoteMessage)
{
val myCondition = getConditionFromAPI()
if (myCondition) {
showNotification(message)
}
}
}
Use AlarmManager to request periodic runs of your code. It may be set up to broadcast a message time to time to your receiver (declared in AndroidManifest.xml or in your code).
It will work while device is on. To continue after reboot, you have to set up a receiver for Intent.ACTION_BOOT_COMPLETED and register it in your code (not in AndroidManifest.xml, as it no longer works in latest versions of Android).

How to connect with a Bluetooth device without pairing

I'm currently developping my first app in java.
This app requires a direct connection with my BL652, which doesn' t accept any kind of pairing option. That being said, I'm stuck with the code below, which still tries to pair with my bluetooth device instead of just connecting. Therefore, I wanted to know what am i supposed to do in order to make a connection that doesnt require pairing.
Thanks in advance for any response.
String mac_address = "DA:72:21:29:0F:F0";
private static final UUID MY_UUID = UUID.fromString("E54B0002-67F5-479E-8711-B3B99198CE6C");
lvNewDevices.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
BluetoothDevice device = (BluetoothDevice) mBluetoothAdapter.getRemoteDevice(mac_address);
try {
socket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
socket.connect();
} catch (IOException e) {
e.printStackTrace();
}
I'm not sure about connection to external hardware but it's definetly possible to connect 2 phones without pairing. I made it using Android Nearby Connections . And yeah it's basically working with BLE. As far as I know it's possible to connect to RaspberryPi using that API.
Bluetooth devices can be made to communicate with each other using master-slave configuration. I first tried with two BLE-HC05s, made one as master and the other as slave, and as expected the master is able to send data, slave is able to receive data, that can be seen using the Serial Monitor in Arduino IDE. I have developed a similar app, which connects to a BLE-HC05 device, sends and receives data. But since you are trying to get data or send data to the App on your phone, Android will not support such unpaired anonymous communications. If you are having trouble with connection or pairing Bluetooth device and your app, I may help you with that. But as far as Bluetooth communication without pairing is concerned, you may not be able to find a way.
Edit: The code to connect any bluetooth device, like BLE-HC06 as you have mentioned, is 1234 (Only if you have not changed it)

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

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".

Complete CONNECTIVITY_SERVICE test

My application connects to the internet onCreate, it does this in an AsyncTaks class and all works fine. I have some error checking in place to make sure there is internet available and works great if I say put my phone on Flight Mode.
My problem is when I’m on WIFI, where I live the WWW drops out from time to time but the phone still thinks it’s connected. E.g.. the phone is still connected to the WIFI dongle but the WIFI dongle is not connected to the WWW, so when the application opens and tries to connect it gets an error and I get a force close.
How can I do a complete internet connection check onCreate that will cover all bases???
Cheers,
Mike.
One possibility is just to handle the error that you are getting in a better manner. You are getting a force close right now because (I assume) you are getting a RuntimeException from your application. Handling the exceptions and putting up proper messaging to your user might be adequate.
Another way is just make one (or a couple) connections to some high available servers to see if it works. For example, something like the following should work:
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://www.google.com/");
try {
HttpResponse response = client.execute(request);
txtResult.setText(HttpHelper.request(response));
// internet is working
} catch(Exception ex) {
// internet is not working
txtResult.setText("Internet ENOWORK");
}
If you post the exception/error that you are getting and comment on my answer I can edit it with more specifics.

Categories

Resources