Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Differentiating 3G, EDGE, GPRS

I know how to check for WiFi vs mobile connection using networkTypeName.

But I need to serve different content to users depending on whether they're on 3G, EDGE or GPRS mobile connections (wildly different data rates between those connection types).

Is this possible?

— asked March 31st 2010 by Nick Haffie-Emslie
  • 3g
  • edge
  • gprs
  • iphone
  • mobile
  • network
  • wifi
0 Comments

3 Answers

  • Sounds like this is not possible.

    — answered June 9th 2010 by Nick Haffie-Emslie
    permalink
    0 Comments
  • I was looking for this as well and I come to conclusion that it's not possible, not even in c-obj. You could just detect the device type, if its pre-3g, it would be EDGE.

    — answered June 9th 2010 by Sj Singh
    permalink
    1 Comment
    • That's true. It wouldn't help you determine whether a 3G-capable device had 3G coverage at the moment (instead of just EDGE), but it would help to weed out pre-3G devices.

      — commented June 9th 2010 by Nick Haffie-Emslie
  • This is actually possible, but requires native modules:

    How to use:

    var utils = require('com.telogis.utils');
    
    var win = Ti.UI.createWindow({
        backgroundColor: 'white'
    });
    var label = Ti.UI.createLabel({ height: 50, left: 0, right: 0, text: 'Connection Type: ' + utils.getConnectionType() });
    win.add(label);
    win.open();
    

    Android Module:

    UtilsModule.java:

    /**
     * This file was auto-generated by the Titanium Module SDK helper for Android
     * Appcelerator Titanium Mobile
     * Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
     * Licensed under the terms of the Apache Public License
     * Please see the LICENSE included with this distribution for details.
     *
     */
    package com.telogis.utils;
    
    import org.appcelerator.kroll.KrollModule;
    import org.appcelerator.kroll.annotations.Kroll;
    
    import org.appcelerator.titanium.TiApplication;
    import org.appcelerator.kroll.common.Log;
    import org.appcelerator.kroll.common.TiConfig;
    
    
    @Kroll.module(name="Utils", id="com.telogis.utils")
    public class UtilsModule extends KrollModule
    {
    
        // Standard Debugging variables
        private static final String LCAT = "com.telogis.utils";
        private static final boolean DBG = TiConfig.LOGD;
    
        // You can define constants with @Kroll.constant, for example:
        // @Kroll.constant public static final String EXTERNAL_NAME = value;
    
        public UtilsModule()
        {
            super();
        }
    
        @Kroll.onAppCreate
        public static void onAppCreate(TiApplication app)
        {
            Log.d(LCAT, "inside onAppCreate");
            // put module init code that needs to run when the application is created
        }
    
        // Methods
        @Kroll.method
        public String getConnectionType()
        {
            return ConnectionManager.getConnectionType();
        }
    }
    

    ConnectionManager.java:

    package com.telogis.utils;
    
    import android.net.ConnectivityManager;
    import android.telephony.TelephonyManager;
    
    import org.appcelerator.kroll.KrollModule;
    import org.appcelerator.kroll.annotations.Kroll;
    
    import org.appcelerator.titanium.TiApplication;
    import org.appcelerator.kroll.common.Log;
    import org.appcelerator.kroll.common.TiConfig;
    
    public class ConnectionManager {
        public static String getConnectionType() {
            TiApplication appContext = TiApplication.getInstance();
            ConnectivityManager manager = (ConnectivityManager)appContext.getSystemService(appContext.CONNECTIVITY_SERVICE);
            android.net.NetworkInfo wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            android.net.NetworkInfo mobile = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    
            if(wifi.isAvailable()) {
                return "Wifi";
            }
             else if(mobile.isAvailable()) {
                 return getMobileConnectionType();
             }
             else
             {
                 return "None";
              }    
        }
    
        private static String getMobileConnectionType() {  
            TiApplication appContext = TiApplication.getInstance();
            TelephonyManager telManager = (TelephonyManager)appContext.getSystemService(appContext.TELEPHONY_SERVICE);
            switch (telManager.getNetworkType()) {
                    case 1: return "GPRS";
                    case 2: return "EDGE";
                    case 3: return "UMTS";
                    case 8: return "HSDPA";
                    case 9: return "HSUPA";
                    case 10: return "HSPA";
                    default: return "Unknown Mobile";
            }
        }
    }
    

    iOS Module:

    ComTelogisUtilsModule.m:

    /**
     * Your Copyright Here
     *
     * Appcelerator Titanium is Copyright (c) 2009-2010 by Appcelerator, Inc.
     * and licensed under the Apache Public License (version 2)
     */
    #import "ComTelogisUtilsModule.h"
    #import "TiBase.h"
    #import "TiHost.h"
    #import "TiUtils.h"
    
    @implementation ComTelogisUtilsModule
    
    #pragma mark Internal
    
    // this is generated for your module, please do not change it
    -(id)moduleGUID
    {
        return @"15d273d3-5437-4382-84b6-ec4888553555";
    }
    
    // this is generated for your module, please do not change it
    -(NSString*)moduleId
    {
        return @"com.telogis.utils";
    }
    
    #pragma mark Lifecycle
    
    -(void)startup
    {
        // this method is called when the module is first loaded
        // you *must* call the superclass
        [super startup];
    
        NSLog(@"[INFO] %@ loaded",self);
    }
    
    -(void)shutdown:(id)sender
    {
        // this method is called when the module is being unloaded
        // typically this is during shutdown. make sure you don't do too
        // much processing here or the app will be quit forceably
    
        // you *must* call the superclass
        [super shutdown:sender];
    }
    
    #pragma mark Cleanup 
    
    -(void)dealloc
    {
        // release any resources that have been retained by the module
        [super dealloc];
    }
    
    #pragma mark Internal Memory Management
    
    -(void)didReceiveMemoryWarning:(NSNotification*)notification
    {
        // optionally release any resources that can be dynamically
        // reloaded once memory is available - such as caches
        [super didReceiveMemoryWarning:notification];
    }
    
    #pragma mark Listener Notifications
    
    -(void)_listenerAdded:(NSString *)type count:(int)count
    {
        if (count == 1 && [type isEqualToString:@"my_event"])
        {
            // the first (of potentially many) listener is being added 
            // for event named 'my_event'
        }
    }
    
    -(void)_listenerRemoved:(NSString *)type count:(int)count
    {
        if (count == 0 && [type isEqualToString:@"my_event"])
        {
            // the last listener called for event named 'my_event' has
            // been removed, we can optionally clean up any resources
            // since no body is listening at this point for that event
        }
    }
    
    #pragma Public APIs
    
    -(id)getConnectionType:(id)args
    {
        NSNumber* connectionType = [self getConnectionTypeFromStatusBar];
        int type = [connectionType intValue];
        NSLog(@"[INFO] %d log", type);
    
        switch(type) {
            case 0: return @"None";
            case 1: return @"2G or earlier";
            case 2: return @"3G";
            case 3: return @"4G";
            case 4: return @"LTE";
            case 5: return @"Wifi";
            default: return @"Unknown";
        }
    }
    
    - (NSNumber *) getConnectionTypeFromStatusBar {
        UIApplication *app = [UIApplication sharedApplication];
        NSArray *subviews = [[[app valueForKey:@"statusBar"] valueForKey:@"foregroundView"] subviews];
        NSNumber *dataNetworkItemView = nil;
    
        for (id subview in subviews) {
            if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
                dataNetworkItemView = subview;
                break;
            }
        }
    
        return [dataNetworkItemView valueForKey:@"dataNetworkType"];
    }
    
    @end
    
    — answered July 11th 2013 by Justin Toth
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.