# flutter_tracker 追踪者app,基于蓝牙提供疫情追踪功能。 ## 基于 google nearby 功能实现 google nearby 基于蓝牙,wifi,gps 等综合信息来发现周围用户: 1、用户注册登录 ``` 注册 void register() async { MessageModel messageModel = await LoginDao.register(_userName, _pwd); if (messageModel != null) { if (messageModel.success) { Navigator.of(context).pushNamed(Routes.loginPage); } AppUtil.buildToast(messageModel.msg); } else { AppUtil.buildToast("注册失败,请检查网络"); } } ``` ``` 登录 void login() async { SharedPreferences prefs = await SharedPreferences.getInstance(); if (_formKey.currentState.validate()) { _formKey.currentState.save(); LoginEntity loginEntity = await LoginDao.login(_username, _password); if (loginEntity.msgModel.success) { prefs.setString('username', _username); prefs.setString('password', _password); prefs.setBool('isLogin', true); Navigator.of(context).pushNamed(Routes.indexPage); } AppUtil.buildToast(loginEntity.msgModel.msg); } } ``` ``` 登出 void logout() async { var sharedPreferences = await SharedPreferences.getInstance(); String token = sharedPreferences.getString("token"); try { MessageModel messageModel = await LoginDao.logout(token); if (messageModel != null) { AppUtil.buildToast(messageModel.msg); } } catch (e) { AppUtil.buildToast("退出异常" + e.toString()); } Navigator.of(context).pushNamed(Routes.loginPage); sharedPreferences.remove("token"); sharedPreferences.setBool("isLogin", false); } ``` 2、进入跟踪页面,用户点击追踪按钮,云端获取最近15天遇到的人(姓名,健康状态)。 ``` try { bool a = await Nearby().startAdvertising( currentUser.username, strategy, onConnectionInitiated: null, onConnectionResult: (id, status) { print(status); }, onDisconnected: (id) { print('Disconnected $id'); }, ); // 上传追踪数据 SharedPreferences sharedPreferences = await SharedPreferences.getInstance(); String token = sharedPreferences.getString("token"); TrackDao.uploadContact(token); print('ADVERTISING ${a.toString()}'); } catch (e) { print(e); } ``` 3、开始周围追踪,上传当前用户遇到的人 ``` void startTrack(bool flag) async { try { bool a = await Nearby().startAdvertising( currentUser.username, strategy, onConnectionInitiated: null, onConnectionResult: (id, status) { print(status); }, onDisconnected: (id) { print('Disconnected $id'); }, ); // 上传追踪数据 TrackDao. print('ADVERTISING ${a.toString()}'); } catch (e) { print(e); } discovery(); } ``` ## 注意 1、flutter_blue 依赖min19,编译30,其他版本都会出错 2、 nearby_connections: ^1.0.3 3、需要 google service 服务,国内安卓系统有问题。