*/
disconnect() {}
/**
* 藍牙連接成功
* @see UnConnectState
*/
connectSuccess() {}
/**
* 藍牙連接失敗
* @see UnConnectState
*/
connectFail() {}
/**
* 路由數(shù)據(jù)接收完成
* @see ReceivedState
*/
received() {}
/**
* 認證中
* @see AuthenticatingState
*/
authenticating() {}
/**
* 認證完成
* @see AuthenticatedState
*/
authenticated() {}
/**
* 充電完成
* @see WifiConnectingState
*/
wifiConnecting() {}
/**
* 時間同步完成
* @see WifiConnectedState
*/
wifiConnected() {}
/**
* 獲取狀態(tài)ID
*/
getStateId() {
return 0;
}
/**
* 釋放資源
* @see ConnectedState
*/
onRelease() {}
}
module.exports = AbstractState
### 實現(xiàn)各狀態(tài)類
#### UnConnectState類
import AbstractState from ‘./AbstractState’
class UnConnectState extends AbstractState { // 未連接狀態(tài)只處理連接的事件
constructor(stateController) {
super(stateController);
}
/**
* @override
* @description 連接設備
*/
connect() { // 這里只是實例,具體需要怎么處理還要更具體的業(yè)務邏輯
const ble = this.stateController.getBLE();
if (ble) {
this.stateController.setState(this.stateController.getConnectingState());
this.stateController.notifyAll();
ble.initBleAdapter();
}
}
getStateId() {
return 100;
}
}
module.exports = UnConnectState
#### ConnectingState類
import AbstractState from ‘./AbstractState’
class ConnectingState extends AbstractState { // 藍牙連接狀態(tài)的類
constructor(stateController) {
super(stateController);
}
connectSuccess() {
this.stateController.setState(this.stateController.getConnectedState());
this.stateController.notifyAll();
}
connectFail() {
this.stateController.setState(this.stateController.getUnConnectState());
this.stateController.notifyAll();
}
disconnect() {
this.stateController.setState(this.stateController.getUnConnectState());
this.stateController.notifyAll();
}
getStateId() {
return 101;
}
}
module.exports = ConnectingState
#### ConnectedState類
import AbstractState from ‘./AbstractState’
class ConnectedState extends AbstractState {
constructor(stateController) {
super(stateController);
}
connectFail() {
this.stateController.setState(this.stateController.getUnConnectState());
this.stateController.notifyAll();
// 結(jié)束發(fā)送路由的命令
}
authenticating() { this.stateController.setState(this.stateController.getAuthenticatingState());
// 發(fā)送認證的指令
}
disconnect() {
this.stateController.setState(this.stateController.getUnConnectState());
this.stateController.notifyAll();
}
getStateId() {
return 102;
}
onRelease() {
}
}
module.exports = ConnectedState
由于篇幅,其他的狀態(tài)類就不在這里寫了。
#### StateController類
import UnConnectState from ‘./UnConnectState’;
import ConnectingState from ‘./ConnectingState’;
import ConnectedState from ‘./ConnectedState’;
const DEBUG = true;
const TAG = ‘StateController#’;
let mUnConnectState = null;
let mConnectingState = null;
let mConnectedState = null;
let mState = null;
let mBle = null;
let STATE_CONTROLLER = null;
/** 頁面監(jiān)聽器列表 */
let mPageObservers = new Set();
class StateController {
constructor() {
mUnConnectState = new UnConnectState(this);
mConnectingState = new ConnectingState(this);
mConnectedState = new ConnectedState(this);
mState = mUnConnectState;
}
static getInstance() {
if (STATE_CONTROLLER == null) {
STATE_CONTROLLER = new StateController();
}
return STATE_CONTROLLER;
}
addPage(page) {
mPageObservers.add(page);
}
deletePage(page) {
mPageObservers.delete(page);
}
clearPages() {
mPageObservers.clear();
}
/**
* 連接設備,在調(diào)用之前確保已經(jīng)調(diào)用過#setDeviceId(deviceId)
*/
connect() {
mState.connect();
}
/**
* 斷開設備連接
*/
disconnect() {
mState.disconnect();
}
/**
* 設備連接成功
*/
connectSuccess() {
mState.connectSuccess();
}
/**
* 設備連接失敗
*/
connectFail() {
mState.connectFail();
}
/**
* 設置狀態(tài)
* @param {*} state 需要設置的狀態(tài)
* @see UnConnectState
* @see ConnectingState
* @see ConnectedState
*/
setState(state) {
mState = state;
}
/**
* 釋放狀態(tài)資源
*/
onRelease() {
mState.onRelease();
}
/**
* 頁面退出
*/
onUnload() {
this.disconnect();
this.onRelease();
}
getState() {
return mState;
}
getUnConnectState() {
return mUnConnectState;
}
getConnectingState() {
return mConnectingState;
}
getConnectedState() {
return mConnectedState;
}
getStateId() {
return mState.getStateId();
}
getBLE() {
if (mBle == null)
mBle = BLE.getInstance();
return mBle;
}
notifyAll() {
this.notifyState(mState.getStateId());
}
/**
* 通知頁面當前的狀態(tài)
* @param {*} status
*/
notifyState(status) {
mPageObservers.forEach((pageObserver) => {
pageObserver(status);
});
}
}
module.exports = StateController
#### 使用新的設計
import State from ‘./state’
import StateController from ‘./StateController’
class StateTest {
constructor() {}
onClick() {
// let state = new State();
自我介紹一下,小編13年上海交大畢業(yè),曾經(jīng)在小公司待過,也去過華為、OPPO等大廠,18年進入阿里一直到現(xiàn)在。
深知大多數(shù)嵌入式工程師,想要提升技能,往往是自己摸索成長或者是報班學習,但對于培訓機構(gòu)動則幾千的學費,著實壓力不小。自己不成體系的自學效果低效又漫長,而且極易碰到天花板技術(shù)停滯不前!
因此收集整理了一份《2024年嵌入式&物聯(lián)網(wǎng)開發(fā)全套學習資料》,初衷也很簡單,就是希望能夠幫助到想自學提升又不知道該從何學起的朋友,同時減輕大家的負擔。
既有適合小白學習的零基礎資料,也有適合3年以上經(jīng)驗的小伙伴深入學習提升的進階課程,基本涵蓋了95%以上嵌入式&物聯(lián)網(wǎng)開發(fā)知識點,真正體系化!
由于文件比較大,這里只是將部分目錄大綱截圖出來,每個節(jié)點里面都包含大廠面經(jīng)、學習筆記、源碼講義、實戰(zhàn)項目、講解視頻,并且后續(xù)會持續(xù)更新
如果你覺得這些內(nèi)容對你有幫助,可以+V:Vip1104z獲取?。?! (備注:嵌入式)

最后
資料整理不易,覺得有幫助的朋友可以幫忙點贊分享支持一下小編~
你的支持,我的動力;祝各位前程似錦,offer不斷,步步高升!??!文章來源:http://www.zghlxwxcb.cn/news/detail-851234.html
img-yq8vrNhI-1712389112572)]
由于文件比較大,這里只是將部分目錄大綱截圖出來,每個節(jié)點里面都包含大廠面經(jīng)、學習筆記、源碼講義、實戰(zhàn)項目、講解視頻,并且后續(xù)會持續(xù)更新
如果你覺得這些內(nèi)容對你有幫助,可以+V:Vip1104z獲?。。?! (備注:嵌入式)

最后
資料整理不易,覺得有幫助的朋友可以幫忙點贊分享支持一下小編~
你的支持,我的動力;祝各位前程似錦,offer不斷,步步高升!!!
更多資料點擊此處獲qu??!文章來源地址http://www.zghlxwxcb.cn/news/detail-851234.html
到了這里,關(guān)于小程序設計模式之狀態(tài)模式實踐-藍牙配網(wǎng)_小程序 藍牙狀態(tài)模式的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!