bluetooth.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <template>
  2. <view class="wrapper">
  3. <view class="footer">
  4. <!-- <button type="primary" size="mini" @click="reset">重新搜索</button> -->
  5. <!-- <button type="primary" size="mini" @click="getBluetoothDevices">获取蓝牙设备信息</button> -->
  6. <!-- <button type="primary" size="mini" @click="getBLEDeviceServices">获取服务</button> -->
  7. <!-- <button type="primary" size="mini" @click="getBLEDeviceCharacteristics">进入特征</button> -->
  8. <button type="primary" size="mini" @click="stopBluetoothDevicesDiscovery">停止搜索蓝牙</button>
  9. </view>
  10. <view class="list">
  11. </view>
  12. <view v-for="(item, index) in list" :key="index" class="list">
  13. <view class="item">
  14. <text>设备名称:{{ item.name }}</text>
  15. <!-- <button class="button" type="default" size="mini" @click="createBLEConnection(item)">{{item.connected ? '断开' :'连接'}}</button> -->
  16. </view>
  17. <view class="item">
  18. <text>设备deviceId:{{ item.deviceId }}</text>
  19. </view>
  20. <view class="item">
  21. <text>信号强度RSSI:{{ item.RSSI }}</text>
  22. </view>
  23. <!-- <view class="item">
  24. <text>距离:{{ item.RSSI | RSSI }}m</text>
  25. <button class="button" type="default" size="mini" @click="getRssi(item)">刷新</button>
  26. </view> -->
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. //定位需要根据算法 RSSI的值莱确定
  32. let timeoff = null
  33. import {bluetoothInt,bluetoothLoc} from '@/common/api.js'
  34. export default {
  35. data() {
  36. return {
  37. list:[],
  38. deviceId:'',
  39. serviceId:'',
  40. characteristics:[],
  41. characteristicId:'',
  42. blueObj:{},
  43. times:'',
  44. location:{
  45. }
  46. }
  47. },
  48. filters:{
  49. RSSI(val){
  50. if(val){ //A为距离设备1米时的rssi 绝对值。 n为环境衰减因子。
  51. let A = 40
  52. let n = 6
  53. return Math.pow(10,((Math.abs(val) - A) / (10 * n))).toFixed(2)
  54. }
  55. }
  56. },
  57. watch:{
  58. list(val){
  59. }
  60. },
  61. destroyed(){
  62. this.stopBluetoothDevicesDiscovery()
  63. clearInterval(timeoff)
  64. },
  65. onLoad(){
  66. uni.openBluetoothAdapter({
  67. success:(res)=> {
  68. uni.getBluetoothAdapterState({//蓝牙的匹配状态
  69. success:(res1)=>{
  70. console.log(res1,'本机设备的蓝牙已打开')
  71. bluetoothInt().then(res =>{
  72. console.log(res)
  73. this.blueObj = res.data
  74. this.startBluetoothDeviceDiscovery()
  75. this.getLocation()
  76. this.startInt()
  77. }).catch(e =>{
  78. })
  79. },
  80. fail(error) {
  81. uni.showToast({icon:'none',duration:3000,title: '查看手机蓝牙是否打开'});
  82. }
  83. });
  84. },
  85. fail:err=>{
  86. uni.showToast({icon:'none',title: '查看手机蓝牙是否打开'});
  87. }
  88. })
  89. },
  90. methods: {
  91. reset(){
  92. clearInterval(timeoff)
  93. uni.openBluetoothAdapter({
  94. success:(res)=> {
  95. uni.getBluetoothAdapterState({//蓝牙的匹配状态
  96. success:(res1)=>{
  97. this.startInt()
  98. },
  99. fail(error) {
  100. uni.showToast({icon:'none',duration:3000,title: '查看手机蓝牙是否打开'});
  101. }
  102. });
  103. },
  104. fail:err=>{
  105. uni.showToast({icon:'none',title: '查看手机蓝牙是否打开'});
  106. }
  107. })
  108. },
  109. stopBluetoothDevicesDiscovery(){
  110. clearInterval(timeoff)
  111. uni.stopBluetoothDevicesDiscovery({
  112. success: e => {
  113. uni.closeBluetoothAdapter()
  114. console.log('停止搜索蓝牙设备:' + e.errMsg);
  115. },
  116. fail: e => {
  117. console.log('停止搜索蓝牙设备失败,错误码:' + e.errCode);
  118. }
  119. });
  120. },
  121. startInt(){
  122. if(this.blueObj.flag == 1) return
  123. timeoff = setInterval( async () =>{
  124. this.list = Array.from(new Set(this.list))
  125. let str = ''
  126. this.list.map((item) =>{
  127. str+=item.name+','+item.RSSI+";"
  128. })
  129. str+=uni.getStorageSync('storage_users').uid+";"+this.location.latitude+','+this.location.longitude
  130. console.log(str)
  131. try{
  132. let data = await bluetoothLoc({rssis:str})
  133. uni.stopBluetoothDevicesDiscovery()
  134. setTimeout(() =>{
  135. this.getLocation()
  136. this.startBluetoothDeviceDiscovery()
  137. },1000)
  138. }
  139. catch(e){
  140. console.log(e)
  141. clearInterval(timeoff)
  142. }
  143. },this.blueObj.seconds*1000)
  144. },
  145. getLocation(){
  146. uni.getLocation({
  147. type: 'gcj02 ',
  148. geocode: true,
  149. success:async (res) => {
  150. this.location = res
  151. },
  152. fail: (e) => {
  153. console.log(e)
  154. plus.nativeUI.toast("获取位置失败")
  155. }
  156. });
  157. },
  158. getRssi(item){
  159. if(item.connected){
  160. uni.getBLEDeviceRSSI({
  161. deviceId:item.deviceId,
  162. success(res) {
  163. console.log(res)
  164. item.RSSI = res.RSSI
  165. },
  166. })
  167. }
  168. else{
  169. uni.createBLEConnection({
  170. deviceId:item.deviceId,
  171. success(res) {
  172. console.log(res)
  173. uni.getBLEDeviceRSSI({
  174. deviceId:item.deviceId,
  175. success(res) {
  176. console.log(res)
  177. item.RSSI = res.RSSI
  178. },
  179. })
  180. console.log("蓝牙连接成功")
  181. },fail(res) {
  182. uni.showToast({icon:'none',title: '蓝牙连接失败'});
  183. console.log("蓝牙连接失败",res)
  184. }
  185. })
  186. }
  187. },
  188. startBluetoothDeviceDiscovery(){
  189. this.list = []
  190. uni.startBluetoothDevicesDiscovery({
  191. success: (res) => {
  192. console.log('startBluetoothDevicesDiscovery success', res)
  193. // 发现外围设备
  194. this.onBluetoothDeviceFound()
  195. },fail:err=>{
  196. console.log(err,'错误信息')
  197. }
  198. })
  199. },
  200. // 发现外围设备
  201. onBluetoothDeviceFound() {
  202. uni.onBluetoothDeviceFound((res) => {
  203. if(res.devices[0].localName.indexOf(this.blueObj.preName) != '-1'){
  204. if(this.list.indexOf(res.devices[0].deviceId)==-1){
  205. this.list.push({
  206. name:res.devices[0].name,
  207. deviceId:res.devices[0].deviceId,
  208. RSSI:res.devices[0].RSSI,
  209. connected:false
  210. })
  211. }
  212. this.list = Array.from(new Set(this.list))
  213. }
  214. })
  215. },
  216. //获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备。
  217. getBluetoothDevices() {
  218. console.log("获取蓝牙设备");
  219. uni.getBluetoothDevices({
  220. success: res => {
  221. console.log('获取蓝牙设备成功:');
  222. console.log(res.devices);
  223. }
  224. });
  225. },
  226. //选择设备连接吧deviceId传进来
  227. createBLEConnection(item){
  228. //连接蓝牙
  229. this.deviceId = item.deviceId
  230. if(item.connected){
  231. uni.closeBLEConnection({
  232. deviceId:item.deviceId,
  233. })
  234. }
  235. else{
  236. uni.createBLEConnection({
  237. deviceId:this.deviceId,
  238. success(res) {
  239. console.log(res)
  240. console.log("蓝牙连接成功")
  241. },fail(res) {
  242. console.log("蓝牙连接失败",res)
  243. }
  244. })
  245. }
  246. },
  247. // 停止搜寻蓝牙设备
  248. //获取蓝牙特征
  249. getBLEDeviceCharacteristics(){
  250. console.log("进入特征");
  251. setTimeout(()=>{
  252. uni.getBLEDeviceCharacteristics({
  253. // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  254. deviceId:this.deviceId,
  255. // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  256. serviceId:this.serviceId,
  257. success:(res)=>{
  258. console.log(res,'特征getBLEDeviceCharacteristics')
  259. this.characteristics = res.characteristics
  260. console.log(this.characteristics)
  261. res.characteristics.forEach((item)=>{
  262. this.characteristicId = item.uuid
  263. //console.log('characteristicId:', item.uuid)
  264. //利用传参的形势传给下面的notify,这里的uuid如果都需要用到,就不用做判断了,建议使用setTimeout进行间隔性的调用此方法
  265. })
  266. },
  267. fail:(res)=>{
  268. console.log(res)
  269. }
  270. })
  271. },1000)
  272. },
  273. //获取蓝牙的所有服务
  274. getBLEDeviceServices(){
  275. setTimeout(()=>{
  276. uni.getBLEDeviceServices({
  277. // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  278. deviceId:this.deviceId,
  279. success:(res)=>{
  280. console.log('device services:', res)
  281. //res.services.forEach((item)=>{
  282. //if(item.uuid.indexOf("00001803")!=-1){
  283. this.serviceId = res.services[3].uuid;
  284. // console.log(this.serviceId)
  285. //获取特征
  286. this.getBLEDeviceCharacteristics()
  287. // }
  288. // })
  289. }
  290. })
  291. },1000)
  292. },
  293. }
  294. }
  295. </script>
  296. <style scoped lang="scss">
  297. .wrapper{
  298. padding: 50rpx;
  299. .footer{
  300. display: flex;
  301. }
  302. .list{
  303. margin:30rpx 0;
  304. .item{
  305. display: flex;
  306. border: 1px solid #F5F5F5;
  307. box-sizing: border-box;
  308. padding:10px 10px;
  309. justify-content: space-between;
  310. align-items: center;
  311. .button{
  312. margin:0;
  313. }
  314. }
  315. }
  316. }
  317. </style>