Disabling and enabling internet in blackberry - 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);

Related

How to re-connect the connected WIFI before in Xamarin Forms(iOS)?

My project is Xamarin Forms in iOS device, and that using WIFI to work with other devices. I want to know whether it is possible to know the current connected ssid name first. If my app loses the link, can it silently try to reconnect to the WiFi without the user having to do anything?
Here is my logic diagram
You could use DependencyService to get a SSID name, you could refer to Xamarin.Forms DependencyService
In your service, try some code like this:
string ssid = "";
string[] supportedInterfaces;
StatusCode status = CaptiveNetwork.TryGetSupportedInterfaces(out supportedInterfaces);
if ((status = CaptiveNetwork.TryGetSupportedInterfaces(out supportedInterfaces)) == StatusCode.OK)
{
foreach (var item in supportedInterfaces)
{
NSDictionary info;
status = CaptiveNetwork.TryCopyCurrentNetworkInfo(item, out info);
if (status == StatusCode.OK)
{
ssid = info[CaptiveNetwork.NetworkInfoKeySSID].ToString();
}
}
}
In your info.plist, you should add this key:
<key>NSLocationWhenInUseUsageDescription</key>
<string>Your Description</string>
Once you get the SSID name (and password), you could use NEHostspotConfiguration to make a connection.
NEHotspotConfiguration hotspotConfiguration = new NEHotspotConfiguration(ssid,pwd,false);
hotspotConfiguration.JoinOnce = true;
NEHotspotConfigurationManager.SharedManager.ApplyConfiguration(hotspotConfiguration, (NSError error) =>
{
if (error != null)
{
if (error?.LocalizedDescription == "already associated.")
{
IsConnected = true;
}
else
{
IsConnected = false;
}
}
else
{
IsConnected = true;
}
});
You may also enable Access WiFi information, Hotspot capability of you App ID at the Apple Developer Portal and add them in your Entitlements.plist. Don't forget to regenerate your provisioning file. Hope my answer could help you~

Invite Friend in Google Play Services

I'm creating a Game App in objective-c which is using Google Play Game services for realtime Multiplayer functionality. I follows the documentation at https://developers.google.com/games/services/ios/turnbasedMultiplayer. In my app there are two options Auto match and Invite Match. Auto Match functionality working fine. But Invite match not.
I follow following Code for this
- (int)minPlayersForPlayerPickerLauncher {
return 1;
}
- (int)maxPlayersForPlayerPickerLauncher {
return 2;
}
- (IBAction)inviteFriendsWasPressed:(id)sender
{
// This can be a 2-4 player game
[GPGLauncherController sharedInstance].playerPickerLauncherDelegate = self;
// This assumes your class has been declared a GPGPlayerPickerLauncherDelegate
[[GPGLauncherController sharedInstance] presentPlayerPicker];
}
on click this button Action follow Screen is open
See here
After that when I enter emailId in textfield there is no action perform to search particular user.
Please help me
Thanks
Unfortunately, the player selection no longer works since Google+ is no longer integrated into Play Game Services: https://android-developers.googleblog.com/2016/12/games-authentication-adopting-google.html
// request code for the "select players" UI
// can be any number as long as it's unique
final static int RC_SELECT_PLAYERS = 10000;
// launch the player selection screen
// minimum: 1 other player; maximum: 3 other players
Intent intent = Games.RealTimeMultiplayer.getSelectOpponentsIntent(mGoogleApiClient, 1, 3);
startActivityForResult(intent, RC_SELECT_PLAYERS);
#Override
public void onActivityResult(int request, int response, Intent data) {
if (request == RC_SELECT_PLAYERS) {
if (response != Activity.RESULT_OK) {
// user canceled
return;
}
// get the invitee list
Bundle extras = data.getExtras();
final ArrayList<String> invitees =
data.getStringArrayListExtra(Games.EXTRA_PLAYER_IDS);
// get auto-match criteria
Bundle autoMatchCriteria = null;
int minAutoMatchPlayers =
data.getIntExtra(Multiplayer.EXTRA_MIN_AUTOMATCH_PLAYERS, 0);
int maxAutoMatchPlayers =
data.getIntExtra(Multiplayer.EXTRA_MAX_AUTOMATCH_PLAYERS, 0);
if (minAutoMatchPlayers > 0) {
autoMatchCriteria = RoomConfig.createAutoMatchCriteria(
minAutoMatchPlayers, maxAutoMatchPlayers, 0);
} else {
autoMatchCriteria = null;
}
// create the room and specify a variant if appropriate
RoomConfig.Builder roomConfigBuilder = makeBasicRoomConfigBuilder();
roomConfigBuilder.addPlayersToInvite(invitees);
if (autoMatchCriteria != null) {
roomConfigBuilder.setAutoMatchCriteria(autoMatchCriteria);
}
RoomConfig roomConfig = roomConfigBuilder.build();
Games.RealTimeMultiplayer.create(mGoogleApiClient, roomConfig);
// prevent screen from sleeping during handshake
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
// create a RoomConfigBuilder that's appropriate for your implementation
private RoomConfig.Builder makeBasicRoomConfigBuilder() {
return RoomConfig.builder(this)
.setMessageReceivedListener(this)
.setRoomStatusUpdateListener(this);
}

Remove password and connect with new password wifi android

i would like to ask your help, this is my code :
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==REQUEST_CODE_INPUT){
switch (resultCode){
case RESULT_CODE_PASS:
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
WifiManager wifiManager= (WifiManager) getSystemService(Context.WIFI_SERVICE);
pass=data.getStringExtra("passWord");
nameWifi=data.getStringExtra("nameWifi");
WifiConfiguration conf=new WifiConfiguration();
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
List<ScanResult> networkList=wifiManager.getScanResults();
if(networkList !=null){
for(ScanResult network : networkList){
if(network.SSID.startsWith("\"")){
network.SSID=network.SSID.substring(1, network.SSID.length() - 1);
}
if(nameWifi.equals(network.SSID)){
String Capabilities=network.capabilities;
if(Capabilities.contains("WPA2")){
conf.preSharedKey="\""+pass+"\"";
}else if(Capabilities.contains("WEP")){
conf.wepKeys[0]="\""+pass+"\"";
conf.wepTxKeyIndex=0;
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
}
break;
}
}
}
wifiManager.addNetwork(conf);
List<WifiConfiguration> list=wifiManager.getConfiguredNetworks();
for(WifiConfiguration i: list){
if(i.SSID!=null && i.SSID.equals("\""+nameWifi+"\"")){
wifiManager.disconnect();
showWaiting();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
if(networkInfo.isConnected()){
dismissWaiting();
}else{
AlertDialog.Builder alert=new AlertDialog.Builder(MainActivity.this);
alert.setTitle("Wrong Password")
.setMessage("Please try again")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert.create().show();
}
}
}
break;
}
}
}
i have 2 Activities (MainActivity and ShareWifi), i create Sharewifi with purpose: users input wifi SSID and password then press Enter, both of them will send to MainActivity--> disable current wifi and reconnect with new password but it still uses old password to reconnect. I write this code follow this link : How do I connect to a specific Wi-Fi network in Android programmatically?
Please help me to resolve this problem. Thank you very much.
Resolved: add saveConfiguration and it works
wifiManager.addNetwork(conf);
wifiManager.saveConfiguration();

