How to check availability of internet connection(Wifi, GPRS, EDGE) in Blackberry - blackberry

How to check availability of internet connection(Wifi, GPRS, EDGE) in Blackberry.

Try this....it worked for me .
protected static boolean isInternetAvailable() {
boolean flag = false;
if(WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED){
flag = true;
}
else if(RadioInfo.isDataServiceOperational()){
flag = true;
}
return flag;
}

Related

Flutter connectivity: Works on Android, but on iOS simulator when I can open webpages in Safari I have internet but the app says there is no internet?

I use this package: https://pub.dev/packages/connectivity_plus
I have a finished application that is working on Android but when I am testing it on iOS it shows that there is no internet. I can use and open pages in Safari so there is definitely one. But the following code returns false in iOS:
class InternetConnectivity with ChangeNotifier {
StreamSubscription<ConnectivityResult>? _subscription;
bool haveInternet = false;
void checkConnectivity() {
if (_subscription == null) {
_subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
bool res = result == ConnectivityResult.mobile || result == ConnectivityResult.wifi;
setHaveInternet = res;
});
}
}
set setHaveInternet(bool value) {
if (haveInternet != value) {
haveInternet = value;
notifyListeners();
}
}
}
I don't get any errors so I don't really know where to look for the problem.
On the screen where it checks that internet connection starts with this:
bool _haveInternet = true;
then in initState() I set the value of it:
#override
void initState() {
super.initState();
InternetConnectivity ? _internetConnectivity = InternetConnectivity();
setState(() {
_haveInternet = _internetConnectivity!.haveInternet;
});
After the initState() ran, the _haveInternet becomes false, so the connectivity_plus package returns false while normally it should be true.
Thanks in advance.
The package has a bug. According to documentation it should only affect iOS simulator. https://github.com/fluttercommunity/plus_plugins/issues/479
From package comments:
/// On iOS, the connectivity status might not update when WiFi
/// status changes, this is a known issue that only affects simulators.
/// For details see https://github.com/fluttercommunity/plus_plugins/issues/479.

Unity check internet connection availability

I am porting our game to Unity, and need some help regarding internet connectivity check in Unity.
Official Unity Documentation says 'do not use Application.internetReachability.
So i am confused which code will work here.
Didn't find any prominent solution in any forum.
I want to check whether wi-fi or GPRS is on or not which should work for iOS and Android.
Thanks in advance.
Solution
Application.internetReachability is what you need. In conjunction with Ping, probably.
Here's an example:
using UnityEngine;
public class InternetChecker : MonoBehaviour
{
private const bool allowCarrierDataNetwork = false;
private const string pingAddress = "8.8.8.8"; // Google Public DNS server
private const float waitingTime = 2.0f;
private Ping ping;
private float pingStartTime;
public void Start()
{
bool internetPossiblyAvailable;
switch (Application.internetReachability)
{
case NetworkReachability.ReachableViaLocalAreaNetwork:
internetPossiblyAvailable = true;
break;
case NetworkReachability.ReachableViaCarrierDataNetwork:
internetPossiblyAvailable = allowCarrierDataNetwork;
break;
default:
internetPossiblyAvailable = false;
break;
}
if (!internetPossiblyAvailable)
{
InternetIsNotAvailable();
return;
}
ping = new Ping(pingAddress);
pingStartTime = Time.time;
}
public void Update()
{
if (ping != null)
{
bool stopCheck = true;
if (ping.isDone)
{
if (ping.time >= 0)
InternetAvailable();
else
InternetIsNotAvailable();
}
else if (Time.time - pingStartTime < waitingTime)
stopCheck = false;
else
InternetIsNotAvailable();
if (stopCheck)
ping = null;
}
}
private void InternetIsNotAvailable()
{
Debug.Log("No Internet :(");
}
private void InternetAvailable()
{
Debug.Log("Internet is available! ;)");
}
}
Things to note
Unity's ping doesn't do any domain name resolutions, i.e. it only accepts IP addresses. So, if some player has Internet access but has some DNS problems, the method will say that he has Internet.
It's only a ping check. Don't expect it to be 100% accurate. In some rare cases it will provide you with false information.
What I do is Network.TestConnection which tests for connectivity, whether or not it can be a server, and whether or not NAT punch through can be used successfully.
This is the sample code they have given us. If the result is ConnectionTesterStatus.Error, chances are there is no internet access.
var testStatus = "Testing network connection capabilities.";
var testMessage = "Test in progress";
var shouldEnableNatMessage : String = "";
var doneTesting = false;
var probingPublicIP = false;
var serverPort = 9999;
var connectionTestResult = ConnectionTesterStatus.Undetermined;
// Indicates if the useNat parameter be enabled when starting a server
var useNat = false;
function OnGUI() {
GUILayout.Label("Current Status: " + testStatus);
GUILayout.Label("Test result : " + testMessage);
GUILayout.Label(shouldEnableNatMessage);
if (!doneTesting)
TestConnection();
}
function TestConnection() {
// Start/Poll the connection test, report the results in a label and
// react to the results accordingly
connectionTestResult = Network.TestConnection();
switch (connectionTestResult) {
case ConnectionTesterStatus.Error:
testMessage = "Problem determining NAT capabilities";
doneTesting = true;
break;
case ConnectionTesterStatus.Undetermined:
testMessage = "Undetermined NAT capabilities";
doneTesting = false;
break;
case ConnectionTesterStatus.PublicIPIsConnectable:
testMessage = "Directly connectable public IP address.";
useNat = false;
doneTesting = true;
break;
// This case is a bit special as we now need to check if we can
// circumvent the blocking by using NAT punchthrough
case ConnectionTesterStatus.PublicIPPortBlocked:
testMessage = "Non-connectable public IP address (port " +
serverPort +" blocked), running a server is impossible.";
useNat = false;
// If no NAT punchthrough test has been performed on this public
// IP, force a test
if (!probingPublicIP) {
connectionTestResult = Network.TestConnectionNAT();
probingPublicIP = true;
testStatus = "Testing if blocked public IP can be circumvented";
timer = Time.time + 10;
}
// NAT punchthrough test was performed but we still get blocked
else if (Time.time > timer) {
probingPublicIP = false; // reset
useNat = true;
doneTesting = true;
}
break;
case ConnectionTesterStatus.PublicIPNoServerStarted:
testMessage = "Public IP address but server not initialized, "+
"it must be started to check server accessibility. Restart "+
"connection test when ready.";
break;
case ConnectionTesterStatus.LimitedNATPunchthroughPortRestricted:
testMessage = "Limited NAT punchthrough capabilities. Cannot "+
"connect to all types of NAT servers. Running a server "+
"is ill advised as not everyone can connect.";
useNat = true;
doneTesting = true;
break;
case ConnectionTesterStatus.LimitedNATPunchthroughSymmetric:
testMessage = "Limited NAT punchthrough capabilities. Cannot "+
"connect to all types of NAT servers. Running a server "+
"is ill advised as not everyone can connect.";
useNat = true;
doneTesting = true;
break;
case ConnectionTesterStatus.NATpunchthroughAddressRestrictedCone:
case ConnectionTesterStatus.NATpunchthroughFullCone:
testMessage = "NAT punchthrough capable. Can connect to all "+
"servers and receive connections from all clients. Enabling "+
"NAT punchthrough functionality.";
useNat = true;
doneTesting = true;
break;
default:
testMessage = "Error in test routine, got " + connectionTestResult;
}
if (doneTesting) {
if (useNat)
shouldEnableNatMessage = "When starting a server the NAT "+
"punchthrough feature should be enabled (useNat parameter)";
else
shouldEnableNatMessage = "NAT punchthrough not needed";
testStatus = "Done testing";
}
}
This is a simple solution that I am already using for Internet check. It works for both IOS and Android. I know that it may need improvements, but here it is:
public static bool InternetStatus ()
{
if (Application.internetReachability != NetworkReachability.NotReachable)
{
return true;
}else{
return false;
}
}

Blackberry GPRS not working

I have one BlackBerry Application which is running perfectly fine with wi-fi. but I am not able to run the application using GPRS. please help me..Thanks..
boolean hasConnectivity_BIS = TransportInfo.isTransportTypeAvailable(TransportInfo.TRANSPORT_BIS_B)&&TransportInfo.hasSufficientCoverage(TransportInfo.TRANSPORT_BIS_B);
boolean hasConnectivity_MDS = TransportInfo.isTransportTypeAvailable(TransportInfo.TRANSPORT_MDS)&&TransportInfo.hasSufficientCoverage(TransportInfo.TRANSPORT_MDS);
boolean hasConnectivity_TCP_Cell = TransportInfo.isTransportTypeAvailable(TransportInfo.TRANSPORT_TCP_CELLULAR)&&TransportInfo.hasSufficientCoverage(TransportInfo.TRANSPORT_TCP_CELLULAR);
boolean hasConnectivity_TCP_wifi = TransportInfo.isTransportTypeAvailable(TransportInfo.TRANSPORT_TCP_WIFI)&&TransportInfo.hasSufficientCoverage(TransportInfo.TRANSPORT_TCP_WIFI);
boolean hasConnectivity_WAP = TransportInfo.isTransportTypeAvailable(TransportInfo.TRANSPORT_WAP)&&TransportInfo.hasSufficientCoverage(TransportInfo.TRANSPORT_WAP);
boolean hasConnectivity_WAP2 = TransportInfo.isTransportTypeAvailable(TransportInfo.TRANSPORT_WAP2)&&TransportInfo.hasSufficientCoverage(TransportInfo.TRANSPORT_WAP2);
if (hasConnectivity_BIS||hasConnectivity_MDS||hasConnectivity_TCP_Cell||hasConnectivity_TCP_wifi||hasConnectivity_WAP||hasConnectivity_WAP2)
{
boolean hasconn=ButtonPay.checkConnection();
System.out.println("Has Connection?????>>> "+hasconn);
//my implementation
}
else
{
caller.showDialog("Response", "Internet connection not available");
}
You need to append your connection type after your URL.
public static String getConnectionString() {
String connectionString = null;
// Simulator behaviour is controlled by the USE_MDS_IN_SIMULATOR
// variable.
if (DeviceInfo.isSimulator()) {
connectionString = ";deviceside=true";
}
// Wifi is the preferred transmission method
else if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) {
connectionString = ";interface=wifi";
}
// Is the carrier network the only way to connect?
else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) {
String carrierUid = getCarrierBIBSUid();
if (carrierUid == null) {
// Has carrier coverage, but not BIBS. So use the carrier's TCP
// network
connectionString = ";deviceside=true";
} else {
// otherwise, use the Uid to construct a valid carrier BIBS
// request
connectionString = ";deviceside=false;connectionUID="+carrierUid + ";ConnectionType=mds-public";
}
}
// Check for an MDS connection instead (BlackBerry Enterprise Server)
else if ((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS) {
connectionString = ";deviceside=false";
}
// If there is no connection available abort to avoid hassling the user
// unnecssarily.
else if (CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE) {
connectionString = "none";
}
// In theory, all bases are covered by now so this shouldn't be reachable.But hey, just in case ...
else {
connectionString = ";deviceside=true";
}
return connectionString;
}
/**
* Looks through the phone's service book for a carrier provided BIBS
* network
*
* #return The uid used to connect to that network.
*/
private synchronized static String getCarrierBIBSUid() {
ServiceRecord[] records = ServiceBook.getSB().getRecords();
int currentRecord;
for (currentRecord = 0; currentRecord < records.length; currentRecord++) {
if (records[currentRecord].getCid().toLowerCase().equals("ippp")) {
if (records[currentRecord].getName().toLowerCase()
.indexOf("bibs") >= 0) {
return records[currentRecord].getUid();
}
}
}
return null;
}
Once you writtent this code. use
con = (HttpConnection) Connector.open(url + getConnectionString());
It will determine available network type .

