Android之把手机的3g流量共享出来让其他人连接这个wifi

时间:2023-03-09 19:15:46
Android之把手机的3g流量共享出来让其他人连接这个wifi

转自:http://blog.csdn.net/luoboo525/article/details/7883998   亲测可用

用过快牙的朋友应该知道它们在两天设备之间传输文件的时候使用的是wifi热点,然后另一台便连接这个热点再进行传输。快牙传输速度惊人应该跟它的这种机制有关系吧。不知道它的搜索机制是怎样的,但我想应该可以通过热点的名字来进行判断吧。下面我们就来探讨一下如何自动创建一个wifi热点吧Android之把手机的3g流量共享出来让其他人连接这个wifi

创建wifi热点首先需要手机支持,建议开发的哥们整个好点的手机,我们公司那些个山寨设备,几近有一半是不支持热点的;其实创建热点很简单,先获取到wifi的服务,再配置热点名称、密码等等,然后再通过反射打开它就OK了。

下面我们看看创建热点的代码实现:

  1. package com.tel.lajoin.wifi.hotspot;
  2. import java.lang.reflect.Method;
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.net.wifi.WifiConfiguration;
  6. import android.net.wifi.WifiManager;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.Button;
  10. public class HotspotActivity extends Activity {
  11. private WifiManager wifiManager;
  12. private Button open;
  13. private boolean flag=false;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. // TODO Auto-generated method stub
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.main);
  19. //获取wifi管理服务
  20. wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
  21. open=(Button)findViewById(R.id.open_hotspot);
  22. //通过按钮事件设置热点
  23. open.setOnClickListener(new View.OnClickListener() {
  24. @Override
  25. public void onClick(View v) {
  26. //如果是打开状态就关闭,如果是关闭就打开
  27. flag=!flag;
  28. setWifiApEnabled(flag);
  29. }
  30. });
  31. }
  32. // wifi热点开关
  33. public boolean setWifiApEnabled(boolean enabled) {
  34. if (enabled) { // disable WiFi in any case
  35. //wifi和热点不能同时打开,所以打开热点的时候需要关闭wifi
  36. wifiManager.setWifiEnabled(false);
  37. }
  38. try {
  39. //热点的配置类
  40. WifiConfiguration apConfig = new WifiConfiguration();
  41. //配置热点的名称(可以在名字后面加点随机数什么的)
  42. apConfig.SSID = "YRCCONNECTION";
  43. //配置热点的密码
  44. apConfig.preSharedKey="12122112";
  45. //通过反射调用设置热点
  46. Method method = wifiManager.getClass().getMethod(
  47. "setWifiApEnabled", WifiConfiguration.class, Boolean.TYPE);
  48. //返回热点打开状态
  49. return (Boolean) method.invoke(wifiManager, apConfig, enabled);
  50. } catch (Exception e) {
  51. return false;
  52. }
  53. }
  54. }

布局就不写了吧,就一按钮,人人都知道的东西,写了也没啥意思。要实现文件传输,当然我们还需要写一个连接热点的客户端吧。连接热点的流程首先是搜索热点然后再判断热点是否符合规则然后再进行连接。

  1. package com.tel.lajoin.wifiscan;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.app.Activity;
  5. import android.content.BroadcastReceiver;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.content.IntentFilter;
  9. import android.net.wifi.ScanResult;
  10. import android.net.wifi.WifiConfiguration;
  11. import android.net.wifi.WifiManager;
  12. import android.os.Bundle;
  13. public class MainActivity extends Activity {
  14. private List<ScanResult> wifiList;
  15. private WifiManager wifiManager;
  16. private List<String> passableHotsPot;
  17. private WifiReceiver wifiReceiver;
  18. private boolean isConnected=false;
  19. private Button connect;
  20. @Override
  21. public void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. init();
  24. }
  25. /* 初始化参数 */
  26. public void init() {
  27. setContentView(R.layout.main);
  28. connect=(Button)findViewById(R.id.connect);
  29. wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
  30. wifiReceiver = new WifiReceiver();
  31. //通过按钮事件搜索热点
  32. connect.setOnClickListener(new View.OnClickListener() {
  33. @Override
  34. public void onClick(View v) {
  35. wifiManager.startScan();
  36. }
  37. });
  38. }
  39. /* 监听热点变化 */
  40. private final class WifiReceiver extends BroadcastReceiver {
  41. @Override
  42. public void onReceive(Context context, Intent intent) {
  43. wifiList = wifiManager.getScanResults();
  44. if (wifiList == null || wifiList.size() == 0 || isConnected)
  45. return;
  46. onReceiveNewNetworks(wifiList);
  47. }
  48. }
  49. /*当搜索到新的wifi热点时判断该热点是否符合规格*/
  50. public void onReceiveNewNetworks(List<ScanResult> wifiList){
  51. passableHotsPot=new ArrayList<String>();
  52. for(ScanResult result:wifiList){
  53. System.out.println(result.SSID);
  54. if((result.SSID).contains("YRCCONNECTION"))
  55. passableHotsPot.add(result.SSID);
  56. }
  57. synchronized (this) {
  58. connectToHotpot();
  59. }
  60. }
  61. /*连接到热点*/
  62. public void connectToHotpot(){
  63. if(passableHotsPot==null || passableHotsPot.size()==0)
  64. return;
  65. WifiConfiguration wifiConfig=this.setWifiParams(passableHotsPot.get(0));
  66. int wcgID = wifiManager.addNetwork(wifiConfig);
  67. boolean flag=wifiManager.enableNetwork(wcgID, true);
  68. isConnected=flag;
  69. System.out.println("connect success? "+flag);
  70. }
  71. /*设置要连接的热点的参数*/
  72. public WifiConfiguration setWifiParams(String ssid){
  73. WifiConfiguration apConfig=new WifiConfiguration();
  74. apConfig.SSID="\""+ssid+"\"";
  75. apConfig.preSharedKey="\"12122112\"";
  76. apConfig.hiddenSSID = true;
  77. apConfig.status = WifiConfiguration.Status.ENABLED;
  78. apConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
  79. apConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
  80. apConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
  81. apConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
  82. apConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
  83. apConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
  84. return apConfig;
  85. }
  86. @Override
  87. protected void onDestroy() {
  88. super.onDestroy();
  89. /*销毁时注销广播*/
  90. unregisterReceiver(wifiReceiver);
  91. }
  92. }

代码很简单,而且都有注释的,相信大伙儿能够看明白。  那就这样吧,至于文件传输建议还是去看看socket相关的文章吧。