dvAndroidBluetoothDeviceMap function

Map<String, Object?> dvAndroidBluetoothDeviceMap({
  1. required String address,
  2. required String? name,
  3. required int bondState,
  4. required Set<String> connectedAddresses,
})

A known device, in the shape DVBluetoothDevice.fromMap reads.

connectedAddresses is what BluetoothManager.getConnectedDevices says is connected over GATT. That is narrower than BlueZ's Connected, which covers every profile: a headset or a serial printer on a classic profile is connected and is not in this set, and reads here as disconnected. Reported as it is rather than left permanently false, because the alternative is not more honest, only less useful — and the gap is named here so the next person reading a "disconnected" printer knows where to look.

Implementation

Map<String, Object?> dvAndroidBluetoothDeviceMap({
  required String address,
  required String? name,
  required int bondState,
  required Set<String> connectedAddresses,
}) {
  final String upper = address.trim().toUpperCase();
  return <String, Object?>{
    // Android exports no object path. The MAC address is the identifier that
    // survives a reboot, and something unique has to go here or two
    // peripherals compare equal.
    'path': address,
    'address': address,
    if (name != null && name.isNotEmpty) 'name': name,
    // Only BOND_BONDED. BOND_BONDING is a dialog somebody has not answered,
    // and treating it as paired starts work against a printer that is not
    // bonded yet.
    'paired': bondState == dvAndroidBondBonded,
    'connected': connectedAddresses
        .any((String other) => other.trim().toUpperCase() == upper),
    // No RSSI: a bonded device carries none. It comes from a scan result, and
    // a number invented here would be read as a signal strength.
  };
}