Setting模块---热点

Setting模块的热点

首先来说布局的加载

启动的lancheractivitySettingActivity,这个Activity中的oncreate()方法的最后


else{
//No UP affordanceif we are displaying the main Dashboard
mDisplayHomeAsUpEnabled= false;
//Show Search affordance
mDisplaySearch= true;
mInitialTitleResId= R.string.dashboard_title;
switchToFragment(DashboardSummary.class.getName(),null,false,false,
mInitialTitleResId,mInitialTitle,false);
}


会去启动一个DashboardSummary,在这个FragmentonResume()方法中的sendRebuildUI();

这个方法实际是在发送一个消息,在这个Fragment中创建了一个Handler,在不断的迭代消息,接收到消息后

执行rebuildUI(context);方法,这个方法中的

List<DashboardCategory>categories =
((SettingsActivity)context).getDashboardCategories(true);

然后在这个方法内部buildDashboardCategories(mCategories);这个方法内部

loadCategoriesFromResource(R.xml.dashboard_categories,categories,this);

加载布局,这个方法实际是去解析xml



总结:首先是创建了一个集合privateArrayList<DashboardCategory>mCategories=newArrayList<DashboardCategory>();然后遍历这个Xml,然后把属性放入这个集合。

回到这个rebuildUI(context)方法,newDashboardTileView对象,将集合中的数据添加到这个对象

最终每个选项会是一个DashboardTileView对象,在这个对象中自身有一个监听事件,进入不同的Fragment


首先这个热点这个在TetherSettings类中

Oncreate方法中使用addPreferencesFromResource(R.xml.tether_prefs);这个方法中执行了

setPreferenceScreen(mPreferenceManager.inflateFromResource(getActivity(),
preferencesResId,getPreferenceScreen()));


这个方法中的参数mPreferenceManager.inflateFromResource(getActivity(),preferencesResId,getPreferenceScreen())

首先getPreferenceScreen()方法得到的是自定义控件,也就是R.xml.tether_prefs的的自定义控件

setPreferenceScreen()方法是在设置这个控件显示,

在这个R.xml.tether_prefs布局中有两个便携式wifi热点的控件,


<PreferenceScreenxmlns:android="http://schemas.android.com/apk/res/android">

<SwitchPreference
android:key="usb_tether_settings"
android:title="@string/usb_tethering_button_text" USB
android:persistent="false"/>

<SwitchPreference
android:key="enable_wifi_ap"
android:title="@string/wifi_tether_checkbox_text" 便携式Wifi热点
android:persistent="false"/>

<com.android.settings.HotspotPreference
android:key="enable_wifi_ap_ext"
android:title="@string/wifi_tether_checkbox_text" 便携式Wifi热点
android:persistent="false"/>

<Preference
android:key="wifi_ap_ssid_and_security"
android:title="@string/wifi_tether_configure_ap_text" 便携式WIFI设置
android:persistent="false"/>
<!-- -->
<SwitchPreference
android:key="enable_bluetooth_tethering"
android:title="@string/bluetooth_tether_checkbox_text" 蓝牙
android:persistent="false"/>
</PreferenceScreen>


通过判断显示那一个便携式wifi热点,相应的移除另一个


布局显示完后,

通过这个对象ConnectivityManager得到蓝牙wifiUSB的列表

mUsbRegexs= cm.getTetherableUsbRegexs();
mWifiRegexs= cm.getTetherableWifiRegexs();
mBluetoothRegexs= cm.getTetherableBluetoothRegexs();


根据相应列表的长度移除控件,根据mWifiApEnabler的值初始化便携式WIFI热点(没有卡)

最后得到配置的应用程序??

mProvisionApp= getResources().getStringArray(
com.android.internal.R.array.config_mobile_hotspot_provision_app);


onstart()方法中注册广播,

最后更新状态,更新蓝牙和USB状态

updateState();



响应事件:

三个响应时间

蓝牙USB onPreferenceTreeClick(PreferenceScreen screen, Preferencepreference)

便携式WIFI onPreferenceChange(Preference preference, Object value)

便携式WIFI热点设置onPreferenceTreeClick()// onCreateDialog() onClick(DialogInterface dialogInterface,intbutton)


蓝牙USB

startProvisioningIfNecessary()

都会去执行这个方法,只是参数不同

在这个方法中会去startActivityForResult()或者这个startTethering()

在执行startActivityForResult()的回调方法onActivityResult()

当打开成功他会利用scheduleRecheckAlarm()方法开启TetherService

然后接着执行startTethering()这个方法,将控件打开

当打开失败时,他会将蓝牙或者USB开关关闭



便携式WIFI热点设置

跟上面一样也是在onPreferenceTreeClick()中去判断的

elseif(preference == mCreateNetwork){//
showDialog(DIALOG_AP_SETTINGS);
}


这个方法会去创建SettingsDialogFragment,并执行它的show方法,show方法是将这个创建的Fragment加入,在这个SettingsDialogFragmentoncreate()方法中回去创建一个WifiApDialog,创建的Dialog的代码在TetherSettings类中的onCreateDialog()方法中,这个方法创建了WifiApDialog

弹出的对话框是WifiApDialog,在这个类中的如下方法解析Xml

