Files
huishou/dist/build/h5/assets/bluetooth.ChhihnNc.js
T
2026-07-27 13:37:48 +08:00

2 lines
8.1 KiB
JavaScript

var e=(e,t,o)=>new Promise((i,r)=>{var n=e=>{try{s(o.next(e))}catch(t){r(t)}},c=e=>{try{s(o.throw(e))}catch(t){r(t)}},s=e=>e.done?i(e.value):Promise.resolve(e.value).then(n,c);s((o=o.apply(e,t)).next())});import{h as t,x as o}from"./index-CfY8Z-kW.js";let i="",r="",n="",c="",s=null;const a=e=>new Promise((t,o)=>{uni.getBLEDeviceServices({deviceId:e,success:i=>{console.log("=== Available Services ==="),i.services.forEach((e,t)=>{console.log(`${t+1}. UUID: ${e.uuid}, Primary: ${e.isPrimary}`)});const n=["0000FFE0","0000ffe0","0000180a","00001800","0000ffe5","0000fff0","fff0","0000ffe3","ffe3","0000ffe1","ffe1"];let c=i.services.find(e=>n.some(t=>e.uuid.toLowerCase().startsWith(t.toLowerCase())));!c&&i.services.length>0&&(c=i.services.find(e=>e.isPrimary)||i.services.find(e=>4===e.uuid.length||8===e.uuid.length)||i.services[0],console.log("Using fallback service:",c.uuid)),c?(r=c.uuid,console.log("Selected service:",r),l(e,c.uuid).then(()=>t()).catch(o)):o(new Error("Service not found"))},fail:o})}),l=(e,t)=>new Promise((o,i)=>{uni.getBLEDeviceCharacteristics({deviceId:e,serviceId:t,success:r=>{console.log("=== Available Characteristics ==="),r.characteristics.forEach((e,t)=>{console.log(`${t+1}. UUID: ${e.uuid}`),console.log(` - Properties: notify=${e.properties.notify}, read=${e.properties.read}, write=${e.properties.write}, writeWithoutResponse=${e.properties.writeWithoutResponse}`)});const s=["0000FFE1","0000ffe1","0000ffe2","0000ffe4","0000fff1","fff1","0000ffe3","ffe3","00002a00","00002a01","00002a04","00002a05"];let a=r.characteristics.find(e=>s.some(t=>e.uuid.toLowerCase().startsWith(t.toLowerCase()))&&(e.properties.notify||e.properties.read));const l=r.characteristics.find(e=>e.properties.write||e.properties.writeWithoutResponse);l&&(c=l.uuid,console.log("Found write characteristic:",c)),a||(a=r.characteristics.find(e=>e.properties.notify)),a||(a=r.characteristics.find(e=>e.properties.read)),!a&&r.characteristics.length>0&&(a=r.characteristics.find(e=>4===e.uuid.length||8===e.uuid.length)||r.characteristics[0],console.log("Using fallback characteristic:",a.uuid)),a?(n=a.uuid,console.log("Selected characteristic for read/notify:",n),console.log("Properties:",a.properties),u(e,t),a.properties.notify?v(e,t,a.uuid).then(()=>o()).catch(i):a.properties.read?p(e,t,a.uuid).then(()=>{g(e,t,a.uuid),o()}).catch(i):o()):i(new Error("Characteristic not found"))},fail:i})}),u=(e,t)=>{if(!c)return void console.log("No write characteristic found, skipping init commands");[f("START"),f("ON"),f("WEIGH"),f("GET"),f("1"),new Uint8Array([1]).buffer,new Uint8Array([2]).buffer,new Uint8Array([3]).buffer,new Uint8Array([85]).buffer,new Uint8Array([170,85]).buffer,new Uint8Array([254,1]).buffer,new Uint8Array([255,1]).buffer,new Uint8Array([0,0,0,1]).buffer].forEach((o,i)=>{setTimeout(()=>{d(e,t,c,o)},200*i)})},d=(e,t,o,i)=>{uni.writeBLECharacteristicValue({deviceId:e,serviceId:t,characteristicId:o,value:i,success:()=>{console.log("Wrote command:",I(i))},fail:e=>{console.log("Write failed (may be normal for read-only chars):",e.errMsg)}})},f=e=>(new TextEncoder).encode(e).buffer;let h=null;const g=(e,t,o)=>{h&&clearInterval(h),h=setInterval(()=>{uni.readBLECharacteristicValue({deviceId:e,serviceId:t,characteristicId:o,success:e=>{y(e.value)},fail:e=>{console.log("Periodic read failed:",e.errMsg)}})},300),console.log("Started periodic read every 300ms")},v=(e,t,o)=>new Promise((i,r)=>{uni.notifyBLECharacteristicValueChange({deviceId:e,serviceId:t,characteristicId:o,state:!0,success:()=>{console.log("Notify enabled for:",o),i()},fail:n=>{console.log("Notify failed, trying read mode:",n.errMsg),p(e,t,o).then(()=>{g(e,t,o),i()}).catch(r)}})}),p=(e,t,o)=>new Promise((i,r)=>{uni.readBLECharacteristicValue({deviceId:e,serviceId:t,characteristicId:o,success:e=>{console.log("Read characteristic value received"),y(e.value),i()},fail:e=>{console.error("Read failed:",e),r(e)}})}),y=e=>{console.log("=== BLE Data Received ==="),console.log("Data length:",e.byteLength,"bytes"),console.log("Hex:",I(e));const t=w(e);console.log("ASCII:",JSON.stringify(t));const o=m(e);console.log("Parsed weight:",o,"kg"),o>0&&o<1e3&&s&&(s(o),console.log("Weight sent to callback:",o))},m=e=>{if(0===e.byteLength)return console.log("Empty buffer received"),0;const t=new DataView(e),o=w(e).replace(/[\r\n\s\x00-\x1F]+/g,"");console.log("Cleaned ASCII:",JSON.stringify(o));const i=[/[+-]?\d+\.\d{1,3}\s*[kK][gG]?/,/[+-]?\d+\.\d{1,3}/,/[+-]?\d+\,\d{1,3}/,/[+-]?\d+\.?\d*/];for(const r of i){const e=o.match(r);if(e){const t=e[0].replace(",",".").replace(/[^0-9.-]/g,""),o=parseFloat(t);if(!isNaN(o)&&o>0&&o<1e3)return console.log("Parsed from text:",o),o}}if(e.byteLength>=2){const o=[];if(e.byteLength>=4&&(o.push(t.getInt32(0,!0)/1e3),o.push(t.getInt32(0,!1)/1e3),o.push(t.getFloat32(0,!0)),o.push(t.getFloat32(0,!1)),o.push(t.getUint32(0,!0)/1e3),o.push(t.getUint32(0,!1)/1e3)),e.byteLength>=2&&(o.push(t.getInt16(0,!0)/10),o.push(t.getInt16(0,!1)/10),o.push(t.getUint16(0,!0)/10),o.push(t.getUint16(0,!1)/10),o.push(t.getInt16(0,!0)/100),o.push(t.getInt16(0,!1)/100),o.push(t.getInt16(0,!0)/1e3),o.push(t.getInt16(0,!1)/1e3)),e.byteLength>=3){const e=t.getUint8(0),i=t.getUint8(1),r=t.getUint8(2);o.push((e<<16|i<<8|r)/1e3),o.push((r<<16|i<<8|e)/1e3),o.push((i<<16|e<<8|r)/1e3)}console.log("Testing binary values:",o.filter(e=>e>0&&e<1e3));for(const e of o)if(e>0&&e<1e3&&!isNaN(e))return console.log("Parsed from binary:",e),e}if(e.byteLength>=4){const t=new Uint8Array(e);let o="";for(let e=0;e<t.length;e++){const i=t[e]>>4&15,r=15&t[e];i>=0&&i<=9&&(o+=i.toString()),r>=0&&r<=9&&(o+=r.toString())}if(console.log("BCD value:",o),o.length>=1){let e=parseFloat(o)/1e3;if(e>0&&e<1e3)return console.log("Parsed from BCD:",e),e;const t=o.length-3;if(t>0){const i=o.slice(0,t)+"."+o.slice(t);if(e=parseFloat(i),e>0&&e<1e3)return console.log("Parsed from BCD with decimal:",e),e}}}return console.log("No valid weight found, returning 0"),0},w=e=>{try{const t=new Uint8Array(e);let o="";for(let e=0;e<t.length;e++)t[e]>=32&&t[e]<=126?o+=String.fromCharCode(t[e]):10!==t[e]&&13!==t[e]||(o+=10===t[e]?"\n":"\r");return o}catch(t){return""}},I=e=>{const t=new Uint8Array(e);return Array.from(t).map(e=>e.toString(16).padStart(2,"0")).join(" ")},b=t("bluetooth",()=>{const t=o(!1),l=o(!1),u=o([]),d=o(null),f=o(0);return{isConnected:t,isDiscovering:l,deviceList:u,connectedDevice:d,currentWeight:f,init:()=>e(void 0,null,function*(){try{yield new Promise((e,t)=>{uni.openBluetoothAdapter({success:()=>{console.log("Bluetooth adapter initialized"),e()},fail:e=>{console.error("Bluetooth init failed:",e),t(e)}})})}catch(e){console.error("Bluetooth init failed:",e)}}),startScan:()=>e(void 0,null,function*(){var e;l.value=!0,u.value=[],yield(e=e=>{u.value=e},new Promise(t=>{const o=[];uni.startBluetoothDevicesDiscovery({services:[],success:()=>{console.log("Started discovery"),uni.onBluetoothDeviceFound(t=>{t.devices.filter(e=>e.name&&(e.name.includes("Scale")||e.name.includes("秤")||e.name.includes("WXL"))).forEach(e=>{o.find(t=>t.deviceId===e.deviceId)||o.push({deviceId:e.deviceId,name:e.name,RSSI:e.RSSI||0})}),e([...o])}),setTimeout(()=>{uni.stopBluetoothDevicesDiscovery(),t()},1e4)}})}))}),stopScan:()=>{l.value=!1,uni.stopBluetoothDevicesDiscovery()},connect:o=>e(void 0,null,function*(){try{yield(e=o,new Promise((t,o)=>{i=e,uni.createBLEConnection({deviceId:e,success:()=>{console.log("Connected to device:",e),a(e).then(()=>t()).catch(o)},fail:e=>{console.error("Connection failed:",e),o(e)}})}));const r=u.value.find(e=>e.deviceId===o);r&&(d.value=r,t.value=!0),s=e=>{f.value=e},console.log("Weight callback registered"),uni.onBLECharacteristicValueChange(e=>{console.log("BLE characteristic value changed event"),y(e.value)})}catch(r){console.error("Connect failed:",r)}var e}),disconnect:()=>e(void 0,null,function*(){try{yield new Promise((e,t)=>{h&&(clearInterval(h),h=null,console.log("Stopped periodic read")),i?uni.closeBLEConnection({deviceId:i,success:()=>{console.log("Disconnected successfully"),i="",r="",n="",c="",s=null,e()},fail:t=>{console.log("Disconnect failed but clearing state:",t.errMsg),i="",r="",n="",c="",s=null,e()}}):e()}),t.value=!1,d.value=null,f.value=0}catch(e){console.error("Disconnect failed:",e)}}),resetWeight:()=>{f.value=0}}});export{b as u};