Vue解决移动端localhost无数据问题

正常web端调用后台接口时使用localhost或者ip都能访问后台数据,但是在移动端上使用localhost却无法访问后台数据

这时候需要把localhost改成ip就可以在移动端上访问后台数据了

对于Vue 3移动端蓝牙通信,你可以使用Web Bluetooth API来实现。下面是一个简单的示例代码: 1. 首先,在你的Vue项目中安装 `web-bluetooth` 包: ```shell npm install web-bluetooth ``` 2. 创建一个蓝牙通信的Vue组件,比如 `BluetoothDevice.vue`: ```html <template> <div> <button @click="connectToDevice">连接设备</button> <button @click="disconnectFromDevice">断开连接</button> <button @click="sendData">发送数据</button> </div> </template> <script> import * as WebBluetooth from 'web-bluetooth'; export default { data() { return { device: null, characteristic: null, }; }, methods: { async connectToDevice() { try { // 请求蓝牙设备 const device = await WebBluetooth.requestDevice({ filters: [{ services: ['heart_rate'] }], }); // 连接蓝牙设备 await device.gatt.connect(); // 获取蓝牙设备的服务和特征值 const service = await device.gatt.getPrimaryService('heart_rate'); const characteristic = await service.getCharacteristic('heart_rate_measurement'); // 设置数据接收的回调函数 characteristic.addEventListener('characteristicvaluechanged', this.handleData); // 启动数据接收 await characteristic.startNotifications(); this.device = device; this.characteristic = characteristic; } catch(error) { console.error('连接蓝牙设备错误:', error); } }, async disconnectFromDevice() { if (this.device && this.device.gatt.connected) { await this.device.gatt.disconnect(); this.device = null; this.characteristic = null; } }, async sendData() { if (this.device && this.characteristic) { const encoder = new TextEncoder(); const data = encoder.encode('Hello, Bluetooth!'); await this.characteristic.writeValue(data); } }, handleData(event) { const value = event.target.value; // 处理接收到的数据 console.log('接收到的数据:', value); }, }, }; </script> ``` 3. 在你的Vue页面中使用该组件: ```html <template> <div> <bluetooth-device></bluetooth-device> </div> </template> <script> import BluetoothDevice from './BluetoothDevice.vue'; export default { components: { BluetoothDevice, }, }; </script> ``` 注意:上述代码仅为示例,实际使用时需要根据具体的蓝牙设备和特征值进行相应的配置和处理。另外,为了能在移动端使用Web Bluetooth API,需要在安全的上下文环境中(HTTPS、localhost)运行。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值