In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
How to use android code to set and open wifi hotspots and hot spots connections, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
Friends who use too fast teeth should know that they use the wifi hotspot when transferring files between two-day devices, and then the other one connects to the hotspot and transfers it. The amazing transmission speed of fast teeth should have something to do with its mechanism. I don't know what its search mechanism is, but I think it can be judged by the name of the hotspot. Let's talk about how to automatically create a wifi hotspot.
To create a wifi hotspot, you first need mobile phone support. It is recommended to develop a better mobile phone. Nearly half of the fake devices in our company do not support hotspots. In fact, it is very simple to create a hotspot, first get the wifi service, then configure the hotspot name, password, etc., and then open it through reflection to OK.
Let's take a look at the code implementation that creates hotspots:
[html] view
Plaincopy
Package com.tel.lajoin.wifi.hotspot
Import java.lang.reflect.Method
Import android.app.Activity
Import android.content.Context
Import android.net.wifi.WifiConfiguration
Import android.net.wifi.WifiManager
Import android.os.Bundle
Import android.view.View
Import android.widget.Button
Public class HotspotActivity extends Activity {
Private WifiManager wifiManager
Private Button open
Private boolean flag=false
@ Override
Protected void onCreate (Bundle savedInstanceState) {
/ / TODO Auto-generated method stub
Super.onCreate (savedInstanceState)
SetContentView (R.layout.main)
/ / obtain wifi management service
WifiManager = (WifiManager) getSystemService (Context.WIFI_SERVICE)
Open= (Button) findViewById (R.id.open_hotspot)
/ / set hotspots through button events
Open.setOnClickListener (new View.OnClickListener () {
@ Override
Public void onClick (View v) {
/ / close if it is open, open if it is closed
Yes, please.
SetWifiApEnabled (flag)
}
});
}
/ / wifi hotspot switch
Public boolean setWifiApEnabled (boolean enabled) {
If (enabled) {/ / disable WiFi in any case
/ / wifi and hotspots cannot be turned on at the same time, so you need to turn off wifi when you open hotspots.
WifiManager.setWifiEnabled (false)
}
Try {
/ / configuration class of hotspot
WifiConfiguration apConfig = new WifiConfiguration ()
/ / configure the name of the hotspot (you can add a random number after the name)
ApConfig.SSID = "YRCCONNECTION"
/ / configure the password for the hotspot
ApConfig.preSharedKey= "12122112"
/ / set hotspots through reflection calls
Method method = wifiManager.getClass () .getMethod (
"setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE)
/ / return to the hot spot open status
Return (Boolean) method.invoke (wifiManager, apConfig, enabled)
} catch (Exception e) {
Return false
}
}
}
Don't write about the layout, just a button, what everyone knows, it doesn't make sense to write. To achieve file transfer, of course, we also need to write a client that connects to hot spots. The process of connecting hotspots is to search for hotspots and then determine whether the hotspots conform to the rules and then connect.
[html] view
Plaincopy
Package com.tel.lajoin.wifiscan
Import java.util.ArrayList
Import java.util.List
Import android.app.Activity
Import android.content.BroadcastReceiver
Import android.content.Context
Import android.content.Intent
Import android.content.IntentFilter
Import android.net.wifi.ScanResult
Import android.net.wifi.WifiConfiguration
Import android.net.wifi.WifiManager
Import android.os.Bundle
Public class MainActivity extends Activity {
Private List wifiList
Private WifiManager wifiManager
Private List passableHotsPot
Private WifiReceiver wifiReceiver
Private boolean isConnected=false
Private Button connect
@ Override
Public void onCreate (Bundle savedInstanceState) {
Super.onCreate (savedInstanceState)
Init ()
}
/ * initialization parameters * /
Public void init () {
SetContentView (R.layout.main)
Connect= (Button) findViewById (R.id.connect)
WifiManager = (WifiManager) getSystemService (Context.WIFI_SERVICE)
WifiReceiver = new WifiReceiver ()
/ / search for hotspots through button events
Connect.setOnClickListener (new View.OnClickListener () {
@ Override
Public void onClick (View v) {
WifiManager.startScan ()
}
});
}
/ * listen for changes in hotspots * /
Private final class WifiReceiver extends BroadcastReceiver {
@ Override
Public void onReceive (Context context, Intent intent) {
WifiList = wifiManager.getScanResults ()
If (wifiList = = null | | wifiList.size () = = 0 | | isConnected)
Return
OnReceiveNewNetworks (wifiList)
}
}
/ * determine whether a new wifi hotspot conforms to the specification when searching for it * /
Public void onReceiveNewNetworks (List wifiList) {
PassableHotsPot=new ArrayList ()
For (ScanResult result:wifiList) {
System.out.println (result.SSID)
If ((result.SSID) .clients ("YRCCONNECTION"))
PassableHotsPot.add (result.SSID)
}
Synchronized (this) {
ConnectToHotpot ()
}
}
/ * Connect to a hotspot * /
Public void connectToHotpot () {
If (passableHotsPot==null | | passableHotsPot.size () = = 0)
Return
WifiConfiguration wifiConfig=this.setWifiParams (passableHotsPot.get (0))
Int wcgID = wifiManager.addNetwork (wifiConfig)
Boolean flag=wifiManager.enableNetwork (wcgID, true)
IsConnected=flag
System.out.println ("connect success?" + flag)
}
/ * set the parameters of the hotspot to be connected * /
Public WifiConfiguration setWifiParams (String ssid) {
WifiConfiguration apConfig=new WifiConfiguration ()
ApConfig.SSID= "\" + ssid+ "\"
ApConfig.preSharedKey= "\" 12122112\ ""
ApConfig.hiddenSSID = true
ApConfig.status = WifiConfiguration.Status.ENABLED
ApConfig.allowedGroupCiphers.set (WifiConfiguration.GroupCipher.TKIP)
ApConfig.allowedGroupCiphers.set (WifiConfiguration.GroupCipher.CCMP)
ApConfig.allowedKeyManagement.set (WifiConfiguration.KeyMgmt.WPA_PSK)
ApConfig.allowedPairwiseCiphers.set (WifiConfiguration.PairwiseCipher.TKIP)
ApConfig.allowedPairwiseCiphers.set (WifiConfiguration.PairwiseCipher.CCMP)
ApConfig.allowedProtocols.set (WifiConfiguration.Protocol.RSN)
Return apConfig
}
@ Override
Protected void onDestroy () {
Super.onDestroy ()
/ * Log out of the broadcast on destruction * /
UnregisterReceiver (wifiReceiver)
}
}
The code is very simple, and there are comments, I believe everyone can understand. That's it. As for the file transfer advice, take a look at the articles related to socket.
After reading the above, have you mastered how to set up and open wifi hotspots and hotspots with android code? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.