Android accesses the Internet through a wired USB Nic

Source: Internet
Author: User

The USB Nic is divided into wireless 3G and wired, and wireless 3G Nic has been implemented by my colleagues. I recently implemented wired Nic. That is, connect to the mid through RJ45 to USB, and the driver will register the ethx network device. What I want to achieve is to operate the network device.

 

Connectivityservice is a service that manages connections. It manages multiple network types of tracker, such as wifistatetracker and mobiledatastatetracker. Similarly, I need to implement usbnetworkstatetracker.

 

Connectivityservice will pass a handler callback when instantiating a tracker. If the status of a tracker changes (signal strength, connection status, etc.), a message will be sent through the callback, connectivityservice sends intent to the application layer based on status changes. A typical example is the action after successful data connection:

Private void sendconnectedbroadcast (networkinfo info) {<br/> intent = new intent (connectivitymanager. connectivity_action); <br/> intent. addflags (intent. flag_receiver_replace_pending); <br/> intent. putextra (connectivitymanager. extra_network_info, Info); <br/> If (info. isfailover () {<br/> intent. putextra (connectivitymanager. extra_is_failover, true); <br/> info. setfailover (false); <br/>}< br /> If (info. getreason ()! = NULL) {<br/> intent. putextra (connectivitymanager. extra_reason, info. getreason (); <br/>}< br/> If (info. getextrainfo ()! = NULL) {<br/> intent. putextra (connectivitymanager. extra_extra_info, <br/> info. getextrainfo (); <br/>}< br/> sendstickybroadcast (intent); <br/>}< br/> 


 

All trackers inherit from networkstatetracker, while networkstatetracker inherits from handler. networkstatetracker has a networkinfo instance to store the status of the current network type. Applications can use the getallnetworkinfo () of connectivitymanager on the connectivityservice client () to obtain these instances.

 

The difference between usbnetworkstatetracker and wifistatetracker is that WiFi has its own service, and applications can call the wifiservice client wifimanager to manipulate the WiFi device and then call wfistatetracker, I can only operate usbnetworkstatetracker through custom messages,

Two custom actions include:

Public static final string action_usb_network_start = "android.net. USB. network_start"; <br/> Public static final string action_usb_network_stop = "android.net. USB. network_stop"; <br/> 

The application sends the intent containing the action. After receiving the message, usbnetworkstatetracker performs the action.

In addition, usbnetworkstatetracker should be able to identify the names of currently available network devices. networkmanagementservice provides an interface inetworkmanagementeventobserver, as long as it inherits the three callback methods in this interface,

Interface inetworkmanagementeventobserver {<br/>/** <br/> * interface link status has changed. <br/> * @ Param iface the interface. <br/> * @ Param link true if Link is up. <br/> */<br/> void interfacelinkstatuschanged (string iface, Boolean link ); <br/>/** <br/> * an interface has been added to the system <br/> * @ Param iface the interface. <br/> */<br/> void interfaceadded (string iface ); <br/>/** <br/> * an interface has been removed from the system <br/> * @ Param iface the interface. <br/> */<br/> void interfaceremoved (string iface); <br/>}< br/> 

Then use the following code to register this interface

// Register for communications from networkmanagement service <br/> ibinder B = servicemanager. getservice (context. networkmanagement_service); <br/> inetworkmanagementservice service = inetworkmanagementservice. stub. asinterface (B); <br/> try {<br/> service. registerobserver (minterfaceobserver); <br/>} catch (RemoteException e) {<br/> log. E (TAG, "error registering observer:" + E); <br/>}< br/> 

In this way, your tracker will be able to monitor the network device plugging, so as to maintain compatibility with WiFi, otherwise you do not know whether the WiFi or USB Nic corresponding to eth0 and eth1.

 

Usbnetworkstatetracker calls networkutils to manipulate network devices. A typical method is to execute DHCP by calling

Public native static Boolean rundhcp (string interfacename, dhcpinfo ipinfo ); 

Call the function in dhcpclient. C through JNI.

 

If you do not want to add tracker, you do not have the permission to directly call networkutils In the APK, even if you have obtained all the network permissions, or even set Android: shareduserid to Android in androidmanifest. UID. none of them work. I haven't figured out the reason yet. In fact, it is okay to implement a tracker. You don't have to manage some intermediate states on your own, and it is very friendly to the application layer.

Adding a tracker will change the android API and may encounter compilation problems. You need to go to the project directory (for example,/All/froyo /)

Run the following command: Make Update-API; make product-SDK;

Reference: http://hi.baidu.com/programmar/blog/item/732776ea307360dfd439c938.html

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.