Set UsbDeviceListener javax.usb/usb4java - java

I tried to set the UsbDeviceListener but nothing happens when I disconnect the device nor when I create a new file on the device.
Can someone tell me where is the problem in my code?
public static void listPeripherique(UsbHub hub) {
List perepheriques = hub.getAttachedUsbDevices();
Iterator iterator = perepheriques.iterator();
while (iterator.hasNext()) {
UsbDevice perepherique = (UsbDevice) iterator.next();
perepherique.addUsbDeviceListener(new UsbDeviceListener() {
#Override
public void usbDeviceDetached(UsbDeviceEvent arg0) {
System.out.println("connect " + arg0);
}
#Override
public void errorEventOccurred(UsbDeviceErrorEvent arg0) {
System.out.println("disconect " + arg0);
}
#Override
public void dataEventOccurred(UsbDeviceDataEvent arg0) {
System.out.println("new data on " + arg0);
}
});
}
if (perepherique.isUsbHub()) {
listPeripherique((UsbHub) perepherique);
}
}
Edit: It's work only for usb Detached.

Do you mind giving use more details on your environment (Linux, Win, OSX) and what kind of USB device (Stick, Gamepad, DVD-Burner, Dongle, iP(a|o)d, iPhone, Smartphone)? However, if you use Windows, it seems the driver is out-of-date: JSR80: This is an ABANDONED Windows implementation. it does not work, it needs a kernel driver.
Keep us posted on your project and progress.
Edit
I tried it with my Logitech Wingman controller and I can confirm that only usbDeviceDetached events are raised. I think the driver is really outdated :(

Sounds like it is working correctly.
Your code iterates once over all currently connected devices and registers a device listener on it. When the device is disconnected then the usbDeviceDetached event is triggered. The other two events are triggered after data is transfered from or to the device by your own program or when an error occurred when your program tried to communicate with the device.
javax-usb can't monitor data transfers made by other programs. Maybe this is what you expected? For this you need a special USB monitor tool like usbmon on Linux.

Related

Android Studio: is possible to have hotspot with internet access? (Local Only Hotspot)

I'm new to Android Studio and I'm using the functionality of Local Only Hotspot to turn on and off programmatically the hotspot (found this two post for reference: How to turn on/off wifi hotspot programmatically in Android 8.0 (Oreo),How to turn on Wifi-Hotspot programmatically on Android >= 7.1 (including sharing the internet access)? .
private void turnOnHotspot() {
wifiManager.startLocalOnlyHotspot(new
WifiManager.LocalOnlyHotspotCallback()
{
#Override
public void onStarted(WifiManager.LocalOnlyHotspotReservation reservation) {
super.onStarted(reservation);
hotspotReservation = reservation;
String key = hotspotReservation.getWifiConfiguration().preSharedKey;
String ussid = hotspotReservation.getWifiConfiguration().SSID;
System.out.println("KEY: "+ key);
System.out.println("USSID: "+ ussid);
currentConfig = hotspotReservation.getWifiConfiguration();
System.out.println("STARTED THE HOTSPOT");
}
#Override
public void onStopped() {
super.onStopped();
System.out.println("STOPPED THE HOTSPOT");
}
#Override
public void onFailed(int reason) {
super.onFailed(reason);
System.out.println("FAILED THE HOTSPOT");
}
}, new Handler());
}
private void turnOffHotspot() {
active = false;
if (hotspotReservation != null) {
hotspotReservation.close();
System.out.println("CLOSE HOTSPOT");
}
}
But from what I gather from other older posts and documentation, this method gives a local network without internet access and a random SSID and Password that cannot be personalised.
I need to connect only one device to this hotspot to share the mobile data (to have internet access), but I didn't find anything that could have help me. Is there another alternative?

How to connect paired bluetooth device on app startup in Android Studio?

Is there any way to automatically connect a specific device via Bluetooth LE on app startup?
I've been scrolling through stack overflow for the past few hours and have seen a number of similar questions, although majority are quite outdated and deal with reflections or other complex methods that I can't quite comprehend (these methods I've tried to implement, but not successfully, as I didn't really understand what was going on).
So far, I've managed to find the device by its friendly name, although I have no clue what to execute in that if statement. This is within my MainActivity:
protected void onCreate(Bundle savedInstanceState) {
...
if (bluetoothAdapter == null) {
Toast.makeText(getApplicationContext(),"Bluetooth not supported",Toast.LENGTH_SHORT).show();
} else {
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
if(pairedDevices.size()>0){
for(BluetoothDevice device: pairedDevices){
if (deviceName.equals(device.getName())) {
//Device found!
//Now how do I pair it?
break;
}
...
Assuming you've successfully identified the BlueToothDevice, you now need to connect to the GATT(Generic Attribute Profile), which allows you to transfer data.
Use the BlueToothDevice.connectGatt method. Using the first overload, the method takes in a Context , a boolean (false = directly connect, true = connect when available), and a BlueToothGhattCallback. The callback receives info from the device.
BlueToothGatt blueToothGatt = device.connectGatt(this, false, blueToothGattCallback);
An example to implement the callback:
BluetoothGattCallback blueToothGattCallback =
new BluetoothGattCallback()
{
#Override
public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
if(newState == BlueToothProfile.STATE_CONNECTED){
/* do stuff */
}
}
}
More details on the callbacks here.
Ended up scrolling through the source code for this app, particularly the SerialSocket, SerialService and SerialListener files which completely solved my problem.

How to save game data to the cloud using gdx-gamesvcs?

I want to implement Google Play Games Services in my game on the libgdx engine. I tried using gdx-gamesvcs for this. But I am having trouble saving data. I understood from the example that one value is being saved, not the entire state of the game. So I decided to check it out: save and load one value using gsClient.loadGameState and gsClient.saveGameState. I deliberately deleted the game data from the device. But as a result, not only the test value changed, but many others as well. I thought that the state of the entire game is being saved, but the values ​​obtained do not fit into the logic of the game and could not be obtained in it.
How should I use this tool and is it worth it at all, or is it better to use what libgdx itself offers?
Here is a piece of code:
if (gsClient.isSessionActive()) {
try {
gsClient.saveGameState("data", intToByteArray(testValue), 0, null);
} catch (UnsupportedOperationException unsupportedOperationException) {
}
if (gsClient.isSessionActive()) {
try {
gsClient.loadGameState("data", new ILoadGameStateResponseListener() {
#Override
public void gsGameStateLoaded(byte[] gameState) {
if (gameState != null) {
setTestValue(bytesToInt(gameState));
}
}
});
} catch (UnsupportedOperationException unsupportedOperationException) {
}
}
UPD
Yes, saving occurs both to the cloud and to the device, for saving to the device I use Preferences. I have a Google account login button in the game, it works, I have repeatedly seen this standard bar of my account level, which appears at the top when I log in. Everything is set up in the developer console too, I have an id for achievements and leaderboards. In code, I work with the client like this (In the create() method):
public IGameServiceClient gsClient;
if (gsClient == null) {
gsClient = new MockGameServiceClient(1) {
#Override
protected Array<ILeaderBoardEntry> getLeaderboardEntries() {
return null;
}
#Override
protected Array<String> getGameStates() {
return null;
}
#Override
protected byte[] getGameState() {
return new byte[0];
}
#Override
protected Array<IAchievement> getAchievements() {
return null;
}
#Override
protected String getPlayerName() {
return null;
}
};
}
gsClient.setListener(this);
gsClient.resumeSession();
Next is loading.
The exception is not caught, I removed it and everything works as before.
Well, libgdx offers no built-in cloud-save, it is hard to use it for that. :-)
You should in any case save to local AND to cloud, as the cloud is not very fast to load its state.
I can see no problem in your code besides the fact that you swallow an UnsupportedOperationException that is thrown if you did not activate cloud save feature. So the interesting question is: what happens if you don't swallow the exception, and did you intialize GpgsClient with cloud save enabled? Are you really logged in to Gpgs, and is the feature also activated in your developer console?
The main problem was that gameState was null, this arose due to the fact that you had to wait 24 hours after enabling the save function in the developer console, and the advice on clearing the memory of google play games on the test device did not help. After a while gameState began to pass the existing values, but I started having problems with the graphics flow, probably due to the asynchronous loading.

FileObserver not working on Android 6, alternative to detect screenshots on Android

I am trying to detect screenshots on Android app using fileObserver, fileObserver does not work on Android 6 as expected.
Here is the snippet which detects the screenshot:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screenshot_detection);
observer = new FileObserver(Environment.getExternalStorageDirectory() + File.separator + Environment.DIRECTORY_PICTURES
+ File.separator + "Screenshots" + File.separator, FileObserver.ALL_EVENTS) {
#Override
public void onEvent(int event, String path) {
if (event == FileObserver.CREATE) {
Log.i(TAG, "Screenshot detected # " + path);
}
}
};
observer.startWatching();
}
I observe that the same code works on Nexus 5 running 4.4.4 where as does not work (the onEvent is never triggered) on Nexus 5 running 6.0.1 though I have taken care of run-time permissions for API 23+.
I see a known issue with fileObserver for Android M, is there a better alternative for detecting screenshots?
I tried contentObserver, faced issues with it as well.
You can check if, the
com.android.systemui:screenshot
process is running or not. If this is running then there is good chance that screen shot was taken while the user was on your app.
Try this code block,
private void screenShotTaken(Activity activity) {
final Handler handler = new Handler();
final int delay = 3000;
final ActivityManager am = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
handler.postDelayed(new Runnable() {
public void run() {
List<ActivityManager.RunningServiceInfo> services = am.getRunningServices(200);
for (ActivityManager.RunningServiceInfo ar : services) {
if (ar.process.equals("com.android.systemui:screenshot")) {
Toast.makeText(activity, "Screenshot is taken!!", Toast.LENGTH_SHORT).show();
}
}
handler.postDelayed(this, delay);
}
}, delay);
}
This would be something which would keep on running with some delay and will give you enough chance to detect if the screenshot was taken or not. Also, you may like to check this git, where the whole code for the screenshot is there, I think it would take a just a little understanding how things are working.
Android 6.0 have extra security features, that is why it does not allow any application from unknown source to access any thing.
For the time being you can go the application manager then to your app and then go to permissions and then allow storage permission.
I hope this might help you now for the time being.

System.exit(0) in Android Wear Watchface?

I've read that using System.exit(0) is frowned upon when it comes to Java and Android, but so far I can find no alternative for what I'm trying to accomplish. To be clear, this is for a Watchface, which is simply a service extending CanvasWatchFaceService. I cannot call finish() in this case. I've also tried stopService and startService with no success.
The issue I'm trying to solve: It's well known that changing timezones on your device will not be reflected on a watchface unless it is restarted. In my testing, I found that System.currentTimeMillis() quite literally does not respond to timezone changes in Android Wear. The watchface must be restarted in order for it to show the correct time after a timezone change.
So I built a system with the following code:
private final BroadcastReceiver timeChangeReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (!restarting) {
if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
if (upSeconds >= 15) {
System.exit(0);
} else {
restarting = true;
int delay = ((15 - upSeconds) * 1000);
new CountDownTimer(delay, delay) {
#Override
public void onTick(long millisUntilFinished) { }
#Override
public void onFinish() {
System.exit(0);
}
}.start();
}
}
}
}
};
The delay is in case a user triggers a time zone change more frequently than 15 seconds at a time. Android Wear seems to detect system exits that are too frequent and replace the watchface with the "Simple" watchface.
It seems to work great, Android Wear automatically boots the watchface back up on exit. But I would eventually like to put this app on the Google Play Store, so I thought I should make sure I'm not playing with fire here.
I can't believe I went through all that work when the proper solution was so simple. Thanks ianhanniballake for the link!
After looking at the Analog Watchface Sample, I found that all I needed to do was use mCalendar.setTimeZone(TimeZone.getDefault());. In many places I was directly comparing the time in milliseconds fetched with long now = System.currentTimeMillis();, so I simply did a now = mCalendar.getTimeInMillis() to take care of that.
Now the watchface changes time properly when the timezone is changed. I guess the other watchfaces I downloaded did not properly handle this!

Categories

Resources