ScanBleDevice.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <template>
  2. <view class="content">
  3. <view class="loadView" v-if="showLoading">
  4. <image class="rotateAnim" src="../../static/loading.svg"></image>
  5. <text style="margin-left: 10rpx;">{{loadingText}}</text>
  6. </view>
  7. <view v-for="device in scanDeviceList" v-bind:key='device.mac' @click="gotoConnect(device)">
  8. <text>设备名称:{{device.name}} \n Mac:{{device.mac}}</text>
  9. </view>
  10. </view>
  11. </template>
  12. <script>
  13. export default {
  14. data() {
  15. return {
  16. isStartScan: false, //是否开始ble扫描
  17. stopScanTimer: undefined, //超时停止扫描的对象
  18. scanDeviceList: [], //扫描到的设备列表
  19. loadingText: '正在扫描设备', //load框的文字提示
  20. showLoading: false, //是否显示loading
  21. isOpenSetting:false,//是否跳转小程序设置界面
  22. }
  23. },
  24. onLoad() {
  25. //this.authorize()
  26. this.init()
  27. },
  28. onShow() {
  29. if(this.isOpenSetting){//如果为true就是从微信小程序设置返回到该页面,重新获取权限。
  30. this.isOpenSetting = false
  31. this.authorize()
  32. }
  33. },
  34. onBackPress() {
  35. console.log('onBackPress');
  36. this.stopScan()
  37. },
  38. methods: {
  39. init() {
  40. let that = this
  41. //初始化蓝牙模块
  42. uni.openBluetoothAdapter({
  43. success(res) {
  44. console.log(`初始化蓝牙模块成功 ${res}`)
  45. //开始扫描设备
  46. that.startBtScan();
  47. },
  48. fail(error) {
  49. if (error.errCode === 10001) { //在用户蓝牙开关未开启或者手机不支持蓝牙功能的情况下
  50. uni.showModal({
  51. title: '提示',
  52. content: '请打开蓝牙!',
  53. success: function(res) {
  54. if (res.confirm) {
  55. console.log('用户点击确定');
  56. uni.showToast({
  57. title: '请打开手机蓝牙以便扫描设备',
  58. icon: 'none',
  59. })
  60. that.showLoading = true;
  61. that.loadingText = '等待开启蓝牙。。。'
  62. that.lisenBtState()
  63. } else if (res.cancel) {
  64. console.log('用户点击取消');
  65. uni.navigateBack({});
  66. }
  67. }
  68. });
  69. } else { //其他错误
  70. uni.showToast({
  71. title: `查看手机蓝牙是否打开`,
  72. duration: 1000,
  73. icon: "none"
  74. })
  75. console.log(error)
  76. }
  77. },
  78. })
  79. },
  80. lisenBtState() { //监听蓝牙是否打开
  81. let that = this;
  82. uni.onBluetoothAdapterStateChange(function(res) {
  83. console.log('onBluetoothAdapterStateChange')
  84. console.log(res)
  85. if (res.available) { //蓝牙已打开
  86. //开始扫描设备
  87. that.startBtScan();
  88. }
  89. })
  90. },
  91. startBtScan() { //开始扫描蓝牙设备
  92. let that = this
  93. that.showLoading = true;
  94. that.loadingText = '正在扫描设备。。。'
  95. let count = that.scanDeviceList.length
  96. that.scanDeviceList.splice(0, count);
  97. //清除停止扫描的定时器
  98. clearTimeout(that.stopScanTimer)
  99. //开始扫描
  100. uni.startBluetoothDevicesDiscovery({
  101. success(res) {
  102. that.isStartScan = true
  103. console.log('开始扫描蓝牙设备');
  104. console.log(res);
  105. //30秒之后停止扫描
  106. that.stopScanTimer = setTimeout(() => {
  107. //停止扫描
  108. uni.stopBluetoothDevicesDiscovery({
  109. complete() {
  110. that.isStartScan = false
  111. that.showLoading = false;
  112. if(that.scanDeviceList.length == 0){
  113. uni.showModal({
  114. title: '提示',
  115. content: '沒有扫描到设备,是否重试!',
  116. success: function(res) {
  117. if (res.confirm) {
  118. console.log('重试!');
  119. that.startBtScan()
  120. } else if (res.cancel) {
  121. uni.navigateBack({});
  122. }
  123. }
  124. });
  125. }
  126. }
  127. })
  128. }, 30 * 1000);
  129. uni.onBluetoothDeviceFound(function(res) {
  130. //console.log('new device list has founded')
  131. console.log(res)
  132. console.log(res.devices)
  133. let devices = res.devices
  134. for (var i = 0; i < devices.length; i++) {
  135. console.log(devices[i])
  136. console.log(`名称:${devices[i].name}, mac : ${devices[i].deviceId}`)
  137. if (devices[i].name !== undefined && devices[i].name !== null &&
  138. devices[i].name !== '' && devices[i].name.startsWith('AIrSMArT')) {
  139. that.scanDeviceList.push({
  140. name: devices[i].name,
  141. mac: devices[i].deviceId
  142. })
  143. }
  144. }
  145. })
  146. },
  147. fail(err) {
  148. console.log(err)
  149. }
  150. })
  151. },
  152. gotoConnect(device) {
  153. this.stopScan()
  154. uni.navigateTo({
  155. url: `./ConnectBleDevice?name=${device.name}&mac=${device.mac}`
  156. })
  157. },
  158. stopScan() {
  159. //if(this.isStartScan){
  160. uni.stopBluetoothDevicesDiscovery({})
  161. clearTimeout(this.stopScanTimer)
  162. this.isStartScan = false
  163. //}
  164. },
  165. authorize(){
  166. let that = this
  167. // #ifdef MP-WEIXIN
  168. //uni.wx.getSetting()
  169. //uni.getUserPr
  170. //微信的定位授权
  171. uni.getSetting({
  172. success(res) {
  173. console.log("获取setting");
  174. console.log(res);
  175. if(!res.authSetting['scope.userLocation']){
  176. uni.authorize({
  177. scope: 'scope.userLocation',
  178. success() {
  179. console.log("获取到定位权限成功");
  180. //初始化蓝牙模块
  181. that.init()
  182. },
  183. fail(err){
  184. console.log("获取到定位权限失败");
  185. console.log(err)
  186. uni.showModal({
  187. title:'未授权',
  188. content:'需要打开小程序设置,授权定位权限,来扫描蓝牙设备',
  189. success() {
  190. uni.openSetting({
  191. success() {
  192. that.isOpenSetting = true
  193. console.log('openSetting 成功')
  194. }
  195. })
  196. },
  197. fail() {
  198. uni.navigateBack({})
  199. }
  200. })
  201. }
  202. })
  203. }else{
  204. //初始化蓝牙模块
  205. that.init()
  206. }
  207. }
  208. })
  209. // #endif
  210. }
  211. }
  212. }
  213. </script>
  214. <style>
  215. .content {
  216. display: flex;
  217. width: 100vw;
  218. height: 100vh;
  219. flex-direction: column;
  220. align-items: center;
  221. /* justify-content: center; */
  222. }
  223. .loadView {
  224. display: flex;
  225. flex-direction: row;
  226. align-items: center;
  227. /* margin-top: 35vh; */
  228. /* justify-content: center; */
  229. }
  230. .rotateAnim {
  231. width: 60rpx;
  232. height: 60rpx;
  233. animation: turn 1200ms linear infinite;
  234. }
  235. @keyframes turn {
  236. 0% {
  237. transform: rotate(0deg);
  238. }
  239. 50% {
  240. transform: rotate(180deg);
  241. }
  242. 100% {
  243. transform: rotate(360deg);
  244. }
  245. }
  246. </style>