Android Java: Get Open WiFi-Networks - java

I want to make an app that is automatically connecting to Open Networks (so no password). I know you can scan with wifi.startScan() and wifi.getScanResults. But how can I save all these Network Names?
So I can connect to them with:
String networkSSID = "test";
String networkPass = "pass";
WifiConfiguration conf = new WifiConfiguration();
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
Sorry, I'm really a nooby.

Do you mean that you just want to store the strings for the SSID and keys so that you can easily restore and connect later?
The easiest way is to use SharedPreferences to store any data.
Here is a a tutorial by slidenerd that is very easy to follow.
https://www.youtube.com/watch?v=riyMQiHY3V4
I'm a noob too, so whenever I have questions, I head straight to slidenerd or thenewboston on youtube and then start digging through tech documentation once I have a basic understanding.

Filter out the Open networks.
Use this method to check if a network is open or not
private boolean isProtectedNetwork(String capability){
return (capability.contains("WPA") ||
capability.contains("WEP") ||
capability.contains("WPS")
);
}
Then iterate through all the network lists and get the all open networks.
private void getAllOpenNetworks(List<ScanResult> allNetworks){
List<ScanResult>openNetworks = new ArrayList<ScanResult>();
for(ScanResult network : allNetworks){
if(!isProtectedNetwork(network.capabilities)){
openNetworks.add(network);
}
}
}
Useful Resource:
You can find more related solutions on My Github Repository

Related

How to filter ble devices by name using regex?

I'm working on a ble project. I need to display the available devices which has "kdd_" in the beginning. I'm not much familiar with regex. Anyone knows how to filter devices using regex? Any other possible solution for filtering devices is also fine. I have attached the code for ble scan code below.
final BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
final ScanSettings settings = new ScanSettings.Builder()
.setLegacy(false)
.setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).setReportDelay(1000).setUseHardwareBatchingIfSupported(false).build();
final List<ScanFilter> filters = new ArrayList<>();
filters.add(new ScanFilter.Builder().build());
scanner.startScan(filters, settings, scanCallback);
tl;dr You can't.
Longer answer: The ScanFilter does list a couple of attributes that can be filtered, among others "Name of remote Bluetooth LE device."
If you look at the public boolean matches(ScanResult scanResult) method you can see
// Local name match.
if (mDeviceName != null && !mDeviceName.equals(scanRecord.getDeviceName())) {
return false;
}
aka if you want to filter on the name you can only do a full name match. For other fields there are partial matches possible, but they are then compared using e.g. BitUtils.maskedEquals, not any regex logic.
You cannot subclass the ScanFilter to create your own filter logic because the class is final.
That means the logic is not there and you cannot add it => you cannot filter based on regex.

Vaadin-14: checking if client is a mobile device

i am developing a webapp and i want to separate UIs for desktops and mobile devices, so i want to check wether the client using my app is a mobile device or not.
I tried to look online for official documentation or vaadin forum , but i couldn't find any useful information, since almost all of the solutions proposed in those answers are not implementable any more (the methods were removed).
You can use VaadinSession.getCurrent().getBrowser() to see if your client is a phone or not.
public boolean isMobileDevice() {
WebBrowser webBrowser = VaadinSession.getCurrent().getBrowser();
return webBrowser.isAndroid() || webBrowser.isIPhone() || webBrowser.isWindowsPhone();
}
If anyone is interested i found a way around CSS, even if i think it's a bit limited.
I used HttpServletRequest, VaadinService and VaadinServletRequest: I basically checked if words like "Mobile", "iPhone" or "Android" are in the request.
It's not an elegant solution, but it works. This is the code:
public static boolean isPhone(HttpServletRequest request) {
String url = request.getHeader("User-Agent");
return (url.contains("iPhone") || url.contains("Android"));
}

Android BLE. Is it possible to use a regex as a filter to scan for Mac Address?

