android11上wifi开发完整案例
时间: 2023-05-28 10:02:33 浏览: 173
以下是一个在Android 11上的WiFi开发完整案例:
1. Manifest文件配置权限和服务。
```xml
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<service
android:name="com.example.wifiservice.WifiService"
android:enabled="true"
android:exported="false"/>
```
2. 创建Service类,实现WifiManager的各种相关操作。其中需要注意的是,在Android 11及以上版本,通过WifiManager获取到的Wifi信息已经不再包含SSID等敏感信息,需要通过registerNetworkCallback或者扫描接口获取到。
```java
public class WifiService extends Service {
private final String TAG = WifiService.class.getSimpleName();
private WifiManager wifiManager;
private Context context;
private WifiScanResultListener scanResultListener;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "WifiService onCreate");
wifiManager = (WifiManager) this.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
context = this.getApplicationContext();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "WifiService onStartCommand");
if (intent != null) {
String action = intent.getAction();
if (action != null && action.equals("start_scan")) {
startScan();
}
}
return START_STICKY;
}
/**
* 开始扫描Wifi
*/
private void startScan() {
wifiManager.startScan();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
registerNetworkCallback();
} else {
scanResultListener = new WifiScanResultListener();
IntentFilter filter = new IntentFilter();
filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
context.registerReceiver(scanResultListener, filter);
}
}
/**
* Android 11及以上版本通过registerNetworkCallback获取Wifi信息
*/
@RequiresApi(api = Build.VERSION_CODES.R)
private void registerNetworkCallback() {
NetworkRequest.Builder builder = new NetworkRequest.Builder();
builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
final ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
connectivityManager.registerNetworkCallback(builder.build(),
new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
Log.d(TAG, "onAvailable network=" + network);
List<ScanResult> scanResults = wifiManager.getScanResults();
if (scanResults != null && scanResults.size() > 0) {
for (ScanResult scanResult : scanResults) {
Log.d(TAG, "onAvailable scanResult=" + scanResult.toString());
}
}
}
});
}
}
/**
* Android 11以下版本通过扫描接口获取Wifi信息
*/
private class WifiScanResultListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action != null && action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) {
List<ScanResult> scanResults = wifiManager.getScanResults();
if (scanResults != null && scanResults.size() > 0) {
for (ScanResult scanResult : scanResults) {
Log.d(TAG, "onReceive scanResult=" + scanResult.toString());
}
}
}
}
}
}
```
3. 在Activity中控制Service的开启和关闭。
```java
public class MainActivity extends AppCompatActivity {
private Button btnStart, btnStop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStart = findViewById(R.id.btn_start);
btnStop = findViewById(R.id.btn_stop);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, WifiService.class);
intent.setAction("start_scan");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent);
} else {
startService(intent);
}
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, WifiService.class);
stopService(intent);
}
});
}
}
```
以上就是一个在Android 11上的WiFi开发完整案例,通过使用Service和WifiManager,实现了对WiFi的扫描和获取WiFi信息的功能,并根据不同Android版本做了不同的处理。
阅读全文