In Android, checking internet connection is like that.
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) activity
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
How about in Blackberry RIM?
p/s*: I am not asking for checking the type of connection or connection available.
For instance, to check only for BIS coverage:
boolean hasConnectivity = TransportInfo.isTransportTypeAvailable(TransportInfo.TRANSPORT_BIS_B) && TransportInfo.hasSufficientCoverage(TransportInfo.TRANSPORT_BIS_B);
You have more flags available in TransportInfo class for Wi-Fi, BES (MDS) and direct TCP. So the method to check for internet connectivity would check for all the available transport types.
Related
I have a Raspberry Pi connected to my Wifi LAN that responds to mDNS as mqtt-broker.local.
I can find it on my laptop with this command:
$ avahi-resolve-host-name -4 mqtt-broker.local
mqtt-broker.local 192.168.XXX.YYY
I have an ESP32 DOIT DevKit device that can send messages to the Raspberry Pi via Wifi if I use the IP address 192.168.XXX.YYY, however I would like my ESP32 to resolve the host using mDNS.
I am not able to get the mDNS working, the code at the bottom prints:
Finding the mDNS details...
No services found...
Done finding the mDNS details...
What's wrong with this code?
What should I put as service in MDNS.queryService("mqtt-broker", "tcp")? I have tried even with service mqtt with no luck, however this shouldn't matter, the mDNS stuff should work regardless what's exposed from the Raspberry Pi (HTTP server, MQTT, FTP whatever...)
Checking here https://github.com/espressif/arduino-esp32/blob/master/libraries/ESPmDNS/src/ESPmDNS.h#L98 there is not that much information about this "service" and "proto", and I am not that much familiar with low-level C/C++, what are these things?
This is the code I am using:
// import the headers
#include <ESPmDNS.h>
void findMyPi() {
Serial.println("Finding the mDNS details...");
// make sure we are connected to the Wifi
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.println("Not yet connected to Wifi...");
}
if (!MDNS.begin("whatever_this_could_be_anything")) {
Serial.println("Error setting up MDNS responder!");
}
// what should I put in here as "service"?
int n = MDNS.queryService("mqtt-broker", "tcp");
if (n == 0) {
Serial.println("No services found...");
}
else {
for (int i = 0; i < n; ++i) {
// Print details for each service found
Serial.print(" ");
Serial.print(i + 1);
Serial.print(": ");
Serial.print(MDNS.hostname(i)); // "mqtt-broker" ??? How can I find it???
Serial.print(" (");
Serial.print(MDNS.IP(i));
Serial.print(":");
Serial.print(MDNS.port(i));
Serial.println(")");
}
}
Serial.println("Done finding the mDNS details...");
}
This function has been inspired by this example:
https://github.com/espressif/arduino-esp32/blob/master/libraries/ESPmDNS/examples/mDNS-SD_Extended/mDNS-SD_Extended.ino
Ended up using a different method from the class on that mDNS library provided by Espressif (ESPmDNS.h), a combination of:
IPAddress serverIp = MDNS.queryHost(mDnsHost);
while loop on this check serverIp.toString() == "0.0.0.0"
This is the code that glues up all together:
// on my laptop (Ubuntu) the equivalent command is: `avahi-resolve-host-name -4 mqtt-broker.local`
String findMDNS(String mDnsHost) {
// the input mDnsHost is e.g. "mqtt-broker" from "mqtt-broker.local"
Serial.println("Finding the mDNS details...");
// Need to make sure that we're connected to the wifi first
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.print(".");
}
if (!MDNS.begin("esp32whatever")) {
Serial.println("Error setting up MDNS responder!");
} else {
Serial.println("Finished intitializing the MDNS client...");
}
Serial.println("mDNS responder started");
IPAddress serverIp = MDNS.queryHost(mDnsHost);
while (serverIp.toString() == "0.0.0.0") {
Serial.println("Trying again to resolve mDNS");
delay(250);
serverIp = MDNS.queryHost(mDnsHost);
}
Serial.print("IP address of server: ");
Serial.println(serverIp.toString());
Serial.println("Done finding the mDNS details...");
return serverIp.toString();
}
I want to set my wifi hotspot password programmatically for my application so that user don't have to go to the setting menu to check their password.
I am already using NEHotspotNetwork, where it set the password, but here, we need to set the password which is already there in the setting menu for connecting to the network.
It's also helpful if I can get my wifi hotspot password, from the application without jailbreak my device.
You just need to use the following code:
WifiConfiguration netConfig = new WifiConfiguration();
netConfig .preSharedKey = "yourpassword";
Using NEHotspotNetwork function register you can set the password
NEHotspotHelper.register(options: options, queue: queue) { (cmd: NEHotspotHelperCommand) in
if cmd.commandType == NEHotspotHelperCommandType.filterScanList {
//Get all available hotspots
var list: [NEHotspotNetwork] = cmd.networkList!
//Figure out the hotspot you wish to connect to
// let desiredNetwork : NEHotspotNetwork? = getBestScanResult(list)
var hotspot = [NEHotspotNetwork]()
for network in cmd.networkList!
{//check for your network ssid and set password
network.setConfidence(.high)
network.setPassword("yourpassword") //Set the WIFI password
hotspot.append(network)
}
let response = cmd.createResponse(NEHotspotHelperResult.success)
response.setNetworkList(hotspot)
response.deliver() } else if cmd.commandType == NEHotspotHelperCommandType.evaluate {
if let network = cmd.network {
let response = cmd.createResponse(NEHotspotHelperResult.success)
response.setNetwork(network)
response.deliver() //Respond back }
} else if cmd.commandType == NEHotspotHelperCommandType.authenticate {
//Perform custom authentication and respond back with success
// if all is OK
let response = cmd.createResponse(NEHotspotHelperResult.success)
response.deliver() //Respond back
}
Also you can use network configuration profile with the help of Apple Configurator 2 tool for your known network. There you need to setup your wi-fi and then after installing the NCP on your device, It will automatically connect with the mentioned network. But you have to host that file on server cause we can't download profile locally and using local server like GCDServer(tried already.)
Having issues connecting to iPv6 hosts with the CocoaAsyncSocket library
I successfully had GCDUDPAsyncSocket working but realized TCP was more appropriate for my use case.
Unfortunately - I can never successfully connect with a bonjour published and discovered NSNetService. The service is discovered and the address is discovered as well. A connection attempt without failure happens but the connection is never secured.
I can connect using "connectWithHost" and passing in the IP address assigned to my mac but this the only way i can get that ip is by hard coding it. Is there a way to obtain this IP through NSNetService?
I'm using swift, Xcode 7.1.1 and iOS 9.1. I am connecting between an iPhone and a Mac running an Apple TV Simulator. This works fine with UDP.
No matter what - the connection attempt times out even though an appropriate address is supplied!
Socket is Disconnecting - Error Domain=NSPOSIXErrorDomain Code=60 "Operation timed out" UserInfo={NSLocalizedDescription=Operation timed out, NSLocalizedFailureReason=Error in connect() function}
Anyone run into this before? Here is my connection code:
func connectToAddress(sender: NSNetService) {
if let addresses = sender.addresses {
for address in addresses {
print(address)
}
self.serverAddresses = addresses
var done = false
while !done && (self.serverAddresses.count > 0) {
let address = self.serverAddresses[0]
self.socket = GCDAsyncSocket(delegate: self, delegateQueue: dispatch_get_main_queue())
do {
try self.socket.connectToAddress(address)
done = true
} catch {
print("Unable to Connect")
}
}
if !done {
print("Could Not Connect To Address")
}
}
}
Please update your CocoaAsyncSocket library. The issue was fixed in May 2nd commit. So this should work with the following flag set to false
socket.IPv4PreferredOverIPv6 = NO;
This will allow your app / game to connect in both IPv4 and IPv6. As of June 1 Apple is rejecting the apps which are not compliant with IPv6 networks. You app should work without any problems in an IPv6 network.
You can setting
socket.IPv4PreferredOverIPv6 = NO;
as #Kumar C mentioned, it will works well in IPv6, but if you still need work with IP address(use IP address as Host), you can update the code as below in GCDAsyncSocket.m(IPv4PreferredOverIPv6 should be set to NO first):
+ (NSMutableArray *)lookupHost:(NSString *)host port:(uint16_t)port error:(NSError **)errPtr
{
......
for (res = res0; res; res = res->ai_next)
{
if (res->ai_family == AF_INET)
{
// Found IPv4 address.
// Wrap the native address structure, and add to results.
if (((struct sockaddr_in *)res->ai_addr)->sin_port == 0)
((struct sockaddr_in *)res->ai_addr)->sin_port = htons(port);
NSData *address4 = [NSData dataWithBytes:res->ai_addr length:res->ai_addrlen];
[addresses addObject:address4];
}
else if (res->ai_family == AF_INET6)
{
// Found IPv6 address.
// Wrap the native address structure, and add to results.
if (((struct sockaddr_in6 *)res->ai_addr)->sin6_port == 0)
((struct sockaddr_in6 *)res->ai_addr)->sin6_port = htons(port);
NSData *address6 = [NSData dataWithBytes:res->ai_addr length:res->ai_addrlen];
[addresses addObject:address6];
}
}
.......
}
This will allows you app work well in IPv4/IPv6 whatever the HOST is IP address or domain.
Calling 'GetFlags' on NetworkReachability always returns 'Reachable' even if
the domain doesn't exist. I'll attach a full test solution but the following
code should result in "Failed..." but results in "Success = True".
The code is based on the Reachability sample.
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
// Perform any additional setup after loading the view, typically from a nib.
string host = "http://nonsense.test12345679.com";
using (var r = new SystemConfiguration.NetworkReachability (host)) {
NetworkReachabilityFlags flags;
if (r.TryGetFlags (out flags)) {
_label.Text = "Success = " + IsReachableWithoutRequiringConnection (flags);
} else {
_label.Text = "Failed to get flags ";
}
}
}
public bool IsReachableWithoutRequiringConnection(NetworkReachabilityFlags flags)
{
// Is it reachable with the current network configuration?
bool isReachable = (flags & NetworkReachabilityFlags.Reachable) != 0;
// Do we need a connection to reach it?
bool noConnectionRequired = (flags & NetworkReachabilityFlags.ConnectionRequired) == 0
|| (flags & NetworkReachabilityFlags.IsWWAN) != 0;
return isReachable && noConnectionRequired;
}
If you switch the device into airplane mode then reachability is correctly
returned as false.
I'm testing on an iPad Air, iOS 9.0.2 and the latest stable Xamarin.
iOS System Configuration framework's Reachability does not actually check to see if the host is alive (i.e. it is not a ping/icmp/http/... based test).
A remote host is considered reachable when a data packet, sent by an
application into the network stack, can leave the local device.
Reachability does not guarantee that the data packet will actually be
received by the host.
iOS SCNetworkReachability Reference
Also a good SO A/Q on about the Reachability flags : How to interpret NetworkReachabilityFlags in Xamarin.iOS?
I haven't used that functionality directly; but have you tried the nuget package for connectivity? https://github.com/jamesmontemagno/Xamarin.Plugins/tree/master/Connectivity. I have tried it and it seems to work pretty well. It has methods for testing if a specific endpoint is reachable.
bool canConnect = CrossConnectivity.Current.IsConnected
&& await CrossConnectivity.Current.IsRemoteReachable(host, portInt));
I'm making an iOS app where I'd need to check if a host, for example, google.com, is available (or is up/down). I've been using this code:
NSString *host = theTextField.text;
bool success = false;
const char *host_name = [host
cStringUsingEncoding:NSASCIIStringEncoding];
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL,
host_name);
SCNetworkReachabilityFlags flags;
success = SCNetworkReachabilityGetFlags(reachability, &flags);
bool isAvailable = success && (flags & kSCNetworkFlagsReachable) &&
!(flags & kSCNetworkFlagsConnectionRequired);
if (isAvailable) {
status.text = #"The host is up! :)";
} else {
status.text = #"The host is down! :(";
}
}
But I've a felling it doesn't works like it should. For example if I enter google.com it says it's up, okay, but if I enter the url of my webpage (which is down ATM) says it's up as well (the address is zad0xsis.net). If I enter a fake address it says it's down, so it may work right and I may be thinking odd things IDK.
zad0xsis.net is up for me.
The Reachability use a the same principle as the ping command line (DNS Resolution + Ping in fact).
I think you should use NSURLConnection to see if the webserver is up.
Have you tried ping zad0xsis.net from terminal? If you receive a successful response, then i believe it is about some networking stuff which i am not good at all.