Disabling and enabling internet in blackberry

I want to know what is the way to enable and disable internet connection in blackberry through coding.
EDIT
protected void disableConnection() {
activeConn = RadioInfo.getActiveWAFs();
if(activeConn == 0){
activeConn = RadioInfo.getEnabledWAFs();
}
mystore.setContents(new Integer(activeConn));
mystore.commit();
Radio.deactivateWAFs(activeConn);
Dialog.alert("Off internet");
}
protected void enableConnection() {
if(RadioInfo.getState() == RadioInfo.STATE_ON){
Dialog.alert("Internet on already");
}else if(mystore.getContents() != null){
if(Radio.activateWAFs(Integer.parseInt(mystore.getContents().toString())) == true){
Dialog.alert("On Internet");
}else{
Dialog.alert("Unable to on internet");
}
}else{
Dialog.alert("Unable to on internet");
}
}
These are two methods which I call on turn on and turn off button click.
You asked about internet connectivity, so I assume you are interested in more than just the Wi-Fi connection. Calling Radio.deactivateWAFs(RadioInfo.WAF_WLAN); will only disable Wi-Fi.
A better implementation would probably first check which radios are on, and then turn those radios off. When you want to turn service on again, reactivate the radios which you turned off. Something like this:
/** we record which radios are active */
private int _activeWAFs = 0;
private void getActiveWAFs() {
_activeWAFs = RadioInfo.getActiveWAFs();
if (_activeWAFs == 0) {
_activeWAFs = RadioInfo.getEnabledWAFs();
}
}
/** turn radios off if they're currently on */
private void disableAll() {
getActiveWAFs();
Radio.deactivateWAFs(_activeWAFs);
}
/** turn radios on, if we turned them off with disableAll() */
private void enableAll() {
boolean success = Radio.activateWAFs(_activeWAFs) && (RadioInfo.getState() == RadioInfo.STATE_ON);
if (!success) {
// do something?
}
}
Also, if you want notifications about the results of these operations, or external changes to the radio, you can implement RadioStatusListener:
public void networkStarted(int networkId, int service) {
if (RadioInfo.getState() == RadioInfo.STATE_ON) {
// network ready to use!
}
}
And, yes, this call will affect the entire device, not just internet connectivity for your app.
Radio.deactivateWAFS() will it deactivate all the wireless connection or just internet connectivity or Bluetooth connectivity.
Example: deactive WiFi connectivity.
Radio.deactivateWAFs(RadioInfo.WAF_WLAN);
try this -
this will turn on wifi-
Radio.activateWAFs(RadioInfo.WAF_WLAN);
this will turn off the wifi-
Radio.deactivateWAFs(RadioInfo.WAF_WLAN);

