在获取到蓝牙权限后初始化蓝牙成功。但是监听蓝牙状态为不可用
initializeBluetooth() {
wx.showLoading({ title: '初始化蓝牙适配器...' });
wx.openBluetoothAdapter({
mode: 'central',
success: res => {
this.log('蓝牙适配器初始化成功');
this.log(`蓝牙状态: ${res.available ? '可用' : '不可用'}`);
wx.onBluetoothAdapterStateChange(res => {
this.log(`蓝牙适配器状态变化: ${res.available ? '可用' : '不可用'}`);
});
wx.onBluetoothDeviceFound(res => {
res.devices.forEach(device => {
const deviceId = device.deviceId;
const devices = this.data.devices;
if (!devices.find(d => d.deviceId === deviceId)) {
devices.push(device);
this.log(`发现设备: ${device.name || '未知'} (${deviceId})`);
this.setData({
devices: devices
});
}
});
});
this.setData({
startScanBtnDisabled: false
});
wx.hideLoading();
},
fail: err => {
this.log(`❌ 初始化蓝牙适配器失败: ${JSON.stringify(err)}`);
wx.hideLoading();
wx.showToast({
title: '初始化失败',
icon: 'none'
});
}
});
},