bluetoothPrinterAllowlist.ts 1.36 KB
/** 扫描/配对列表仅展示以下蓝牙名称(大小写不敏感;支持完整匹配或关键字包含) */
export const ALLOWED_BLUETOOTH_PRINTER_NAMES = [
  'GP-D320FX-spp_A7FO',
  'Virtual BT Printer',
] as const

const ALLOWED_BLUETOOTH_PRINTER_NAME_SET = new Set(
  ALLOWED_BLUETOOTH_PRINTER_NAMES.map((name) => name.toLowerCase()),
)

/** 名称包含以下片段也视为允许(兼容系统配对名略有差异) */
const ALLOWED_BLUETOOTH_PRINTER_NAME_FRAGMENTS = [
  'virtual bt',
  'gp-d320fx',
  'd320fx',
] as const

export function normalizeBluetoothPrinterName (name: string | undefined | null): string {
  return String(name ?? '').trim()
}

/** 仅允许白名单内的蓝牙打印机名称出现在连接列表 */
export function isAllowedBluetoothPrinterName (name: string | undefined | null): boolean {
  const normalized = normalizeBluetoothPrinterName(name)
  if (!normalized) return false
  const lower = normalized.toLowerCase()
  if (ALLOWED_BLUETOOTH_PRINTER_NAME_SET.has(lower)) return true
  return ALLOWED_BLUETOOTH_PRINTER_NAME_FRAGMENTS.some((frag) => lower.includes(frag))
}

/** 一体机虚拟蓝牙名(走整页光栅与预览一致,不走 native printTemplate) */
export function isVirtualBtPrinterDeviceName (name: string | undefined | null): boolean {
  return normalizeBluetoothPrinterName(name).toLowerCase() === 'virtual bt printer'
}