当前位置: 首页 > news >正文

详细介绍:Android 热点开发的相关api总结

详细介绍:Android 热点开发的相关api总结

Android 热点 

一、前言

热点开发属于系统级功能开发,涉及的核心 API 多为系统签名权限保护(如android.permission.TETHER_PRIVILEGED),通常仅系统应用(如 Settings)可正常调用。
实际开发中,除基础的开关、配置功能外,可能需要扩展自定义信道设置、频段切换等 Settings 未涵盖的功能。本文总结热点开发的核心流程、调试技巧及版本适配要点

二、热点开发

1、开关和默认配置

(1)核心 API(分版本)
  • Android 13 及以上:推荐使用ConnectivityManagerTetheringManager的公开 API,支持回调监听状态:

    // 开启热点
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    TetheringManager tetheringManager = connectivityManager.getTetheringManager();
    tetheringManager.startTethering(
    TetheringManager.TETHERING_WIFI,
    ContextCompat.getMainExecutor(context),
    new TetheringManager.StartTetheringCallback() {
    @Override
    public void onTetheringStarted() { /* 开启成功 */ }
    @Override
    public void onTetheringFailed() { /* 开启失败 */ }
    }
    );
    // 关闭热点
    tetheringManager.stopTethering(TetheringManager.TETHERING_WIFI);
  • Android 11-12:可使用WifiManagerstartTetheredHotspot(需系统签名):

    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    // 开启(使用现有配置)
    wifiManager.startTetheredHotspot(null);
    // 关闭
    wifiManager.stopTetheredHotspot();
(2)配置信息设置

通过SoftApConfiguration配置热点参数(SSID、密码、频段、信道等),需注意配置修改后需重启热点才能生效

private SoftApConfiguration buildConfig() {
SoftApConfiguration.Builder builder = new SoftApConfiguration.Builder();
builder.setSsid("MyHotspot"); // 热点名称
// 加密类型:0=无密码(OPEN),1=WPA2-PSK
builder.setPassphrase("12345678", SoftApConfiguration.SECURITY_TYPE_WPA2_PSK);
// 频段与信道(必须匹配,否则开启失败)
// 2.4G频段(band=1):信道1-14;5G频段(band=2):信道36-165
builder.setChannel(6, 1); // 2.4G信道6
return builder.build();
}
// 应用配置
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setSoftApConfiguration(buildConfig());

2、主要流程

热点开启的核心调用链

1. ConnectivityManager.startTethering() → 触发 tethering 服务请求
2. TetheringManager.startTethering() → 管理 tethering 状态与权限校验
3. TetheringService.startTethering() → 系统服务层处理请求
4. WifiManager.startTetheredHotspot() → 调用WiFi服务开启热点
5. WifiServiceImpl.startTetheredHotspot() → 实现热点启动逻辑
6. ActiveModeWarden.startSoftAp() → 管理活跃模式(AP模式)
7. SoftApManager.start() → 构建AP配置并调用底层
8. WifiNative.startSoftAp() → 与WiFi HAL交互
9. HostapdHal.addAccessPoint() → 调用硬件抽象层接口,启动hostapd进程

关键节点SoftApManager负责 AP 配置转换,HostapdHal对接硬件驱动,若某环节日志缺失,需重点排查对应层问题(如权限、硬件支持)。

3、相关日志

调试热点问题时,需重点关注以下日志关键字和过滤命令:

日志类型关键字 / 进程名过滤命令示例
Framework 层SoftApManagerWifiService`logcatgrep -iE "SoftApManagerWifiService"`
底层服务hostapdwpa_supplicant`logcatgrep -i hostapd`
HAL 与驱动交互WifiNativeWifiHAL`logcatgrep -i WifiNative`
网络接口wlanap0(接口名)`logcatgrep -i wlan1`

常见错误日志分析

  • Failed to start softap: invalid channel:信道与频段不匹配(如 5G 频段用了 2.4G 信道)。
  • SecurityException: Not allowed to start tethering:缺少系统签名或TETHER_PRIVILEGED权限。
  • hostapd: Failed to set channel:硬件不支持该信道(需检查设备支持的频段范围)。

