更新记录
1.0.6(2025-06-04)
修复安卓端需要配对时,未等待配对直接报配对失败的问题
1.0.5(2025-05-07)
移除无效代码
1.0.4(2025-05-06)
修复取消配对后自动再次发起配对的问题
查看更多平台兼容性
uni-app
Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 |
---|---|---|---|---|---|---|---|---|
√ | √ | - | - | - | - | 6.0 | × | √ |
微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 快应用-华为 | 快应用-联盟 |
---|---|---|---|---|---|---|---|---|---|---|
× | × | × | × | × | × | - | × | × | × | × |
uni-app x
Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 |
---|---|---|---|---|---|
- | - | 6.0 | × | √ | × |
bluetooth-spp
<template>
<view class="container">
<button @click="scan">开始扫描</button>
<button @click="stopScan">停止扫描</button>
<button @click="connect" :disabled="!selectedDeviceId">连接</button>
<button @click="disconnect" :disabled="!isConnected">断开连接</button>
<button @click="writeData" :disabled="!isConnected">发送数据</button>
<view>
<text>连接状态: {{ connectionStatus }}</text>
</view>
<view>
<text>收到的数据: {{ receivedData }}</text>
</view>
<view>
<text>找到的设备:</text>
<view style="padding: 10px; border-bottom: 1px solid #ccc;" v-for="device in foundDevices" :key="device.address" @click="selectDevice(device.address)">
<text>{{ device.name }} - {{ device.address }}</text>
</view>
</view>
</view>
</template>
<script>
import {
connect,
disconnect,
onDeviceFound,
onDeviceConnectionStateChange,
onDataReceived,
scan,
stopScan,
writeData,
initAdapter,
} from '../../uni_modules/bluetooth-spp';
export default {
data() {
return {
foundDevices: [],
selectedDeviceId: '',
isConnected: false,
connectionStatus: '未连接',
receivedData: '',
};
},
onLoad() {
this.initAdapter();
this.onDeviceFound();
this.onDeviceConnectionStateChange();
this.onDataReceived();
},
methods: {
initAdapter() {
initAdapter({
success: () => {
console.log('蓝牙适配器初始化成功');
},
fail: (err) => {
console.error('蓝牙适配器初始化失败', err);
},
fail: null,
});
},
startBluetoothDevicesDiscovery() {
scan({
success: () => {
console.log('开始扫描蓝牙设备');
},
fail: (err) => {
console.error('开始扫描失败', err);
}
})
},
scan() {
this.startBluetoothDevicesDiscovery();
},
stopScan() {
stopScan({
success: () => {
console.log('停止扫描蓝牙设备');
},
fail: (err) => {
console.error('停止扫描失败', err);
}
});
},
connect() {
if (this.selectedDeviceId) {
connect({
address: this.selectedDeviceId,
success: () => {
console.log('连接成功');
this.isConnected = true;
this.connectionStatus = '已连接';
},
fail: (err) => {
console.error('连接失败', err);
this.isConnected = false;
this.connectionStatus = '连接失败';
}
});
}
},
disconnect() {
if (this.selectedDeviceId) {
disconnect({
address: this.selectedDeviceId,
success: () => {
console.log('断开连接成功');
this.isConnected = false;
this.connectionStatus = '未连接';
},
fail: (err) => {
console.error('断开连接失败', err);
}
});
}
},
onDeviceFound() {
onDeviceFound((res) => {
const newDevices = res.devices.filter((device) => {
return !this.foundDevices.some((d) => d.address === device.address);
});
this.foundDevices = [...this.foundDevices, ...newDevices];
});
},
onDeviceConnectionStateChange() {
onDeviceConnectionStateChange((res) => {
console.log(res.address, res.state);
this.isConnected = res.state;
this.connectionStatus = res.state? '已连接' : '未连接';
});
},
onDataReceived() {
onDataReceived((res) => {
console.log(res);
this.receivedData = res;
});
},
selectDevice(deviceId) {
this.selectedDeviceId = deviceId;
},
writeData() {
// tsc '1B213F' esc '100402';
writeData({
address: this.selectedDeviceId,
type: 'hex',
data: '100402',
success: () => {
console.log('数据发送成功');
},
fail: (err) => {
console.error('数据发送失败', err);
}
});
}
}
};
</script>
<style lang="scss" scoped>
.container {
padding: 20px;
}
button {
margin: 10px;
}
</style>