Check wifi condition in Blackberry application

I have developed an application for blackberry devices. The application is working fine if it uses internet via data service provider.
I have BB 9550 and I want to use my application using wifi. I tried a lot but I cant get proper answer to check wifi condition.
How we can differentiate to run our application for wifi or data service provider?
For checking wifi is connected or not the following method will help you.
public static boolean isWifiConnected()
{
try
{
if (RadioInfo.getSignalLevel(RadioInfo.WAF_WLAN) != RadioInfo.LEVEL_NO_COVERAGE)
{
return true;
}
}
catch(Exception e)
{
System.out.println("Exception during get WiFi status");
}
return false;
}
if wifi is not connected the following methods will help to add data service.
public static String getConnParam(){
String connectionParameters = "";
if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) {
// Connected to a WiFi access point
connectionParameters = ";interface=wifi";
} else {
int coverageStatus = CoverageInfo.getCoverageStatus();
ServiceRecord record = getWAP2ServiceRecord();
if (record != null
&& (coverageStatus & CoverageInfo.COVERAGE_DIRECT) ==
CoverageInfo.COVERAGE_DIRECT) {
// Have network coverage and a WAP 2.0 service book record
connectionParameters = ";deviceside=true;ConnectionUID="
+ record.getUid();
} else if ((coverageStatus & CoverageInfo.COVERAGE_MDS) ==
CoverageInfo.COVERAGE_MDS) {
// Have an MDS service book and network coverage
connectionParameters = ";deviceside=false";
} else if ((coverageStatus & CoverageInfo.COVERAGE_DIRECT) ==
CoverageInfo.COVERAGE_DIRECT) {
// Have network coverage but no WAP 2.0 service book record
connectionParameters = ";deviceside=true";
}
}
return connectionParameters;
}
private static ServiceRecord getWAP2ServiceRecord() {
ServiceBook sb = ServiceBook.getSB();
ServiceRecord[] records = sb.getRecords();
for(int i = 0; i < records.length; i++) {
String cid = records[i].getCid().toLowerCase();
String uid = records[i].getUid().toLowerCase();
if (cid.indexOf("wptcp") != -1 &&
uid.indexOf("wifi") == -1 &&
uid.indexOf("mms") == -1) {
return records[i];
}
}
return null;
}
Example to use above methods.
String connParams=(isWifiConnected())?";interface=wifi":getConnParam();
Hope This will help you
try this:
private static String getParameters() {
if (GetWiFiCoverageStatus()) {
return ";deviceside=true;interface=wifi";
}
else {
return yourParametersForEdge
}
}
private static boolean GetWiFiCoverageStatus() {
if((WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED)) {
return true;
}
else
return false;
}
And when you need to connect, you'll have to add the parameters to the URL:
yourUrl = yourUrl + getParameters();

Resources