網(wǎng)站備案流程及資料seo搜索引擎
模擬的是藍牙設備簽到/簽出:
- 獲取指定藍牙設備
- 藍牙初始搜索次數(shù)限制,超過限制就停止搜索
- 藍牙連接失敗次數(shù)限制,超過限制標識藍牙連接失敗(離開藍牙范圍或其他原因)
- 自動重連指定藍牙
const device = ref<any>(null); // 設備信息
const crpBlueList = ref<any[]>([]); // 掃描到的藍牙信息列表
const linkStatus = ref(false); // 連接狀態(tài)
const searchTimes = ref(0); // 搜索次數(shù)
const searchLimit = 5; // 搜索次數(shù)限制
const linkTimes = ref(0); // 掃描次數(shù)
const failTimes = ref(0); // 失敗次數(shù)
const failLimit = 5; // 失敗次數(shù)限制
const blueName = 'zo-crp'; // 指定藍牙設備的名稱前綴
let isSignIn = false; // 是否簽到
// 簽到
const signIn = () => {searchTimes.value = 0;uni.showLoading({title: '藍牙搜索中...',mask: true});openBluetoothAdapter(() => {startBluetoothDeviceDiscovery();});
};
// 簽出
const logout = () => {closeBlueTooth(device.value, () => {isSignIn = false;device.value = null;linkStatus.value = false;searchTimes.value = 0;linkTimes.value = 0;failTimes.value = 0;crpBlueList.value = [];uni.showToast({title: '已簽出'});});
};
// 初始化藍牙
const openBluetoothAdapter = (callback: Function) => {uni.openBluetoothAdapter({//打開藍牙適配器接口success: (res) => {//已打開callback();},fail: (err) => {uni.hideLoading();uni.showModal({title: '',content: '該操作需要藍牙支持!請打開藍牙',showCancel: false,success: (res) => {if (res.confirm) {// #ifdef APPlet main = plus.android.runtimeMainActivity();let Intent = plus.android.importClass('android.content.Intent');let mIntent = new Intent('android.settings.BLUETOOTH_SETTINGS');main.startActivity(mIntent);// #endif} else {navigateBack();}},complete: () => {}});}});
};
// 搜索藍牙
const startBluetoothDeviceDiscovery = () => {if (searchTimes.value > searchLimit - 1) {uni.showModal({content:'沒有找到指定的藍牙設備,請確認所在位置周邊有指定藍牙設備,且手機已開啟位置信息并授權(quán)',confirmText: '重試',success: (res) => {if (res.confirm) {signIn();} else {stopBluetoothDevicesDiscovery(() => {uni.closeBluetoothAdapter({complete: () => {navigateBack();}});});}}});return;}searchTimes.value += 1;uni.startBluetoothDevicesDiscovery({success: (res) => {// 發(fā)現(xiàn)外圍設備onBluetoothDeviceFound();},fail: (err) => {console.log(err, '開始搜索藍牙設備備錯誤信息');}});
};
// 發(fā)現(xiàn)設備
const onBluetoothDeviceFound = () => {let t: any = setTimeout(() => {clearTimeout(t);t = null;stopBluetoothDevicesDiscovery(() => {// 停止搜索藍牙uni.getBluetoothDevices({success: (res) => {const blueList = res.devices.filter((item: any) => item.name.toLowerCase().startsWith(blueName)).sort((a: any, b: any) => b.RSSI - a.RSSI);crpBlueList.value = blueList;if (!blueList.length) {startBluetoothDeviceDiscovery();return;}const Device: any = blueList[0];isSignIn = true;// 獲取電量const serviceData = Array.prototype.map.call(new Uint8Array(Device.serviceData[Object.keys(Device.serviceData)[0]]), (bit) =>bit.toString(16)).join('');const Electric = parseInt(serviceData.slice(-2), 16);// 獲取uuidconst UUID = Array.prototype.map.call(new Uint8Array(Device.advertisData), (bit) => ('00' + bit.toString(16)).slice(-2)).join('').substring(8, 40).toUpperCase();device.value = {name: Device.name,deviceId: Device.deviceId,electric: Electric,RSSI: Device.RSSI,UUID: UUID};linkStatus.value = true;createBLEConnection(Device);}});});}, 2000);
};
// 連接設備
const createBLEConnection = (item: any) => {uni.showLoading({title: '連接中,請稍等',mask: true});linkTimes.value += 1;uni.createBLEConnection({deviceId: item.deviceId,success(res) {linkStatus.value = true;failTimes.value = 0;uni.showToast({title: '藍牙已連接',mask: true});onBLEConnectionStateChange(item);},fail(res) {linkStatus.value = false;plus.device.vibrate(500);if (failTimes.value < failLimit) {failTimes.value += 1;uni.showToast({title: item.name + '藍牙連接失敗',icon: 'none'});reLink(item);} else {closeBlueTooth(item, () => {uni.showToast({title: item.name + '藍牙連接失敗,取消連接',icon: 'none'});});}}});
};
// 監(jiān)聽藍牙狀態(tài)
const onBLEConnectionStateChange = (item: any) => {uni.onBLEConnectionStateChange((res) => {m_Debounce(() => {if (!res.connected && isSignIn) {reLink(item);}}, 500);});
};
// 藍牙重連
const reLink = (item: any) => {closeBlueTooth(item, () => {let t: any = setTimeout(() => {clearTimeout(t);t = null;openBluetoothAdapter(() => {createBLEConnection(item);});}, 1000);});
};// 關(guān)閉連接+關(guān)閉藍牙模塊
const closeBlueTooth = (item: any, callback: Function) => {// 關(guān)閉連接uni.closeBLEConnection({deviceId: item.deviceId,complete: () => {// 關(guān)閉藍牙模塊uni.closeBluetoothAdapter({complete: () => {callback();}});}});
};// 停止搜索
const stopBluetoothDevicesDiscovery = (callback: Function) => {uni.stopBluetoothDevicesDiscovery({complete: (e) => {callback();},fail: (e) => {console.log('停止搜索藍牙設備失敗,錯誤碼:' + e.errCode);}});
};
// 后退
const navigateBack = () => {uni.navigateBack({delta: 1,fail: () => {uni.reLaunch({url: '/pages/home/home'});}});
};