4、相关广播

热点状态变化的核心广播为WifiManager.WIFI_AP_STATE_CHANGED_ACTION,可监听开关过程:

private BroadcastReceiver hotspotReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (WifiManager.WIFI_AP_STATE_CHANGED_ACTION.equals(intent.getAction())) {
int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_AP_STATE, -1);
switch (state) {
case WifiManager.WIFI_AP_STATE_ENABLED:
Log.d(TAG, "热点已开启");
// 可在此处获取热点IP(通过NetworkInterface)
break;
case WifiManager.WIFI_AP_STATE_FAILED:
Log.e(TAG, "热点开启失败");
break;
// 其他状态:开启中(WIFI_AP_STATE_ENABLING)、关闭中(WIFI_AP_STATE_DISABLING)、已关闭(WIFI_AP_STATE_DISABLED)
}
}
}
};
// 注册广播
IntentFilter filter = new IntentFilter(WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
context.registerReceiver(hotspotReceiver, filter);

三、其他

1、Android 热点开发调试小结

  • 权限依赖:必须为系统应用(安装在/system/priv-app)并使用系统签名,还需要导入framwork包;
  • 硬件限制:频段和信道支持依赖 WiFi 芯片,需通过WifiManager.getConfiguredNetworks()或底层日志确认设备能力。
// 依赖ZXing库:implementation 'com.google.zxing:core:3.5.0'
private Bitmap generateHotspotQrCode(String ssid, String password) {
// 二维码内容格式:WIFI:S:热点名;T:加密类型(WPA/WEP/OPEN);P:密码;;
String content = "WIFI:S:" + ssid + ";T:WPA;P:" + password + ";;";
MultiFormatWriter writer = new MultiFormatWriter();
try {
BitMatrix matrix = writer.encode(
content,
BarcodeFormat.QR_CODE,
300, 300, // 二维码宽高
new HashMap<>()
);
// 转换BitMatrix为Bitmap
int width = matrix.getWidth();
int height = matrix.getHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
bitmap.setPixel(x, y, matrix[x][y] ? Color.BLACK : Color.WHITE);
}
}
return bitmap;
} catch (WriterException e) {
e.printStackTrace();
return null;
}
}

http://www.agseo.cn/news/147/

相关文章:

  • 工业主板:工业自动化与智能设备的强大心脏
  • 十大经典排序算法 - lucky
  • 深度学习入门基于python
  • 2025网络赛1 C、D
  • 图像配准尝试
  • TypeScript索引访问类型详解
  • 【URP】Unity Shader Tags
  • 存储器的性能指标 计算机组成原理第三章
  • 基于Operator方式和二进制方式部署prometheus环境
  • 安全不是一个功能-而是一个地基
  • 你的错误处理一团糟-是时候修复它了-️
  • idea gitee 更新已取消 解决方案
  • 27家网省
  • 你的测试又慢又不可靠-因为你测错了东西
  • 国内人力资源信息管理软件排行:选红海云一体化人力HR系统
  • 历年 CSP-J/S 数学类真题知识点整理
  • Log4j2 CVE-2021-44228 漏洞复现
  • AI Compass前沿速览:字节Seedream4.0、Qwen3-Max、EmbeddingGemma、OneCAT多模态、rStar2-Agent
  • 使用DeepState进行API模糊测试的技术解析(第二部分)
  • TeX 的 ctex 宏包的基本用法
  • 原子操作并不能保证数值的准确与一致性
  • Linux 进程管理之软硬限制以及企业应用实践
  • mybatis-plus引入
  • 79、制作表头不能用合并后居中
  • 智能血压计芯片解决方案AI版
  • 408 Request Timeout:请求超时,服务器等待客户端发送请求的时间过长。
  • 01bfs 对 dij最短路的优化,以及一些易错点
  • MySQL约束
  • 数据结构与算法-21.堆-排序
  • Avalonia 学习笔记01. Images Buttons(图片与按钮) (转载)