我是Android系统的新手。 这是否正确,Android 2.2.1仅能检测非ad hoc无线网络? 我想知道是否有办法将我的Android连接到从笔记本电脑建立的ad hoc网络。
你说得对,目前Android尚不支持这个功能,但自从Android正式发布以来,谷歌一直在表示将会推出该功能。
虽然不是原生支持,但迄今为止发布的每个Android设备上的硬件都支持它。它只是在软件中被禁用了,您需要启用它才能使用这些功能。
然而,实现这一点相当容易,但您需要获取root权限,而且具体细节可能在不同的设备中略有不同。获取更多信息的最佳来源是XDA开发者社区:http://forum.xda-developers.com/forumdisplay.php?f=564。 大多数现有的解决方案都基于替换wpa_supplicant,并且如果可能的话,这也是我推荐的方法。有关更多详细信息,请参见http://szym.net/2010/12/adhoc-wifi-in-android/。
更新:几年过去了,每当我需要在手机上建立一个adhoc网络连接时,我会使用CyanogenMod。它可以给您程序化和脚本化访问这些功能的能力,并且可以在WiFi设置菜单中创建adhoc(ibss)网络。
*一些具有 Android 2.3 的手机具有专有的操作系统扩展名,可启用 Wi-Fi Direct (大多数较新的三星手机),但 Android 4 应完全支持此功能 (source)。
netsh wlan set hostednetwork mode=allow ssid=NAME key=PASSWORD
netsh wlan start hostednetwork
netsh wlan stop hostednetwork
就是这样!
public class MainActivity extends Activity {
private static final String CHIPKIT_BSSID = "E2:14:9F:18:40:1C";
private static final int CHIPKIT_WIFI_PRIORITY = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button btnDoSomething = (Button) findViewById(R.id.btnDoSomething);
final Button btnNewScan = (Button) findViewById(R.id.btnNewScan);
final TextView textWifiManager = (TextView) findViewById(R.id.WifiManager);
final TextView textWifiInfo = (TextView) findViewById(R.id.WifiInfo);
final TextView textIp = (TextView) findViewById(R.id.Ip);
final WifiManager myWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
final WifiInfo myWifiInfo = myWifiManager.getConnectionInfo();
WifiConfiguration wifiConfiguration = new WifiConfiguration();
wifiConfiguration.BSSID = CHIPKIT_BSSID;
wifiConfiguration.priority = CHIPKIT_WIFI_PRIORITY;
wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
wifiConfiguration.allowedKeyManagement.set(KeyMgmt.NONE);
wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wifiConfiguration.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wifiConfiguration.status = WifiConfiguration.Status.ENABLED;
myWifiManager.setWifiEnabled(true);
int netID = myWifiManager.addNetwork(wifiConfiguration);
myWifiManager.enableNetwork(netID, true);
textWifiInfo.setText("SSID: " + myWifiInfo.getSSID() + '\n'
+ myWifiManager.getWifiState() + "\n\n");
btnDoSomething.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
clearTextViews(textWifiManager, textIp);
}
});
btnNewScan.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getNewScan(myWifiManager, textWifiManager, textIp);
}
});
}
private void clearTextViews(TextView...tv) {
for(int i = 0; i<tv.length; i++){
tv[i].setText("");
}
}
public void getNewScan(WifiManager wm, TextView...textViews) {
wm.startScan();
List<ScanResult> scanResult = wm.getScanResults();
String scan = "";
for (int i = 0; i < scanResult.size(); i++) {
scan += (scanResult.get(i).toString() + "\n\n");
}
textViews[0].setText(scan);
textViews[1].setText(wm.toString());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
不要忘记在Eclipse中使用Ctrl+Shift+[字母O]来填充缺失的导入...
以及我的清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.digilent.simpleclient"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>