how to check internet connection in java in blackberry

I want to check whether internet connection is there or not in blackberry device so that depending on the result I can call webservices to get data or upload data from my application
I have tried this one
CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_MDS))) ||
(CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_BIS_B)) != false
If you want to check the internet connection, then send any url to the web service and check the HTTP Response. If HTTPResponse is 200 then only you are having internet connection. Do like this.......
try
{
factory = new HttpConnectionFactory();
url="Here put any sample url or any of your web service to check network connection.";
httpConnection = factory.getHttpConnection(url);
response=httpConnection.getResponseCode();
if(response==HttpConnection.HTTP_OK)
{
callback(response);
}else
{
callback(response);
}
} catch (Exception e)
{
System.out.println(e.getMessage());
callback(0);
}
Here "response"=200 then you have an internet connection. otherwise it is a connection problem. You can check this like below...........
public void callback(int i)
{
if(i==200)
{
//You can do what ever you want.
}
else
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
int k=Dialog.ask(Dialog.D_OK,"Connection error,please check your network connection..");
if(k==Dialog.D_OK)
{
System.exit(0);
}
}
});
}
}
Here System.exit(0); exit the application where ever you are.
Take these two classes
1)HttpConnectionFactory.java
2)HttpConnectionFactoryException.java
from this link:HttpConnection Classes

Critical tunnel failure error blackberry

I have developed a app for blackberry ,its approved from appworld but it gives following error
on 4.6
Critical tunnel failure
and
on 5.0 and 6.0
ava.io APN not specified
please help why this error is coming and how to solve it
I think Problem is you didn't add appropriate connection suffix to the url.
Follow the link can solve your problem:http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800451/800563/What_Is_-_Different_ways_to_make_an_HTTP_or_socket_connection.html?nodeid=826935&vernum=0
And also ou can use the following sample code:
private static String getConnectionString(){
String connectionString="";
if(WLANInfo.getWLANState()==WLANInfo.WLAN_STATE_CONNECTED){
connectionString=";interface=wifi";
}
else if((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS){
connectionString = ";deviceside=false";
}
else if((CoverageInfo.getCoverageStatus() & CoverageInfo.COVERAGE_DIRECT)==CoverageInfo.COVERAGE_DIRECT){
String carrierUid=getCarrierBIBSUid();
if(carrierUid == null) {
connectionString = ";deviceside=true";
}
else{
connectionString = ";deviceside=false;connectionUID="+carrierUid + ";ConnectionType=mds-public";
}
}
else if(CoverageInfo.getCoverageStatus() == CoverageInfo.COVERAGE_NONE)
{
}
return connectionString;
}
Just to clear up some issues.
#Jisson your answer was helpful
But you did not include code for the method getCarrierBIBSUid()
/**
* Looks through the phone's service book for a carrier provided BIBS network
* #return The uid used to connect to that network.
*/
private 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;
}
Also it might be helpful to include
if (DeviceInfo.isSimulator()){
return ";deviceSide=true";
}
At the beginning of the getConnectionString() method
For more info see Melick's Blog
Solved this issue by setting the APN values on the phone its self and using the connection string code suggested in the other answers.
Change your APN settings (in South Africa)
On Home screen, click Options
Click Advanced Options and then TCP
Enter the APN: **internet**
username: **guest**
password: **guest**
Press the Menu key and select Save
(for rest of the world find your settings here)
From http://www.blackberrytune.com/blackberry-tcp-ip-apn-settings/
and
http://www.blackberryfaq.com/index.php/Carrier_specific_APN/TCP_settings

Resources