You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
172 lines
5.3 KiB
172 lines
5.3 KiB
Page({
|
|
data: {
|
|
deviceList: [],
|
|
isSearching: false
|
|
},
|
|
|
|
// 开始搜索 请给出一个微信小程序可展开列表并且展开后是一个列表,并且宽度占满屏幕,展开后父列表改变背景颜色,缩起其它已展开的列表的示例代码
|
|
startSearch() {
|
|
|
|
|
|
if (this.data.isSearching) return;
|
|
|
|
wx.openBluetoothAdapter({
|
|
mode: 'central', // ios必须要带这个参数
|
|
success: () => {
|
|
this.setData({ isSearching: true });
|
|
this.startDiscovery();
|
|
},
|
|
fail: (err) => {
|
|
wx.showToast({ title: '请开启手机蓝牙', icon: 'none' });
|
|
console.error('蓝牙初始化失败:', err);
|
|
}
|
|
});
|
|
|
|
// wx.navigateTo({
|
|
// // url: '/pages/datalist/brand?name='+'FORD_OBD' // 支持参数传递
|
|
// url: '/pages/newzk/newlist?name='+"FORD_OBD" // 支持参数传递
|
|
// })
|
|
// wx.showToast({ title: '连接成功'+deviceName, icon: 'success' });
|
|
|
|
},
|
|
|
|
// 启动设备发现
|
|
startDiscovery() {
|
|
wx.startBluetoothDevicesDiscovery({
|
|
allowDuplicatesKey: false,
|
|
success: () => {
|
|
this.listenDevices();
|
|
setTimeout(() => this.stopDiscovery(), 7000); // 10秒后停止搜索
|
|
},
|
|
fail: (err) => {
|
|
console.error('搜索启动失败:', err);
|
|
this.setData({ isSearching: false });
|
|
}
|
|
});
|
|
},
|
|
|
|
// 监听发现设备
|
|
listenDevices() {
|
|
wx.onBluetoothDeviceFound((res) => {
|
|
const newDevices = res.devices.filter(device =>
|
|
!this.data.deviceList.some(d => d.deviceId === device.deviceId),
|
|
);
|
|
|
|
// const blename = newDevices.blename
|
|
// if(blename.includes('FORD')){
|
|
const filteredDevices = newDevices.filter(device =>
|
|
device.name.includes("OBD"),
|
|
);
|
|
// this.setData({ devices: filteredDevices });
|
|
// }
|
|
this.setData({
|
|
deviceList: [...this.data.deviceList, ...filteredDevices]
|
|
});
|
|
|
|
});
|
|
},
|
|
|
|
// 停止搜索
|
|
stopDiscovery() {
|
|
wx.stopBluetoothDevicesDiscovery({
|
|
success: () => {
|
|
this.setData({ isSearching: false });
|
|
// wx.showToast({ title: '搜索完成', icon: 'success' });
|
|
}
|
|
});
|
|
},
|
|
|
|
// 设备连接(示例框架)
|
|
connectDevice(e) {
|
|
|
|
const deviceId = e.currentTarget.dataset.deviceid;
|
|
const deviceName = e.currentTarget.dataset.blename;
|
|
const app = getApp();
|
|
|
|
|
|
wx.createBLEConnection({
|
|
deviceId,
|
|
success: () => {
|
|
getApp().globalData.deviceId = deviceId;
|
|
|
|
wx.navigateTo({
|
|
// url: '/pages/datalist/brand?name='+deviceName // 支持参数传递
|
|
url: '/pages/newzk/newlist?name='+deviceName // 支持参数传递
|
|
})
|
|
|
|
this.discoverServices(deviceId);
|
|
wx.showToast({ title: '连接成功'+deviceName+deviceId, icon: 'success' });
|
|
}
|
|
|
|
|
|
});
|
|
|
|
wx.onBLEConnectionStateChange((res) => {
|
|
if (!res.connected) {
|
|
console.log("设备已断开,尝试重连...");
|
|
this.reconnectDevice(); // 触发重连逻辑
|
|
}
|
|
});
|
|
},
|
|
discoverServices(deviceId) {
|
|
wx.getBLEDeviceServices({
|
|
deviceId: deviceId,
|
|
success: (res) => {
|
|
// 假设目标服务UUID为 FEE0(需替换为实际值)
|
|
const targetServiceId = res.services.find(s => s.uuid.toLowerCase() === '0000ffe0-0000-1000-8000-00805f9b34fb').uuid;
|
|
let upSUuid = targetServiceId.toUpperCase();
|
|
getApp().globalData.serviceUuid = upSUuid;
|
|
|
|
console.error('可写特征值1:', upSUuid);
|
|
console.error('可写特征值2:', getApp().globalData.serviceUuid);
|
|
this.getCharacteristics(deviceId, targetServiceId);
|
|
},
|
|
fail: (err) => console.error('发现服务失败', err)
|
|
});
|
|
},
|
|
getCharacteristics(deviceId, serviceId) {
|
|
wx.getBLEDeviceCharacteristics({
|
|
deviceId: deviceId,
|
|
serviceId: serviceId,
|
|
success: (res) => {
|
|
const characteristics = res.characteristics;
|
|
// let writeUuid = '', notifiUuid = '';
|
|
|
|
characteristics.forEach(char => {
|
|
if (char.properties.writeNoResponse){
|
|
let uuidNo = char.uuid;
|
|
let upUuid = uuidNo.toUpperCase();
|
|
getApp().globalData.writeUuid = upUuid; // 可写特征
|
|
}
|
|
// if(char.properties.notify){
|
|
// let uuidNo = char.uuid;
|
|
// let upUuid = uuidNo.toUpperCase();
|
|
// getApp().globalData.notifiUuid = upUuid;
|
|
// }
|
|
if(char.uuid == '0000ffe3-0000-1000-8000-00805f9b34fb'){
|
|
let uuidNo = char.uuid;
|
|
let upUuid = uuidNo.toUpperCase();
|
|
getApp().globalData.notifiUuid = upUuid;
|
|
}
|
|
|
|
// if (char.properties.) getApp().globalData.notifyUUID = char.uuid; // 通知特征
|
|
});
|
|
|
|
// console.log('可写特征值:', writeUuid);
|
|
// console.log('通知特征值:', notifiUuid);
|
|
|
|
// 启用通知(必须)
|
|
if (notifyUUID) {
|
|
wx.notifyBLECharacteristicValueChange({
|
|
deviceId: deviceId,
|
|
serviceId: serviceId,
|
|
characteristicId: notifyUUID,
|
|
state: true,
|
|
success: () => console.log('通知已启用')
|
|
});
|
|
}
|
|
},
|
|
fail: (err) => console.error('获取特征值失败', err)
|
|
});
|
|
}
|
|
})
|
|
|