更新记录
1.0.2(2025-04-16)
打包编译报错, 手动添加 GrantStatus 类型
1.0.1(2025-04-16)
fix 鸿蒙连接后立即断开导致的闪退问题;
1.0.0(2025-04-15)
init
查看更多
平台兼容性
App |
快应用 |
微信小程序 |
支付宝小程序 |
百度小程序 |
字节小程序 |
QQ小程序 |
HBuilderX 3.6.8,Android:6.0,iOS:不支持,HarmonyNext:支持 |
× |
× |
× |
× |
× |
× |
钉钉小程序 |
快手小程序 |
飞书小程序 |
京东小程序 |
鸿蒙元服务 |
× |
× |
× |
× |
× |
H5-Safari |
Android Browser |
微信浏览器(Android) |
QQ浏览器(Android) |
Chrome |
IE |
Edge |
Firefox |
PC-Safari |
× |
× |
× |
× |
× |
× |
× |
× |
× |
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>