bluetooth1.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <template>
  2. <view class="wrapper">
  3. <view class="footer">
  4. <button type="primary" size="mini" @click="startBluetoothDeviceDiscovery">重新搜索</button>
  5. <button type="primary" size="mini" @click="getAll">获取所有</button>
  6. <button type="primary" size="mini" @click="stopBluetoothDevicesDiscovery">停止搜索蓝牙</button>
  7. </view>
  8. <view v-for="(item, index) in list" :key="index" class="list">
  9. <view class="item">
  10. <text>设备uuid:{{ item.uuid }}</text>
  11. </view>
  12. <view class="item">
  13. <text>信号强度RSSI:{{ item.rssi }}</text>
  14. </view>
  15. <view class="item">
  16. <text>major:{{ item.major }}</text>
  17. </view>
  18. <view class="item">
  19. <text>minor:{{ item.minor }}</text>
  20. </view>
  21. <view class="item">
  22. <text>距离:{{ item.rssi | RSSI }}m</text>
  23. </view>
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. //定位需要根据算法 RSSI的值莱确定
  29. export default {
  30. data() {
  31. return {
  32. list:[],
  33. deviceId:'',
  34. serviceId:'',
  35. characteristics:[],
  36. characteristicId:'',
  37. }
  38. },
  39. filters:{
  40. RSSI(val){
  41. if(val){ //A为距离设备1米时的rssi 绝对值。 n为环境衰减因子。
  42. let A = 40
  43. let n = 6
  44. return Math.pow(10,((Math.abs(val) - A) / (10 * n))).toFixed(2)
  45. }
  46. }
  47. },
  48. onHide() {
  49. //this.stopBluetoothDevicesDiscovery()
  50. uni.stopBeaconDiscovery()
  51. },
  52. onShow(){
  53. uni.openBluetoothAdapter({
  54. success:(res)=> {
  55. uni.getBluetoothAdapterState({//蓝牙的匹配状态
  56. success:(res1)=>{
  57. console.log(res1,'本机设备的蓝牙已打开')
  58. this.startBluetoothDeviceDiscovery()
  59. },
  60. fail(error) {
  61. uni.showToast({icon:'none',duration:3000,title: '查看手机蓝牙是否打开'});
  62. }
  63. });
  64. uni.onBeaconServiceChange(function(res,e){
  65. console.log(res,e)
  66. })
  67. uni.onBeaconUpdate(function(res,e){
  68. if(res.beacons.length >0){
  69. // console.log(res)
  70. }
  71. })
  72. },
  73. fail:err=>{
  74. uni.showToast({icon:'none',title: '查看手机蓝牙是否打开'});
  75. }
  76. })
  77. },
  78. methods: {
  79. getAll(){
  80. uni.getBeacons({
  81. success:function(res){
  82. console.log(res)
  83. }
  84. })
  85. },
  86. startBluetoothDeviceDiscovery(){
  87. this.list = []
  88. let that = this
  89. uni.startBeaconDiscovery({
  90. uuids:'fda50693-a4e2-4fb1-afcf-c6eb07647825',
  91. success: (res) => {
  92. uni.onBeaconUpdate(function(value){
  93. try{
  94. that.list = value.beacons
  95. }catch(e){
  96. console.log(e)
  97. }
  98. })
  99. },fail:err=>{
  100. console.log(err,'错误信息')
  101. }
  102. })
  103. },
  104. // 停止搜寻蓝牙设备
  105. stopBluetoothDevicesDiscovery(){
  106. uni.stopBeaconDiscovery({
  107. success: e => {
  108. this.loading = false
  109. console.log('停止搜索蓝牙设备:' + e.errMsg);
  110. },
  111. fail: e => {
  112. console.log('停止搜索蓝牙设备失败,错误码:' + e.errCode);
  113. }
  114. });
  115. },
  116. // 启用 notify 功能
  117. notifyBLECharacteristicValueChange(characteristicId){
  118. console.log(characteristicId,'characteristicId')
  119. uni.notifyBLECharacteristicValueChange({
  120. state: true, // 启用 notify 功能
  121. // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  122. deviceId:this.deviceId,
  123. // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  124. serviceId:this.serviceId,
  125. // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
  126. characteristicId:characteristicId,
  127. success:(res)=> {
  128. console.log(res)
  129. // console.log(this.characteristicId)
  130. console.log('notifyBLECharacteristicValueChange success', res.errMsg)
  131. },
  132. fail:(res)=> {
  133. console.log('notifyBLECharacteristicValueChange fail', res.errMsg)
  134. }
  135. })
  136. },
  137. }
  138. }
  139. </script>
  140. <style scoped lang="scss">
  141. .wrapper{
  142. padding: 50rpx;
  143. .footer{
  144. display: flex;
  145. }
  146. .list{
  147. margin:30rpx 0;
  148. .item{
  149. display: flex;
  150. border: 1px solid #F5F5F5;
  151. box-sizing: border-box;
  152. padding:10px 10px;
  153. justify-content: space-between;
  154. align-items: center;
  155. .button{
  156. margin:0;
  157. }
  158. }
  159. }
  160. }
  161. </style>