123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <template>
- <view class="wrapper">
- <view class="footer">
- <button type="primary" size="mini" @click="startBluetoothDeviceDiscovery">重新搜索</button>
- <button type="primary" size="mini" @click="getAll">获取所有</button>
- <button type="primary" size="mini" @click="stopBluetoothDevicesDiscovery">停止搜索蓝牙</button>
- </view>
- <view v-for="(item, index) in list" :key="index" class="list">
- <view class="item">
- <text>设备uuid:{{ item.uuid }}</text>
- </view>
- <view class="item">
- <text>信号强度RSSI:{{ item.rssi }}</text>
- </view>
- <view class="item">
- <text>major:{{ item.major }}</text>
- </view>
- <view class="item">
- <text>minor:{{ item.minor }}</text>
- </view>
- <view class="item">
- <text>距离:{{ item.rssi | RSSI }}m</text>
- </view>
- </view>
- </view>
- </template>
- <script>
- //定位需要根据算法 RSSI的值莱确定
- export default {
- data() {
- return {
- list:[],
- deviceId:'',
- serviceId:'',
- characteristics:[],
- characteristicId:'',
- }
- },
- filters:{
- RSSI(val){
- if(val){ //A为距离设备1米时的rssi 绝对值。 n为环境衰减因子。
- let A = 40
- let n = 6
- return Math.pow(10,((Math.abs(val) - A) / (10 * n))).toFixed(2)
- }
- }
- },
- onHide() {
- //this.stopBluetoothDevicesDiscovery()
- uni.stopBeaconDiscovery()
- },
- onShow(){
- uni.openBluetoothAdapter({
- success:(res)=> {
- uni.getBluetoothAdapterState({//蓝牙的匹配状态
- success:(res1)=>{
- console.log(res1,'本机设备的蓝牙已打开')
- this.startBluetoothDeviceDiscovery()
- },
- fail(error) {
- uni.showToast({icon:'none',duration:3000,title: '查看手机蓝牙是否打开'});
- }
- });
-
- uni.onBeaconServiceChange(function(res,e){
- console.log(res,e)
- })
-
- uni.onBeaconUpdate(function(res,e){
- if(res.beacons.length >0){
- // console.log(res)
- }
- })
- },
- fail:err=>{
- uni.showToast({icon:'none',title: '查看手机蓝牙是否打开'});
- }
- })
- },
- methods: {
- getAll(){
- uni.getBeacons({
- success:function(res){
- console.log(res)
- }
- })
- },
- startBluetoothDeviceDiscovery(){
- this.list = []
- let that = this
- uni.startBeaconDiscovery({
- uuids:'fda50693-a4e2-4fb1-afcf-c6eb07647825',
- success: (res) => {
- uni.onBeaconUpdate(function(value){
- try{
- that.list = value.beacons
- }catch(e){
- console.log(e)
- }
- })
- },fail:err=>{
- console.log(err,'错误信息')
- }
- })
- },
-
- // 停止搜寻蓝牙设备
- stopBluetoothDevicesDiscovery(){
- uni.stopBeaconDiscovery({
- success: e => {
- this.loading = false
- console.log('停止搜索蓝牙设备:' + e.errMsg);
- },
- fail: e => {
- console.log('停止搜索蓝牙设备失败,错误码:' + e.errCode);
- }
- });
- },
-
- // 启用 notify 功能
- notifyBLECharacteristicValueChange(characteristicId){
- console.log(characteristicId,'characteristicId')
- uni.notifyBLECharacteristicValueChange({
- state: true, // 启用 notify 功能
- // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
- deviceId:this.deviceId,
- // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
- serviceId:this.serviceId,
- // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
- characteristicId:characteristicId,
- success:(res)=> {
- console.log(res)
- // console.log(this.characteristicId)
- console.log('notifyBLECharacteristicValueChange success', res.errMsg)
- },
- fail:(res)=> {
- console.log('notifyBLECharacteristicValueChange fail', res.errMsg)
- }
- })
- },
- }
- }
- </script>
- <style scoped lang="scss">
- .wrapper{
- padding: 50rpx;
- .footer{
- display: flex;
- }
- .list{
- margin:30rpx 0;
- .item{
- display: flex;
- border: 1px solid #F5F5F5;
- box-sizing: border-box;
- padding:10px 10px;
- justify-content: space-between;
- align-items: center;
- .button{
- margin:0;
- }
- }
- }
- }
- </style>
|