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.
268 lines
8.5 KiB
268 lines
8.5 KiB
Page({
|
|
data: {
|
|
deviceList: [],
|
|
isSearching: false
|
|
},
|
|
|
|
// 开始搜索 请给出一个微信小程序可展开列表并且展开后是一个列表,并且宽度占满屏幕,展开后父列表改变背景颜色,缩起其它已展开的列表的示例代码
|
|
startSearch() {
|
|
|
|
wx.navigateTo({
|
|
url: '/pages/datalist/brand?name='+"" // 支持参数传递
|
|
// url: '/pages/newzk/newlist?name='+deviceName // 支持参数传递
|
|
})
|
|
|
|
|
|
// 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() {
|
|
const systemInfo = wx.getSystemInfoSync();
|
|
if (systemInfo.platform === 'android') {
|
|
console.log('当前为Android设备');
|
|
} else if (systemInfo.platform === 'ios') {
|
|
console.log('当前为iOS设备');
|
|
getApp().globalData.currentDevice = "ios"
|
|
}
|
|
|
|
|
|
|
|
|
|
wx.onBluetoothDeviceFound((res) => {
|
|
let deviceType = getApp().globalData.currentDevice
|
|
console.log("当前设备==》"+deviceType)
|
|
if(deviceType.includes("ios")){
|
|
res.devices.forEach(device => {
|
|
// console.log(device.RSSI+device.deviceid+device.advertisData);
|
|
|
|
if (device.advertisData) {
|
|
// console.log('设备名称:', device.name);
|
|
// console.log('广播数据原始Buffer:', device.advertisData);
|
|
if(device.name.includes("OBD")){
|
|
const hexData = this.ab2hex(device.advertisData);
|
|
console.log(`设备 ${device.name || device.deviceId} 的 advertisData: ${hexData}`);
|
|
// 提取 MAC 地址(假设在第 2-8 字节)
|
|
const mac = this.getMacFromAdvertisData(device.advertisData);
|
|
console.log(`设备 MAC: ${mac}`);
|
|
console.log(`设备 id: ${device.deviceId}`);
|
|
wx.setStorageSync(device.deviceId, mac)
|
|
}
|
|
// parseAdvertiseData(device.advertisData);
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
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 });
|
|
// }
|
|
|
|
// console.error(filteredDevices.advertisData)
|
|
// if (filteredDevices.advertisData) {
|
|
// const hexData = this.ab2hex(filteredDevices.advertisData);
|
|
// console.error(`设备 ${filteredDevices.name || filteredDevices.deviceId} 的 advertisData: ${hexData}`);
|
|
// // 提取 MAC 地址(假设在第 2-8 字节)
|
|
// const mac = this.getMacFromAdvertisData(filteredDevices.advertisData);
|
|
// console.error(`设备 MAC: ${mac}`);
|
|
// }
|
|
|
|
this.setData({
|
|
deviceList: [...this.data.deviceList, ...filteredDevices]
|
|
});
|
|
|
|
});
|
|
},
|
|
// ArrayBuffer 转 16 进制
|
|
ab2hex(buffer) {
|
|
return Array.prototype.map.call(
|
|
new Uint8Array(buffer),
|
|
bit => ('00' + bit.toString(16)).slice(-2)
|
|
).join('');
|
|
},
|
|
onShareAppMessage() {
|
|
return {
|
|
title: 'OBD小程序',
|
|
path: '/pages/index/index'
|
|
};
|
|
},
|
|
|
|
// 提取 MAC 地址
|
|
getMacFromAdvertisData(buffer) {
|
|
const macBuffer = buffer.slice(2, 8);
|
|
return Array.prototype.map.call(
|
|
new Uint8Array(macBuffer),
|
|
x => ('00' + x.toString(16)).slice(-2)
|
|
).join(':').toUpperCase();
|
|
},
|
|
|
|
// 停止搜索
|
|
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;
|
|
|
|
// wx.getConnectedBluetoothDevices({
|
|
|
|
// })
|
|
|
|
let panduan = getApp().globalData.deviceId;
|
|
let currentD = getApp().globalData.currentDevice;
|
|
|
|
console.log(panduan+'==='+deviceId+"==="+currentD);
|
|
if(panduan!=''&&deviceId==panduan&¤tD==''){
|
|
wx.navigateTo({
|
|
url: '/pages/datalist/brand?name='+deviceName // 支持参数传递
|
|
// url: '/pages/newzk/newlist?name='+deviceName // 支持参数传递
|
|
})
|
|
}else{
|
|
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; // 可写特征
|
|
// }
|
|
console.log(char.uuid);
|
|
|
|
if(char.uuid == '0000FFE1-0000-1000-8000-00805F9B34FB'){
|
|
let uuidNo = char.uuid;
|
|
let upUuid = uuidNo.toUpperCase();
|
|
getApp().globalData.notifiUuid = upUuid;
|
|
}
|
|
// if(char.properties.notify){
|
|
// let uuidNo = char.uuid;
|
|
// let upUuid = uuidNo.toUpperCase();
|
|
// getApp().globalData.notifiUuid = upUuid;
|
|
// }
|
|
if(char.uuid == '0000FFE2-0000-1000-8000-00805F9B34FB'){
|
|
let uuidNo = char.uuid;
|
|
let upUuid = uuidNo.toUpperCase();
|
|
getApp().globalData.writeUuid = 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)
|
|
});
|
|
}
|
|
})
|
|
|