protectedvoidonCreate(Bundle savedInstanceState) {
booleanmInit = true;
mView= getLayoutInflater().inflate(R.layout.wifi_ap_dialog,null);
Spinner mSecurity = ((Spinner)mView.findViewById(R.id.security));
mSsid= (TextView) mView.findViewById(R.id.ssid);//名称
mPassword= (EditText) mView.findViewById(R.id.password);//密码
SpinnermSecurity = ((Spinner) mView.findViewById(R.id.security));//安全性的下拉列表
finalSpinner mChannel = (Spinner)mView.findViewById(R.id.choose_channel);//频段的下拉列表

setButton(BUTTON_SUBMIT,context.getString(R.string.wifi_save), mListener);
setButton(DialogInterface.BUTTON_NEGATIVE,context.getString(R.string.wifi_cancel),mListener);//Dialog的按钮



当数据填写好后,用户点击保存会执行一个Onclick()方法,这个方法在TetherSettings类中



publicvoid onClick(DialogInterfacedialogInterface, int button) {
if(button == DialogInterface.BUTTON_POSITIVE){
mWifiConfig= mDialog.getConfig();
if(mWifiConfig!= null){
if(mWifiManager.getWifiApState()== WifiManager.WIFI_AP_STATE_ENABLED){
mWifiManager.setWifiApEnabled(null,false);
mWifiManager.setWifiApEnabled(mWifiConfig,true);
} else{
mWifiManager.setWifiApConfiguration(mWifiConfig);
}
intindex = WifiApDialog.getSecurityTypeIndex(mWifiConfig);
mCreateNetwork.setSummary(String.format(getActivity().getString(CONFIG_SUBTEXT),
mWifiConfig.SSID,
mSecurityType[index]));
}
}
}


从这段代码中我们可以看到利用WifiManager对象wifi热点进行设置,最后再更新这个wifi热点的值,

这个dialog中的数据全部存储在WifiConfiguration对象中,利用wifiMagenerWifiConfiguration对象传下去,在显示的时候得到这个对象就可以显示了。

对于preference控件有自己的存储数据的方法,自动保存


http://www.it165.net/pro/html/201410/23922.html


### ESP-01s 通讯程序流程图设计 ESP-01s 是一种基于 ESP8266 的 Wi-Fi 模块,广泛应用于物联网设备中。其通讯程序的设计通常围绕 AT 命令集展开,因为该模块默认支持通过串口接收和执行 AT 命令来配置网络参数并实现数据传输功能[^3]。 以下是关于 ESP-01s 通讯程序的典型流程图设计说明: #### 流程概述 整个通讯过程可以分为以下几个阶段: 1. **初始化阶段** 配置硬件接口(UART),设置波特率、校验位等参数,并发送初始命令以确认模块状态。 2. **Wi-Fi 连接阶段** 使用 `AT+CWMODE` 设置工作模式(Station/SoftAP/Sta+Ap)。随后通过 `AT+CWJAP` 或其他相关指令连接到指定的 AP 热点。 3. **TCP/IP 协议栈操作阶段** 创建客户端或服务器实例,绑定端口号并通过特定命令建立 TCP 或 UDP 数据通道。 4. **数据交互阶段** 发送请求至远程主机或者监听来自外部的数据包;解析返回的结果反馈给主控单元处理。 5. **断开连接与资源释放阶段** 当不再需要保持在线时关闭会话链接,清理占用内存空间以便下次重新启动服务。 #### 具体步骤描述 下面给出更详细的伪代码表示方法以及对应的逻辑框图结构建议: ```plaintext Start -> Initialize UART Settings (Baud Rate etc.) -> Check Module Response ("ATE0", "AT") -> Set WiFi Mode via CWMODE Command -> Connect to Access Point using CWJAP Cmd -> Establish Socket Connection with Remote Host If Client then use AT+CIPSTART="TCP","<ip>","<port>" Else as Server listen on port number by setting up appropriate parameters. -> Send Data Through Created Channel & Receive Acknowledgement Back From Peer Endpoints. -> Disconnect After Communication Ends By Issuing Proper Termination Directives Like 'CIPCLOSE'. End Program Execution Safely Releasing All Allocated Resources Including Buffers Etc.. ``` 对于图形化展示部分,则可以根据以上文字叙述构建类似的矩形节点箭头指向关系图表形式呈现出来更加直观易懂一些。 --- ### 示例代码片段 这里提供一段简单的 Arduino 平台下的示例代码用于演示如何利用软件库驱动 ESP-01S 完成基本联网任务: ```cpp #include <SoftwareSerial.h> // Define RX and TX pins connected between MCU and ESP module respectively. #define EspRxPin D7 // Change according actual wiring setup accordingly! #define EspTxPin D8 // SoftwareSerial espSerial(EspRxPin, EspTxPin); void setup(){ Serial.begin(9600); delay(1000); /* Start communication */ espSerial.begin(115200); String response = sendCommand("AT\r\n"); // Test if device responds correctly after power-on reset sequence executed internally within firmware itself already. if(response.indexOf("OK") != -1){ Serial.println("Module Ready!"); }else{ while(true){} // Stop further execution until fixed manually since something went wrong here... } } String sendCommand(String cmdStr,int timeout=2000){ char buff[128]={}; unsigned long startMillis=millis(); espSerial.print(cmdStr); int indexPos=0; do { while(espSerial.available()){ buff[indexPos++]=espSerial.read();if(indexPos>=sizeof(buff)-1)break;}}while((millis()-startMillis)<timeout); return String(buff).trim(); } ``` 注意此仅为简化版框架示意用途,请依据实际情况调整相应参数值匹配目标应用场景需求特点再做适当修改优化即可满足大多数常规开发场景所需基础能力要求范围之内。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值