I have to build an App that scans for BLE devices, and return it's data.
The devices won't show on scan, unless I use a filter.
UUID is not an option, and the device does not broadcast it's name (It shows N/A when scanned with nrfConnect.
I am trying to scan it by MAC Address. BUT, I do not know the MAC Addresses, since it can be any device of it's kind, so the App won't previously know the MAC Address of the device.
I already know that the device have a prefix on it's Address which is F8:36:9B. The thing is the suffix. How can I (and if it is possible to) make a regex to pass as a parameter to find all possible matches of the Device MAC Address?
The regex per se, I have, ([A-Fa-f0-9]{2}:){2}[A-Fa-f0-9]{2}, which I got from Android Bluetooth ScanFilter Partial String Matching.
I just don't know how to implement it on the scanFilter.
ScanFilter filterMac = new ScanFilter.Builder().setDeviceAddress(/**THE_SUFIX_AND_REGEX*/).build();
Is it possible? If it is, then how?
Everything I tried, I get this error:
Error: invalid device address
I have tried generating all the possible matches using for loops and saving it to an ArrayList, and then adding it to the list of filters, but I get an OutOfMemoryException, since the result is over 16million possibilities.
Not possible with filters. You must filter yourself...like you did already
After a lot of struggle, I found a solution to my problem.
It does not answer the question per se, i.e. if it's possible to use a regex as a filter to scan for MAC Address.
But, I managed to properly scan for the devices I needed.
This is what I did:
First of all, I made a filter by name. Yes, name, the device have no name. So I had to think... What if, I filter by name, to scan for devices whose name == null?
private List<ScanFilter> scanFilters() {
List<ScanFilter> list = new ArrayList<ScanFilter>();
ScanFilter scanFilterName = new ScanFilter.Builder().setDeviceName(null).build();
list.add(scanFilterName);
return list;
}
Well, it worked! But, it returned me not only the devices I needed, but tons of other devices, alongside with them. It was a mess in my scan, so how to refine the filter to give me only those ones I needed?
The second filter (which wasn't on the scanFilters()method above, it was on the scanResult), was by MAC Address prefix (which is the same for my devices).
private Set<String> btDevice = new LinkedHashSet<String>();
private ScanCallback scanCallback = new ScanCallback() {
#Override
public void onScanResult(int callbackType, ScanResult result) {
BluetoothDevice device = result.getDevice();
ScanRecord record = result.getScanRecord();
byte[] dataByteArray = record.getBytes();
if (device.getAddress().startsWith("F8:36:9B")) {
btDevice.add(device.getAddress());
}
}
};
And voilá, I got a scan with only the devices I needed.
I still want to know if the main question is possible, i.e. if we can use a regex on the scanFilter(), to filter a range of something (in this case, MAC Address). So, if someone have a solution to this, please feel free to answer.
Thanks!

How to retrieve exposed content providers of an installed application?

I am trying to extract all exported content providers from installed application using the following code. But for every application, this returns zero. Though, when I check the same with ADB, the application lists all exposed content providers and their URIs. Do I need any permission to extract? Could someone please guide me on this? I am quite new to android.
List<ProviderInfo> returnList = new ArrayList<ProviderInfo>();
ProviderInfo[] prov = getPackageManager().getPackageInfo(packageName, 0).providers;
if (prov != null)
{
returnList.addAll(Arrays.asList(prov));
}
int count1 = returnList.size();

Reading Gmail mails using android SDK

I want to read Gmail mails in my own android app. Is there anyway to do it using android sdk? If not, what are the other options? parsing gmail atom?
I ask and answer that question here.
You need Gmail.java code (in the question there are a link) and you must understand that you shouldn't use that undocumented provider
Are there any good short code examples that simply read a new gmail message?
It's possible using the GMail API, here are some steps I found helpful.
Start with the official sample to get the GMailAPI started, see here
When following the instructions I found it helpful to read about the app signing here in order to get Step1+2 in the sample right.
With the sample running you can use the information here to access messages. You can e.g. replace the implementation in MakeRequestTask.getDataFromApi
Be sure to add at least the read-only scope for proper permissions. In the sample the scopes are defined in an array:
private static final String[] SCOPES = { GmailScopes.GMAIL_LABELS, mailScopes.GMAIL_READONLY };
My intention was to read all subjects. I used the following code (which is the adapted getDataFromApi method from the official sample):
private List<String> getDataFromApi() throws IOException {
// Get the labels in the user's account. "me" referes to the authentized user.
String user = "me";
List<String> labels = new ArrayList<String>();
ListMessagesResponse response = mService.users().messages().list(user).execute();
for (Message message : response.getMessages()) {
Message readableMessage = mService.users().messages().get(user, message.getId()).execute();
if (readableMessage.getPayload() != null) {
for (MessagePartHeader header : readableMessage.getPayload().getHeaders()) {
if (header.getName().compareToIgnoreCase("Subject") == 0) {
labels.add(header.getValue());
}
}
}
}
return labels;
}

Categories

Resources