feat: 配对信息二维码与主控端导入
- 被控端新增 zxing 二维码,展示 lancast://pair 链接(含设备 id、指纹、TLS 指纹位) - 配对 URI 增加 id/name/tls 字段,端口与指纹仍由手机端生成 - 主控端设备列表新增「导入配对信息」,粘贴链接即可添加设备并写入 pin - 导入设备落盘 ttstd_paired_devices.json,与发现结果按 id/ip+端口去重
This commit is contained in:
1 parent
1548139051
commit
9a82766fa2
8 files changed
+338
-6
No files matched your search
@@ -70,4 +70,6 @@ android {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation("org.jetbrains.kotlin:kotlin-stdlib:2.0.21")
|
implementation("org.jetbrains.kotlin:kotlin-stdlib:2.0.21")
|
||||||
|
// 配对二维码(纯 Java 编码,无额外传递依赖)。
|
||||||
|
implementation("com.google.zxing:core:3.5.4")
|
||||||
}
|
}
|
||||||
@@ -9,7 +9,9 @@ import android.os.Build
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
import android.os.Looper
|
import android.os.Looper
|
||||||
|
import android.view.View
|
||||||
import android.widget.Button
|
import android.widget.Button
|
||||||
|
import android.widget.ImageView
|
||||||
import android.widget.LinearLayout
|
import android.widget.LinearLayout
|
||||||
import android.widget.ScrollView
|
import android.widget.ScrollView
|
||||||
import android.widget.Switch
|
import android.widget.Switch
|
||||||
@@ -18,8 +20,10 @@ import android.widget.Toast
|
|||||||
import com.ttstd.lancast.receiver.discovery.HostBeacon
|
import com.ttstd.lancast.receiver.discovery.HostBeacon
|
||||||
import com.ttstd.lancast.receiver.file.StorageAccess
|
import com.ttstd.lancast.receiver.file.StorageAccess
|
||||||
import com.ttstd.lancast.receiver.session.PairingCode
|
import com.ttstd.lancast.receiver.session.PairingCode
|
||||||
|
import com.ttstd.lancast.receiver.util.DeviceIdentity
|
||||||
import com.ttstd.lancast.receiver.util.Fingerprint
|
import com.ttstd.lancast.receiver.util.Fingerprint
|
||||||
import com.ttstd.lancast.receiver.util.Privileged
|
import com.ttstd.lancast.receiver.util.Privileged
|
||||||
|
import com.ttstd.lancast.receiver.util.QrCode
|
||||||
import java.net.NetworkInterface
|
import java.net.NetworkInterface
|
||||||
|
|
||||||
class MainActivity : Activity() {
|
class MainActivity : Activity() {
|
||||||
@@ -40,6 +44,7 @@ class MainActivity : Activity() {
|
|||||||
private lateinit var status: TextView
|
private lateinit var status: TextView
|
||||||
private lateinit var endpoint: TextView
|
private lateinit var endpoint: TextView
|
||||||
private lateinit var pairing: TextView
|
private lateinit var pairing: TextView
|
||||||
|
private lateinit var pairingQr: ImageView
|
||||||
private lateinit var fileInfo: TextView
|
private lateinit var fileInfo: TextView
|
||||||
private lateinit var storageInfo: TextView
|
private lateinit var storageInfo: TextView
|
||||||
private lateinit var autoSwitch: Switch
|
private lateinit var autoSwitch: Switch
|
||||||
@@ -59,6 +64,11 @@ class MainActivity : Activity() {
|
|||||||
status = TextView(this).apply { textSize = 18f }
|
status = TextView(this).apply { textSize = 18f }
|
||||||
endpoint = TextView(this).apply { textSize = 13f }
|
endpoint = TextView(this).apply { textSize = 13f }
|
||||||
pairing = TextView(this).apply { textSize = 13f }
|
pairing = TextView(this).apply { textSize = 13f }
|
||||||
|
// 主控端可以直接扫码导入,省掉手输 IP / 核对指纹。
|
||||||
|
pairingQr = ImageView(this).apply {
|
||||||
|
adjustViewBounds = true
|
||||||
|
setPadding(0, 8, 0, 8)
|
||||||
|
}
|
||||||
fileInfo = TextView(this).apply { textSize = 13f }
|
fileInfo = TextView(this).apply { textSize = 13f }
|
||||||
storageInfo = TextView(this).apply { textSize = 12f }
|
storageInfo = TextView(this).apply { textSize = 12f }
|
||||||
|
|
||||||
@@ -114,6 +124,7 @@ class MainActivity : Activity() {
|
|||||||
addView(status)
|
addView(status)
|
||||||
addView(endpoint)
|
addView(endpoint)
|
||||||
addView(pairing)
|
addView(pairing)
|
||||||
|
addView(pairingQr)
|
||||||
addView(autoSwitch)
|
addView(autoSwitch)
|
||||||
addView(confirmSwitch)
|
addView(confirmSwitch)
|
||||||
addView(authorize)
|
addView(authorize)
|
||||||
@@ -263,14 +274,28 @@ class MainActivity : Activity() {
|
|||||||
} else {
|
} else {
|
||||||
"本机 $ip · 主控端自动发现(UDP ${HostBeacon.PORT})"
|
"本机 $ip · 主控端自动发现(UDP ${HostBeacon.PORT})"
|
||||||
}
|
}
|
||||||
val pairingText = if (ip.isEmpty()) {
|
val pairingUri = if (ip.isEmpty()) {
|
||||||
|
""
|
||||||
|
} else {
|
||||||
|
PairingCode.build(
|
||||||
|
ip = ip,
|
||||||
|
port = 41001,
|
||||||
|
fingerprint = Fingerprint.of(this),
|
||||||
|
deviceId = DeviceIdentity.of(this),
|
||||||
|
name = "Android Receiver",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
val pairingText = if (pairingUri.isEmpty()) {
|
||||||
"配对码:无法获取本机 IP"
|
"配对码:无法获取本机 IP"
|
||||||
} else {
|
} else {
|
||||||
"配对码:\n${PairingCode.build(ip, 41001, Fingerprint.of(this))}"
|
"配对信息(主控端可扫码或粘贴):\n$pairingUri"
|
||||||
}
|
}
|
||||||
|
val qr = if (pairingUri.isEmpty()) null else QrCode.bitmap(pairingUri)
|
||||||
runOnUiThread {
|
runOnUiThread {
|
||||||
endpoint.text = endpointText
|
endpoint.text = endpointText
|
||||||
pairing.text = pairingText
|
pairing.text = pairingText
|
||||||
|
pairingQr.setImageBitmap(qr)
|
||||||
|
pairingQr.visibility = if (qr == null) View.GONE else View.VISIBLE
|
||||||
}
|
}
|
||||||
}.start()
|
}.start()
|
||||||
}
|
}
|
||||||
|
|||||||
+22
-1
@@ -2,13 +2,34 @@ package com.ttstd.lancast.receiver.session
|
|||||||
|
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配对信息 URI:
|
||||||
|
*
|
||||||
|
* `lancast://pair?ip=&port=&fp=&id=&name=&tls=`
|
||||||
|
*
|
||||||
|
* - `fp`:应用签名证书 SHA-256,主控端 TOFU 比对用;
|
||||||
|
* - `id`:稳定设备 id,中继寻址与指纹记录都用它;
|
||||||
|
* - `tls`:TLS 自签证书 SHA-256(启用 TLS 时才有),主控端据此校验传输层。
|
||||||
|
*/
|
||||||
object PairingCode {
|
object PairingCode {
|
||||||
fun build(ip: String, port: Int, fingerprint: String): String = Uri.Builder()
|
fun build(
|
||||||
|
ip: String,
|
||||||
|
port: Int,
|
||||||
|
fingerprint: String,
|
||||||
|
deviceId: String = "",
|
||||||
|
name: String = "",
|
||||||
|
tlsFingerprint: String = "",
|
||||||
|
): String = Uri.Builder()
|
||||||
.scheme("lancast")
|
.scheme("lancast")
|
||||||
.authority("pair")
|
.authority("pair")
|
||||||
.appendQueryParameter("ip", ip)
|
.appendQueryParameter("ip", ip)
|
||||||
.appendQueryParameter("port", port.toString())
|
.appendQueryParameter("port", port.toString())
|
||||||
.appendQueryParameter("fp", fingerprint)
|
.appendQueryParameter("fp", fingerprint)
|
||||||
|
.apply {
|
||||||
|
if (deviceId.isNotBlank()) appendQueryParameter("id", deviceId)
|
||||||
|
if (name.isNotBlank()) appendQueryParameter("name", name)
|
||||||
|
if (tlsFingerprint.isNotBlank()) appendQueryParameter("tls", tlsFingerprint)
|
||||||
|
}
|
||||||
.build()
|
.build()
|
||||||
.toString()
|
.toString()
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
package com.ttstd.lancast.receiver.util
|
||||||
|
|
||||||
|
import android.graphics.Bitmap
|
||||||
|
import android.graphics.Color
|
||||||
|
import android.util.Log
|
||||||
|
import com.google.zxing.BarcodeFormat
|
||||||
|
import com.google.zxing.EncodeHintType
|
||||||
|
import com.google.zxing.qrcode.QRCodeWriter
|
||||||
|
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel
|
||||||
|
|
||||||
|
/** 配对二维码:把 `lancast://pair?...` 渲染成方形位图。 */
|
||||||
|
object QrCode {
|
||||||
|
private const val TAG = "QrCode"
|
||||||
|
|
||||||
|
fun bitmap(text: String, sizePx: Int = 600): Bitmap? = runCatching {
|
||||||
|
val matrix = QRCodeWriter().encode(
|
||||||
|
text,
|
||||||
|
BarcodeFormat.QR_CODE,
|
||||||
|
sizePx,
|
||||||
|
sizePx,
|
||||||
|
mapOf(
|
||||||
|
EncodeHintType.ERROR_CORRECTION to ErrorCorrectionLevel.M,
|
||||||
|
EncodeHintType.MARGIN to 1,
|
||||||
|
EncodeHintType.CHARACTER_SET to "UTF-8",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
val pixels = IntArray(matrix.width * matrix.height)
|
||||||
|
for (y in 0 until matrix.height) {
|
||||||
|
val row = y * matrix.width
|
||||||
|
for (x in 0 until matrix.width) {
|
||||||
|
pixels[row + x] = if (matrix[x, y]) Color.BLACK else Color.WHITE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Bitmap.createBitmap(pixels, matrix.width, matrix.height, Bitmap.Config.ARGB_8888)
|
||||||
|
}.onFailure { Log.w(TAG, "二维码生成失败", it) }.getOrNull()
|
||||||
|
}
|
||||||
@@ -51,6 +51,8 @@ class DeviceInfo {
|
|||||||
final String model;
|
final String model;
|
||||||
final String resolution;
|
final String resolution;
|
||||||
final String? signalingUrl;
|
final String? signalingUrl;
|
||||||
|
/// 被控端 TLS 自签证书的 SHA-256(为空表示对端不支持 TLS)。
|
||||||
|
final String tlsFingerprint;
|
||||||
|
|
||||||
const DeviceInfo({
|
const DeviceInfo({
|
||||||
required this.id,
|
required this.id,
|
||||||
@@ -63,6 +65,7 @@ class DeviceInfo {
|
|||||||
this.model = '',
|
this.model = '',
|
||||||
this.resolution = '1920x1080',
|
this.resolution = '1920x1080',
|
||||||
this.signalingUrl,
|
this.signalingUrl,
|
||||||
|
this.tlsFingerprint = '',
|
||||||
});
|
});
|
||||||
|
|
||||||
factory DeviceInfo.fromJson(Map<String, dynamic> json) {
|
factory DeviceInfo.fromJson(Map<String, dynamic> json) {
|
||||||
@@ -79,6 +82,7 @@ class DeviceInfo {
|
|||||||
model: (json['model'] ?? '').toString(),
|
model: (json['model'] ?? '').toString(),
|
||||||
resolution: (json['res'] ?? '1920x1080').toString(),
|
resolution: (json['res'] ?? '1920x1080').toString(),
|
||||||
signalingUrl: json['sig']?.toString(),
|
signalingUrl: json['sig']?.toString(),
|
||||||
|
tlsFingerprint: (json['tls'] ?? json['tlsFp'] ?? '').toString(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,6 +97,7 @@ class DeviceInfo {
|
|||||||
'model': model,
|
'model': model,
|
||||||
'res': resolution,
|
'res': resolution,
|
||||||
if (signalingUrl != null) 'sig': signalingUrl,
|
if (signalingUrl != null) 'sig': signalingUrl,
|
||||||
|
if (tlsFingerprint.isNotEmpty) 'tls': tlsFingerprint,
|
||||||
};
|
};
|
||||||
|
|
||||||
bool supports(String capability) => capabilities.contains(capability);
|
bool supports(String capability) => capabilities.contains(capability);
|
||||||
|
|||||||
@@ -0,0 +1,141 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'models.dart';
|
||||||
|
|
||||||
|
/// `lancast://pair?ip=&port=&fp=&id=&name=&tls=` 配对信息。
|
||||||
|
///
|
||||||
|
/// 被控端把这条 URI 渲染成二维码展示;主控端扫码(或直接粘贴文本)即可导入设备,
|
||||||
|
/// 省掉手输 IP 和核对指纹。`fp` 是应用签名证书指纹,`tls` 是 TLS 自签证书指纹(可选)。
|
||||||
|
class PairingUri {
|
||||||
|
const PairingUri({
|
||||||
|
required this.ip,
|
||||||
|
required this.port,
|
||||||
|
this.fingerprint = '',
|
||||||
|
this.deviceId = '',
|
||||||
|
this.name = '',
|
||||||
|
this.tlsFingerprint = '',
|
||||||
|
});
|
||||||
|
|
||||||
|
final String ip;
|
||||||
|
final int port;
|
||||||
|
final String fingerprint;
|
||||||
|
final String deviceId;
|
||||||
|
final String name;
|
||||||
|
final String tlsFingerprint;
|
||||||
|
|
||||||
|
static PairingUri? parse(String text) {
|
||||||
|
final trimmed = text.trim();
|
||||||
|
if (trimmed.isEmpty) return null;
|
||||||
|
final uri = Uri.tryParse(trimmed);
|
||||||
|
if (uri == null) return null;
|
||||||
|
if (uri.scheme.toLowerCase() != 'lancast') return null;
|
||||||
|
if (uri.host.toLowerCase() != 'pair') return null;
|
||||||
|
final ip = (uri.queryParameters['ip'] ?? '').trim();
|
||||||
|
final port = int.tryParse(uri.queryParameters['port'] ?? '');
|
||||||
|
if (ip.isEmpty || port == null || port <= 0 || port > 65535) return null;
|
||||||
|
return PairingUri(
|
||||||
|
ip: ip,
|
||||||
|
port: port,
|
||||||
|
fingerprint: (uri.queryParameters['fp'] ?? '').trim(),
|
||||||
|
deviceId: (uri.queryParameters['id'] ?? '').trim(),
|
||||||
|
name: (uri.queryParameters['name'] ?? '').trim(),
|
||||||
|
tlsFingerprint: (uri.queryParameters['tls'] ?? '').trim(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
DeviceInfo toDevice() {
|
||||||
|
final resolvedName = name.isNotEmpty ? name : ip;
|
||||||
|
return DeviceInfo(
|
||||||
|
id: deviceId.isNotEmpty ? deviceId : '$ip:$port',
|
||||||
|
name: resolvedName,
|
||||||
|
ip: ip,
|
||||||
|
mediaPort: 41000,
|
||||||
|
controlPort: port,
|
||||||
|
fingerprint: fingerprint,
|
||||||
|
tlsFingerprint: tlsFingerprint,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 已导入的配对设备,落盘 `ttstd_paired_devices.json`。
|
||||||
|
///
|
||||||
|
/// 与发现结果按「设备 id,缺失时 ip+控制端口」去重;发现结果优先(信息更新),
|
||||||
|
/// 但导入记录能保证设备不在线时也留一个可连接的条目。
|
||||||
|
class PairedDeviceStore {
|
||||||
|
PairedDeviceStore({this.fileName = 'ttstd_paired_devices.json'});
|
||||||
|
|
||||||
|
final String fileName;
|
||||||
|
final List<DeviceInfo> _devices = <DeviceInfo>[];
|
||||||
|
File? _file;
|
||||||
|
|
||||||
|
bool get isReady => _file != null;
|
||||||
|
|
||||||
|
String get path => _file?.path ?? '';
|
||||||
|
|
||||||
|
List<DeviceInfo> get devices => List<DeviceInfo>.unmodifiable(_devices);
|
||||||
|
|
||||||
|
void load() {
|
||||||
|
for (final directory in _directories()) {
|
||||||
|
try {
|
||||||
|
final candidate = File('${directory.path}${Platform.pathSeparator}$fileName');
|
||||||
|
if (!candidate.existsSync()) {
|
||||||
|
candidate.writeAsStringSync('[]', flush: true);
|
||||||
|
}
|
||||||
|
final decoded = jsonDecode(candidate.readAsStringSync());
|
||||||
|
if (decoded is List) {
|
||||||
|
_devices
|
||||||
|
..clear()
|
||||||
|
..addAll(
|
||||||
|
decoded.whereType<Map<dynamic, dynamic>>().map(
|
||||||
|
(item) => DeviceInfo.fromJson(Map<String, dynamic>.from(item)),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
_file = candidate;
|
||||||
|
return;
|
||||||
|
} on Object {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 保存一台设备;同 id(或同 ip:端口)的旧记录会被覆盖。
|
||||||
|
void save(DeviceInfo device) {
|
||||||
|
_devices.removeWhere((item) => sameDevice(item, device));
|
||||||
|
_devices.add(device);
|
||||||
|
_flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
void remove(DeviceInfo device) {
|
||||||
|
final before = _devices.length;
|
||||||
|
_devices.removeWhere((item) => sameDevice(item, device));
|
||||||
|
if (_devices.length != before) _flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool sameDevice(DeviceInfo left, DeviceInfo right) {
|
||||||
|
final leftId = left.id.trim();
|
||||||
|
final rightId = right.id.trim();
|
||||||
|
if (leftId.isNotEmpty && rightId.isNotEmpty) return leftId == rightId;
|
||||||
|
return left.ip == right.ip && left.controlPort == right.controlPort;
|
||||||
|
}
|
||||||
|
|
||||||
|
void _flush() {
|
||||||
|
final file = _file;
|
||||||
|
if (file == null) return;
|
||||||
|
try {
|
||||||
|
file.writeAsStringSync(
|
||||||
|
jsonEncode(_devices.map((device) => device.toJson()).toList()),
|
||||||
|
flush: true,
|
||||||
|
);
|
||||||
|
} on Object {
|
||||||
|
// 落盘失败不影响本次运行。
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static List<Directory> _directories() => <Directory>[
|
||||||
|
File(Platform.resolvedExecutable).parent,
|
||||||
|
Directory.current,
|
||||||
|
Directory.systemTemp,
|
||||||
|
];
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ class DeviceList extends StatelessWidget {
|
|||||||
required this.selectedId,
|
required this.selectedId,
|
||||||
required this.onSelected,
|
required this.onSelected,
|
||||||
required this.onRefresh,
|
required this.onRefresh,
|
||||||
|
this.onPair,
|
||||||
this.refreshing = false,
|
this.refreshing = false,
|
||||||
this.shrinkWrap = false,
|
this.shrinkWrap = false,
|
||||||
});
|
});
|
||||||
@@ -17,6 +18,9 @@ class DeviceList extends StatelessWidget {
|
|||||||
final String? selectedId;
|
final String? selectedId;
|
||||||
final ValueChanged<DeviceInfo> onSelected;
|
final ValueChanged<DeviceInfo> onSelected;
|
||||||
final VoidCallback onRefresh;
|
final VoidCallback onRefresh;
|
||||||
|
|
||||||
|
/// 导入配对信息(扫码/粘贴 `lancast://pair?...`)。
|
||||||
|
final VoidCallback? onPair;
|
||||||
final bool refreshing;
|
final bool refreshing;
|
||||||
final bool shrinkWrap;
|
final bool shrinkWrap;
|
||||||
|
|
||||||
@@ -35,6 +39,12 @@ class DeviceList extends StatelessWidget {
|
|||||||
Text('可连接设备', style: theme.textTheme.titleMedium),
|
Text('可连接设备', style: theme.textTheme.titleMedium),
|
||||||
const Spacer(),
|
const Spacer(),
|
||||||
Text('${devices.length} 台', style: theme.textTheme.bodySmall),
|
Text('${devices.length} 台', style: theme.textTheme.bodySmall),
|
||||||
|
if (onPair != null)
|
||||||
|
IconButton(
|
||||||
|
tooltip: '导入配对信息',
|
||||||
|
onPressed: onPair,
|
||||||
|
icon: const Icon(Icons.qr_code_scanner),
|
||||||
|
),
|
||||||
IconButton(
|
IconButton(
|
||||||
tooltip: '重新发现',
|
tooltip: '重新发现',
|
||||||
onPressed: onRefresh,
|
onPressed: onRefresh,
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import 'core/diagnostic_log.dart';
|
|||||||
import 'core/control_file_client.dart';
|
import 'core/control_file_client.dart';
|
||||||
import 'core/native_core.dart';
|
import 'core/native_core.dart';
|
||||||
import 'core/models.dart';
|
import 'core/models.dart';
|
||||||
|
import 'core/pairing.dart';
|
||||||
import 'core/pin_store.dart';
|
import 'core/pin_store.dart';
|
||||||
import 'core/watch_client.dart';
|
import 'core/watch_client.dart';
|
||||||
import 'features/control/control_toolbar.dart';
|
import 'features/control/control_toolbar.dart';
|
||||||
@@ -50,6 +51,7 @@ class _HomePageState extends State<HomePage> {
|
|||||||
late final SenderBridge _bridge;
|
late final SenderBridge _bridge;
|
||||||
final DiagnosticLog _diagnosticLog = DiagnosticLog();
|
final DiagnosticLog _diagnosticLog = DiagnosticLog();
|
||||||
final PinStore _pins = PinStore();
|
final PinStore _pins = PinStore();
|
||||||
|
final PairedDeviceStore _paired = PairedDeviceStore();
|
||||||
Timer? _demoStatusTimer;
|
Timer? _demoStatusTimer;
|
||||||
Timer? _diagnosticTimer;
|
Timer? _diagnosticTimer;
|
||||||
Timer? _connectWatchdog;
|
Timer? _connectWatchdog;
|
||||||
@@ -111,6 +113,11 @@ class _HomePageState extends State<HomePage> {
|
|||||||
_diagnosticLog.write('app initialized; nativeError=${_nativeError ?? '-'}');
|
_diagnosticLog.write('app initialized; nativeError=${_nativeError ?? '-'}');
|
||||||
_pins.load();
|
_pins.load();
|
||||||
_diagnosticLog.write('pinned devices file: ${_pins.path}');
|
_diagnosticLog.write('pinned devices file: ${_pins.path}');
|
||||||
|
_paired.load();
|
||||||
|
_diagnosticLog.write('paired devices file: ${_paired.path}');
|
||||||
|
if (_paired.devices.isNotEmpty) {
|
||||||
|
_devices = _mergePaired(const <DeviceInfo>[]);
|
||||||
|
}
|
||||||
_bridge.watchPhase().listen((phase) {
|
_bridge.watchPhase().listen((phase) {
|
||||||
_diagnosticLog.write('phase event -> ${phase.wireName} (${phase.label})');
|
_diagnosticLog.write('phase event -> ${phase.wireName} (${phase.label})');
|
||||||
if (mounted) setState(() => _phase = phase);
|
if (mounted) setState(() => _phase = phase);
|
||||||
@@ -669,22 +676,105 @@ class _HomePageState extends State<HomePage> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setState(() => _refreshing = true);
|
setState(() => _refreshing = true);
|
||||||
final devices = await _bridge.discover(signalingUrl: _effectiveSignalingUrl);
|
final discovered = await _bridge.discover(signalingUrl: _effectiveSignalingUrl);
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
|
final devices = _mergePaired(discovered);
|
||||||
setState(() {
|
setState(() {
|
||||||
_devices = devices;
|
_devices = devices;
|
||||||
if (_selected == null || !devices.any((device) => device.id == _selected!.id)) {
|
if (_selected == null ||
|
||||||
|
!devices.any((device) => PairedDeviceStore.sameDevice(device, _selected!))) {
|
||||||
_selected = devices.isEmpty ? null : devices.first;
|
_selected = devices.isEmpty ? null : devices.first;
|
||||||
}
|
}
|
||||||
_refreshing = false;
|
_refreshing = false;
|
||||||
});
|
});
|
||||||
_diagnosticLog.write('discover result: ${devices.length} devices');
|
_diagnosticLog.write(
|
||||||
|
'discover result: ${discovered.length} found, ${devices.length} shown '
|
||||||
|
'(${_paired.devices.length} paired)',
|
||||||
|
);
|
||||||
if (_refreshQueued) {
|
if (_refreshQueued) {
|
||||||
_refreshQueued = false;
|
_refreshQueued = false;
|
||||||
unawaited(_refreshDevices());
|
unawaited(_refreshDevices());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 发现结果与已导入的配对设备合并:发现结果优先,导入记录补位。
|
||||||
|
List<DeviceInfo> _mergePaired(List<DeviceInfo> discovered) {
|
||||||
|
final merged = <DeviceInfo>[...discovered];
|
||||||
|
for (final paired in _paired.devices) {
|
||||||
|
final index = merged.indexWhere((device) => PairedDeviceStore.sameDevice(device, paired));
|
||||||
|
if (index < 0) merged.add(paired);
|
||||||
|
}
|
||||||
|
return merged;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 导入 `lancast://pair?...`(扫码内容或手工粘贴)。
|
||||||
|
Future<void> _showPairingDialog() async {
|
||||||
|
final controller = TextEditingController();
|
||||||
|
final text = await showDialog<String>(
|
||||||
|
context: context,
|
||||||
|
builder: (context) => AlertDialog(
|
||||||
|
title: const Text('导入配对信息'),
|
||||||
|
content: Column(
|
||||||
|
mainAxisSize: MainAxisSize.min,
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
|
children: [
|
||||||
|
const Text(
|
||||||
|
'用手机扫描被控端界面上的二维码,把得到的链接粘贴到这里;'
|
||||||
|
'也可以让被控端把链接文本发给你。',
|
||||||
|
style: TextStyle(fontSize: 13),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 12),
|
||||||
|
TextField(
|
||||||
|
controller: controller,
|
||||||
|
autofocus: true,
|
||||||
|
minLines: 1,
|
||||||
|
maxLines: 3,
|
||||||
|
decoration: const InputDecoration(
|
||||||
|
labelText: 'lancast://pair?...',
|
||||||
|
hintText: 'lancast://pair?ip=192.168.1.10&port=41001&fp=...',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.of(context).pop(),
|
||||||
|
child: const Text('取消'),
|
||||||
|
),
|
||||||
|
FilledButton(
|
||||||
|
onPressed: () => Navigator.of(context).pop(controller.text),
|
||||||
|
child: const Text('导入'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
controller.dispose();
|
||||||
|
if (text == null || text.trim().isEmpty) return;
|
||||||
|
final pairing = PairingUri.parse(text);
|
||||||
|
if (pairing == null) {
|
||||||
|
_snack('配对信息无法识别,请确认粘贴的是 lancast://pair?... 链接');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final device = pairing.toDevice();
|
||||||
|
// 配对链接本身就是带外渠道,导入即可信:直接写 pin,后续连接走一致性校验。
|
||||||
|
if (device.fingerprint.isNotEmpty) {
|
||||||
|
_pins.pin(_pinKeyOf(device), device.fingerprint);
|
||||||
|
}
|
||||||
|
_paired.save(device);
|
||||||
|
_diagnosticLog.write(
|
||||||
|
'paired device imported: ${device.id} ${device.ip}:${device.controlPort} '
|
||||||
|
'tls=${device.tlsFingerprint.isEmpty ? '-' : device.tlsFingerprint}',
|
||||||
|
);
|
||||||
|
setState(() {
|
||||||
|
_devices = _mergePaired(_devices);
|
||||||
|
_selected = device;
|
||||||
|
});
|
||||||
|
_snack('已导入 ${device.name}');
|
||||||
|
}
|
||||||
|
|
||||||
|
static String _pinKeyOf(DeviceInfo device) =>
|
||||||
|
device.id.trim().isNotEmpty ? device.id.trim() : '${device.ip}:${device.controlPort}';
|
||||||
|
|
||||||
Future<void> _connect() async {
|
Future<void> _connect() async {
|
||||||
final device = _selected;
|
final device = _selected;
|
||||||
if (device == null) return;
|
if (device == null) return;
|
||||||
@@ -942,6 +1032,7 @@ class _HomePageState extends State<HomePage> {
|
|||||||
selectedId: _selected?.id,
|
selectedId: _selected?.id,
|
||||||
onSelected: (device) => setState(() => _selected = device),
|
onSelected: (device) => setState(() => _selected = device),
|
||||||
onRefresh: _refreshDevices,
|
onRefresh: _refreshDevices,
|
||||||
|
onPair: _showPairingDialog,
|
||||||
refreshing: _refreshing,
|
refreshing: _refreshing,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@@ -965,6 +1056,7 @@ class _HomePageState extends State<HomePage> {
|
|||||||
selectedId: _selected?.id,
|
selectedId: _selected?.id,
|
||||||
onSelected: (device) => setState(() => _selected = device),
|
onSelected: (device) => setState(() => _selected = device),
|
||||||
onRefresh: _refreshDevices,
|
onRefresh: _refreshDevices,
|
||||||
|
onPair: _showPairingDialog,
|
||||||
refreshing: _refreshing,
|
refreshing: _refreshing,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|||||||
Reference in new issue
Block a user