bleWriteModeRules.ts 1.61 KB
/**
 * 部分标签机 BLE 串口:GATT 同时声明 write / writeNoResponse,但数据口实际只接受 Write Command(无响应)。
 * 用默认「带响应写」时常见首包过、第二包起 writeBLECharacteristicValue:fail property not support (10007)。
 * printerManager 在「同时声明两种属性」时对本白名单 UUID 优先选 writeNoResponse;若系统只暴露 write,则绝不强行 Command 写(否则 10007)。
 * sendViaBle 在已选无响应写仍 10007 时,对白名单禁止翻到带响应写(避免佳博长任务第二包必挂)。
 */

export function normalizeBleUuid (uuid: string): string {
  return String(uuid || '').replace(/-/g, '').toLowerCase()
}

/** serviceUuid + characteristicUuid(无横线小写) */
const FORCE_WRITE_NO_RESPONSE: Array<{ service: string; characteristic: string }> = [
  /** 佳博 GP-D320FX 等常见 Nordic UART 风格串口(与你机子日志一致) */
  {
    service: '49535343fe7d4ae58fa99fafd205e455',
    characteristic: '49535343884143f4a8d4ecbe34729bb3',
  },
]

export function blePairRequiresWriteNoResponse (
  serviceId: string,
  characteristicId: string
): boolean {
  const s = normalizeBleUuid(serviceId)
  const c = normalizeBleUuid(characteristicId)
  return FORCE_WRITE_NO_RESPONSE.some((p) => p.service === s && p.characteristic === c)
}

/** 是否为本仓库维护的 Nordic UART 风格串口服务(需写前开 notify、分包间留间隔等) */
export function isNordicUartStyleBleService (serviceId: string): boolean {
  const s = normalizeBleUuid(serviceId)
  return FORCE_WRITE_NO_RESPONSE.some((p) => p.service === s)
}