First of all, I'd just like to point out that I'm a junior dev breaking new ground here for myself. I'm trying to send an ADB command to my Oculus to change the texture quality so that I don't have to hook up my Oculus to my PC every time with SideQuest. I've had no luck and there's no documentation that I can find to do this. Here's the code I have...
Retrieving the correct endpoint
private static UsbEndpoint getCorrectEndpoint(UsbDevice device, boolean outEndpoint) {
UsbEndpoint result = null;
//get interfaces
ArrayList<UsbInterface> usbInterfaceArrayList = new ArrayList<>();
for (int i = 0; i < device.getInterfaceCount(); i++) {
usbInterfaceArrayList.add(device.getInterface(i));
}
//get endpoints from those interfaces
UsbEndpoint endpointOut = null;
UsbEndpoint endpointIn = null;
for (int i = 0; i < usbInterfaceArrayList.get(0).getEndpointCount(); i++) {
if (usbInterfaceArrayList.get(0).getEndpoint(i).getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
if (usbInterfaceArrayList.get(0).getEndpoint(i).getDirection() == UsbConstants.USB_DIR_OUT) {
endpointOut = usbInterfaceArrayList.get(0).getEndpoint(i); //If this Endpoint is XFER_BULK and DIR_OUT
} else {
endpointIn = usbInterfaceArrayList.get(0).getEndpoint(i); //If this EndPoint is XFER_BULK and DIR_IN
}
}
}
if (outEndpoint) { //Check the parameter to see which endpoint the user wants
result = endpointOut;
} else {
result = endpointIn;
}
return result;
}
Sending the data
public static void setOculusTexture(double textureWidth, Context context, Intent intent, TextView textView) {
double textureHeight = textureWidth * 1.0997;
boolean forceClaim = true;
int timeout = 0;
String textureString = "adb shell " + OculusADBConstants.OCULUS_TEXTURE_ADB_BASE + "Width " + textureWidth +
" && " + OculusADBConstants.OCULUS_TEXTURE_ADB_BASE + "Height " + textureHeight;
byte[] textureStringBytes = textureString.getBytes();
if (getOculusDeviceFromUSB(context) != null) { //If there is an Oculus connected
UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
UsbDevice usbDevice = (UsbDevice) getOculusDeviceFromUSB(context);
UsbDeviceConnection usbDeviceConnection = usbManager.openDevice(usbDevice);
usbDeviceConnection.claimInterface(usbDevice.getInterface(0), forceClaim);
int cmdBulkTransfer = usbDeviceConnection.bulkTransfer(getCorrectEndpoint(usbDevice, true), textureStringBytes, textureStringBytes.length, timeout);
int controlTransferResult = usbDeviceConnection.controlTransfer(UsbConstants.USB_DIR_OUT, 1,
0, 0, textureStringBytes, textureStringBytes.length, timeout);
textView.append(textureString + " data sent to device..." + "\n");
textView.append("cmdBulkTransfer Result = " + (cmdBulkTransfer) + "\n");
textView.append("ControlTransfer Result = " + controlTransferResult);
} else { //If there isn't an Oculus connected
Toast.makeText(context, "No Oculus detected. Is it plugged into your phone?", Toast.LENGTH_LONG).show();
}
}
The bulk transfer returns an actual number while the controlled transfer returns -1. I'm fairly sure that I need to somehow initiate an ADB request from my phone, but I have no idea how to do that. I assume this because when I connect my phone to the Oculus, and use the BugJaeger app, the BugJaeger app somehow makes the Oculus prompt with allowing USB Debugging dialog. My app does not cause that to happen. What am I doing wrong?
Related
I need to check all the available wifi and then connect to a different one which I am currently connected to.
Sample - device is connect to AP01
private void switchAP(String ssid, String pass)) {
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + ssid + "\"";
if(!TextUtils.isEmpty(pass))
conf.wepKeys[0] = "\"" + pass + "\"";
conf.preSharedKey = "\"" + pass + "\"";
conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
WifiManager wifiManager = (WifiManager) SyncActivity.this.getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(conf);
// List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
List<ScanResult> list = wifiManager.getScanResults();
for (ScanResult i : list) {
Log.w(TAG, "WIFI LIST > " + i.SSID);
if (i.SSID != null && i.SSID.equals(ssid)) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true); //>i.networkId Does not work - only in List<WifiConfiguration>…
wifiManager.reconnect();
logMsg("WiFi connection switched to " + ssid);
break;
}
}
}
Then I call
switchAP("AP02", ""));
i can not connect because this i.networkId does not seem to work when I use ScanResult rather than WifiConfiguration.
Has anyone came across this ?
Thanks guys.
The reason why I am not using List is because the device has never been connect to this SSOD before.
and List only returns all the previews connected network.
You need to use WifiConfiguration.Status.ENABLED.
An example code is written below for you:
public void WIFI_CONNECT_WIFI (String ssid, String password)
{
WifiManager wifi_manager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wifi_configuration = new WifiConfiguration();
wifi_configuration.SSID = String.format("\"%s\"", ssid);
wifi_configuration.preSharedKey = String.format("\"%s\"", password);
if (WIFI_NETWORK_EXIST (wifi_configuration) == true) {
wifi_configuration.status = WifiConfiguration.Status.ENABLED;
wifi_configuration.priority = 40;
}
else {
boolean result = wifi_manager.getConfiguredNetworks().add(wifi_configuration);
if (result == false) {
return;
}
wifi_configuration.status = WifiConfiguration.Status.ENABLED;
wifi_configuration.priority = 40;
wifi_configuration.networkId = wifi_manager.addNetwork(wifi_configuration);
}
wifi_manager.enableNetwork(wifi_configuration.networkId, true);
wifi_manager.saveConfiguration();
}
-
public boolean WIFI_NETWORK_EXIST (WifiConfiguration wifi_configuration)
{
WifiManager wifi_manager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
for (WifiConfiguration config : wifi_manager.getConfiguredNetworks()) {
if (wifi_configuration.SSID.equals(config.SSID)) {
wifi_configuration.networkId = config.networkId;
return true;
}
}
return false;
}
And by the way, if there's a possibility to get SSID null, you are always gonna crash on your if comparison when SSID comes with null a value since you are trying to use a member function of a null value.
if (i.SSID != null && i.SSID.equals(ssid)) {
// ...
}
expected stack trace is: "bla bla bla trying to use String.equals(String) on a null value."
You should do an inner or different if after null comparison for equals(...) comparison.
I would like to build an app, that checks all the available WiFi networks,
If a network's SSID matches a search key then connect to that network, if two networks match then connect to the one with the higher signal strength.
e.g. SearchKey = "Open";
here is the code to check check all the wifi names :
if (networkInfo.isConnected()) {
ArrayList<ScanResult> mItems = new ArrayList<ScanResult>();
List<ScanResult> results = wifiManager.getScanResults();
int size = results.size();
HashMap<String, Integer> signalStrength = new HashMap<String, Integer>();
try {
for (int i = 0; i < size; i++) {
ScanResult result = results.get(i);
if (!result.SSID.isEmpty()) {
String key = result.SSID + " " + result.capabilities;
Log.i("TAG", "ssid: " + result.SSID + " | level: " + result.level);
}
Then I would need to
Arrays.asList(mItems).contains("Open")
I am stuck here, How to do a proper check if the keyword "Open" exits, if so, then get the whole name and use below. ?
online sample how to conenct.
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = String.format("\"%s\"", ssid);
wifiConfig.preSharedKey = String.format("\"%s\"", key);
WifiManager wifiManager = (WifiManager).getSystemService(WIFI_SERVICE);
//remember id
int netId = wifiManager.addNetwork(wifiConfig);
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
UPDATE CODE ------------------------------ - --------------------------------- - -- ---------------------------------------
How to check, This works But Now I need to implement Search by signal strength
if (!result.SSID.isEmpty()) {
String key = result.SSID + " " + result.capabilities;
Log.i("TAG", "ssid: " + result.SSID + " | level: " + result.level);
if(result.SSID.contains("Open")) {
String useSSID = result.SSID;
Log.w(TAG, "useSSID => " + useSSID);
connectToWifi(MainActivity.this, useSSID);
break;
}
else { Log.e(TAG, "NO result contains"); }
Now how can I query my :
ArrayList<ScanResult> mItems = new ArrayList<ScanResult>();
if (!signalStrength.containsKey(key)) {
signalStrength.put(key, i);
mItems.add(result);
} else {
int position = signalStrength.get(key);
ScanResult updateItem = mItems.get(position);
if (calculateSignalStength(wifiManager, updateItem.level) > calculateSignalStength(wifiManager, result.level)) {
mItems.set(position, updateItem);
}
}
if(mItems.contains("Open")) {
String useSSID = mItems #how to the name SSID name from mItems ???
Log.w(TAG, "useSSID => " + useSSID);
connectToWifi(MainActivity.this, useSSID);
break;
}
else { Log.e(TAG, "NO result contains"); }
=== Now my question is how to the SSID name from mItems ?
Thanks guys for your help.
Here is a snippet for the same:
public string getValidSSID()
{
List<ScanResult> results = wifiManager.getScanResults();
HashMap<String,ScanResult> distinctNetworks = new HashMap<String, ScanResult>();
for(ScanResult scanResult : results)
{
if(scanResult.SSID.contains("Open"))
{
if(!distinctNetworks.containsKey(scanResult))
{
distinctNetworks.put(scanResult.SSID, scanResult);
}
else
{
if(WifiManager.compareSignalLevel(scanResult.level, distinctNetworks.get(scanResult.SSID).level)>0)
{
distinctNetworks.put(scanResult.SSID, scanResult);
}
}
}
}
Set<String> networks = distinctNetworks.keySet();// This will only contain one key which will be ths ssid with the max strength containing "open" in SSID
for (String s : networks) {
return s;
}
}
I'm using the Twitter4j library to retrieve tweets, but I'm not getting nearly enough for my purposes. Currently, I'm getting that maximum of 100 from one page. How do I implement maxId and sinceId into the below code in Processing in order to retrieve more than the 100 results from the Twitter search API? I'm totally new to Processing (and programming in general), so any bit of direction on this would be awesome! Thanks!
void setup() {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("xxxx");
cb.setOAuthConsumerSecret("xxxx");
cb.setOAuthAccessToken("xxxx");
cb.setOAuthAccessTokenSecret("xxxx");
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
Query query = new Query("#peace");
query.setCount(100);
try {
QueryResult result = twitter.search(query);
ArrayList tweets = (ArrayList) result.getTweets();
for (int i = 0; i < tweets.size(); i++) {
Status t = (Status) tweets.get(i);
GeoLocation loc = t.getGeoLocation();
if (loc!=null) {
tweets.get(i++);
String user = t.getUser().getScreenName();
String msg = t.getText();
Double lat = t.getGeoLocation().getLatitude();
Double lon = t.getGeoLocation().getLongitude();
println("USER: " + user + " wrote: " + msg + " located at " + lat + ", " + lon);
}
}
}
catch (TwitterException te) {
println("Couldn't connect: " + te);
};
}
void draw() {
}
Unfortunately you can't, at least not in a direct way such as doing
query.setCount(101);
As the javadoc says it will only allow up to 100 tweets.
In order to overcome this, you just have to ask for them in batches and in every batch set the maximum ID that you get to be 1 less than the last Id you got from the last one. To wrap this up, you gather every tweet from the process into an ArrayList (which by the way should not stay generic, but have its type defined as ArrayList<Status> - An ArrayList that carries Status objects) and then print everything! Here's an implementation:
void setup() {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("xxxx");
cb.setOAuthConsumerSecret("xxxx");
cb.setOAuthAccessToken("xxxx");
cb.setOAuthAccessTokenSecret("xxxx");
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
Query query = new Query("#peace");
int numberOfTweets = 512;
long lastID = Long.MAX_VALUE;
ArrayList<Status> tweets = new ArrayList<Status>();
while (tweets.size () < numberOfTweets) {
if (numberOfTweets - tweets.size() > 100)
query.setCount(100);
else
query.setCount(numberOfTweets - tweets.size());
try {
QueryResult result = twitter.search(query);
tweets.addAll(result.getTweets());
println("Gathered " + tweets.size() + " tweets");
for (Status t: tweets)
if(t.getId() < lastID) lastID = t.getId();
}
catch (TwitterException te) {
println("Couldn't connect: " + te);
};
query.setMaxId(lastID-1);
}
for (int i = 0; i < tweets.size(); i++) {
Status t = (Status) tweets.get(i);
GeoLocation loc = t.getGeoLocation();
String user = t.getUser().getScreenName();
String msg = t.getText();
String time = "";
if (loc!=null) {
Double lat = t.getGeoLocation().getLatitude();
Double lon = t.getGeoLocation().getLongitude();
println(i + " USER: " + user + " wrote: " + msg + " located at " + lat + ", " + lon);
}
else
println(i + " USER: " + user + " wrote: " + msg);
}
}
Note: The line
ArrayList<Status> tweets = new ArrayList<Status>();
should properly be:
List<Status> tweets = new ArrayList<Status>();
because you should always use the interface in case you want to add a different implementation. This of course, if you are on Processing 2.x will require this in the beginning:
import java.util.List;
Here's the function I made for my app based on the past answers. Thank you everybody for your solutions.
List<Status> tweets = new ArrayList<Status>();
void getTweets(String term)
{
int wantedTweets = 112;
long lastSearchID = Long.MAX_VALUE;
int remainingTweets = wantedTweets;
Query query = new Query(term);
try
{
while(remainingTweets > 0)
{
remainingTweets = wantedTweets - tweets.size();
if(remainingTweets > 100)
{
query.count(100);
}
else
{
query.count(remainingTweets);
}
QueryResult result = twitter.search(query);
tweets.addAll(result.getTweets());
Status s = tweets.get(tweets.size()-1);
firstQueryID = s.getId();
query.setMaxId(firstQueryID);
remainingTweets = wantedTweets - tweets.size();
}
println("tweets.size() "+tweets.size() );
}
catch(TwitterException te)
{
System.out.println("Failed to search tweets: " + te.getMessage());
System.exit(-1);
}
}
From the Twitter search API doc:
At this time, users represented by access tokens can make 180 requests/queries per 15 minutes. Using application-only auth, an application can make 450 queries/requests per 15 minutes on its own behalf without a user context.
You can wait for 15 min and then collect another batch of 400 Tweets, something like:
if(tweets.size() % 400 == 0 ) {
try {
Thread.sleep(900000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Just keep track of the lowest Status id and use that to set the max_id for subsequent search calls. This will allow you to step back through the results 100 at a time until you've got enough, e.g.:
boolean finished = false;
while (!finished) {
final QueryResult result = twitter.search(query);
final List<Status> statuses = result.getTweets();
long lowestStatusId = Long.MAX_VALUE;
for (Status status : statuses) {
// do your processing here and work out if you are 'finished' etc...
// Capture the lowest (earliest) Status id
lowestStatusId = Math.min(status.getId(), lowestStatusId);
}
// Subtracting one here because 'max_id' is inclusive
query.setMaxId(lowestStatusId - 1);
}
See Twitter's guide on Working with Timelines for more information.
I have to read data(signals) from a device which sends signals continously to USB port and display it on monitor.
As POC I tried reading data from USB flash drive connected to my system. I am using JUSB library for windows. But, as I change ,my device driver settings as described in below doc for JUSB, my device configurations disappear( I get null for device.getConfiguration();).
http://www.steelbrothers.ch/jusb/ -- documents-->appendix D
Can anyone please help me to figure out where I am going wrong or suggest me some other good API to read data from USB device using JAVA.
Code is below,
try{
Device device = null;
DeviceImpl dev;
for(int k=0; k < busses.length ; k++){
System.out.println("\n\nBus[ " + ((USB)busses[k]).getBusNum() + " ] ");
for(int i = 0; i < 5; i++){
dev = (DeviceImpl)busses[k].getDevice(i);
device = busses[k].getDevice(i);
System.out.print(" [ " + i + " ] : ");
if(dev != null){
if(dev.getAddress() == 0) System.out.println(" [ROOT] numOfPort:" + dev.getNumPorts()+" Address:" + dev.getAddress());
else {
if(dev.getNumPorts() > 0) System.out.println(" [EXTERNAL HUB] numOfPort:" + dev.getNumPorts()+" Address:" + dev.getAddress());
else System.out.println(" [USB DEVICE] on Port "+dev.getHubPortNum() + " Address : " + dev.getAddress());
System.out.println(" uniqueID : " + dev.getUniqueDeviceID());
System.out.println(" driverKeyName : " + dev.getDriverKeyName());
System.out.println(" friendlyDeviceName: " + dev.getFriendlyDeviceName());
if (dev instanceof Device) System.out.print(" Object Type : Device");
if (dev instanceof DeviceImpl) System.out.print(", DeviceImpl");
if (dev instanceof JUSB) System.out.println(", JUSB");
if (dev instanceof NonJUSB) System.out.println(", NonJUSB");
System.out.println("***************confoig: "+dev.configuration);//getConfiguration(0));
boolean brk = false;
StringTokenizer uniqueNameValPairs = new StringTokenizer(dev.getUniqueDeviceID(),"&");
while(uniqueNameValPairs.hasMoreTokens()){
StringTokenizer strToken = new StringTokenizer(uniqueNameValPairs.nextToken(),"_");
while(strToken.hasMoreTokens()){
if(strToken.nextToken().equals("03f0")){
System.out.println("breaking");
readData(dev);
brk = true;
break;
}
}
}
static void readData(DeviceImpl device){
try{
if (device != null)
{
// Obtain the current Configuration of the device and the number of
// Interfaces available under the current Configuration.
Configuration config = device.getConfiguration();
if(null == config)
config = device.configuration;
System.out.println("config: "+config);
int total_interface = config.getNumInterfaces();
// Traverse through the Interfaces
for (int k=0; k<total_interface; k++)
{
// Access the currently Interface and obtain the number of
// endpoints available on the Interface.
Interface itf = config.getInterface(k, 0);
int total_ep = itf.getNumEndpoints();
// Traverse through all the endpoints.
for (int l=0; l<total_ep; l++)
{
// Access the endpoint, and obtain its I/O type.
Endpoint ep = itf.getEndpoint(l);
String io_type = ep.getType();
boolean input = ep.isInput();
System.out.println("ep.getInputStream(): "+ep.getInputStream());
// If the endpoint is an input endpoint, obtain its
// InputStream and read in data.
if (input)
{
InputStream in;
System.out.println("ep.getInputStream()111: "+ep.getInputStream());
BufferedReader brIn = new BufferedReader(new InputStreamReader(ep.getInputStream()));
System.out.println("**************Input stream: "+brIn);
String inStr = null;
while( (inStr = brIn.readLine()) != null){
System.out.println(inStr);
}
// Read in data here
// in.close();
}
// If the Endpoint is and output Endpoint, obtain its
// OutputStream and write out data.
else
{
OutputStream out;
out = ep.getOutputStream();
System.out.println("**************Output stream: "+out);
// Write out data here.
out.close();
}
}
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
//System.exit(0);
}
}
I am using following code to connect with WPA2 in android (I can connect with WEP and WPA). But I am getting only 'Scanning' status. And I am unable to connect with WPA2 network. Can you tell me what changes I need to make this code relevant with wpa2 WiFi.
private boolean saveWepConfigAndEnableNetwork(String ssid, String pass) {
isAlreadyPresend = false;
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "\"" + ssid + "\""; // IMP! This should be in Quotes!!
wc = checkPreviousConfiguration(wc);
wc.hiddenSSID = true;
wc.status = WifiConfiguration.Status.DISABLED;
wc.priority = 40;
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
wc.preSharedKey = "\"" + pass + "\"";
wc.wepKeys[0] = "\"" + pass + "\""; // This is the WEP Password
wc.wepTxKeyIndex = 0;
boolean res1 = wifi.setWifiEnabled(true);
int res = 0;
if(isAlreadyPresend){
res = wifi.addNetwork(wc);
}else{
res = wifi.updateNetwork(wc);
}
Log.d("WifiPreference", "add Network returned " + res);
boolean es = wifi.saveConfiguration();
Log.d("WifiPreference", "saveConfiguration returned " + es);
boolean b = wifi.enableNetwork(res, true);
Log.d("WifiPreference", "enableNetwork returned " + b);
return b;
}
// Check if this SSID is already stored. If it is, return that
// configuration.
// If not, return the configuration being tested.
public WifiConfiguration checkPreviousConfiguration(WifiConfiguration wc) {
List<WifiConfiguration> configs = wifi.getConfiguredNetworks();
for (WifiConfiguration config : configs) {
if (config.SSID.equals(wc.SSID)){
isAlreadyPresend = true;
return config;
}
}
return wc;
}
Here is the code which worked for me to connect with WPA2
// Adding a WPA or WPA2 network
public static void changeNetworkWPA(WifiManager wifiManager, String ssid, String password) {
WifiConfiguration config = changeNetworkCommon(ssid);
// Hex passwords that are 64 bits long are not to be quoted.
config.preSharedKey = quoteNonHex(password, 64);
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
config.allowedProtocols.set(WifiConfiguration.Protocol.WPA); // For WPA
config.allowedProtocols.set(WifiConfiguration.Protocol.RSN); // For WPA2
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
updateNetwork(wifiManager, config);
}
Code: From Zxing library