0

我有一个单例类,它在所有应用程序中为我处理附近的连接 API 实时连接,我想在列表中显示它的设备,但现在它仍然不适合我,如果对象有任何问题或更好表示它的方式我想知道太少的例子:

目的:

private val STRATEGY = Strategy.P2P_CLUSTER

data class ConnectedDevice(val endpointId: String, val connectionInfo: DiscoveredEndpointInfo)

object NearbyConnectionsApi {
    private lateinit var mConnectionsClient: ConnectionsClient

    fun setContext(currentContext: Context) {
        mConnectionsClient = Nearby.getConnectionsClient(currentContext)
    }

    var mDevicesList: MutableList<ConnectedDevice> = ArrayList() //the place i want to have                  live in the screen

    private val mEndpointDiscoveryCallback: EndpointDiscoveryCallback =
        object : EndpointDiscoveryCallback() {
            override fun onEndpointFound(
                endpointId: String,
                discoveredEndpointInfo: DiscoveredEndpointInfo
            ) {
                Log.d("nearby", "find $endpointId, ${discoveredEndpointInfo.endpointName}")
                mDevicesList.add(ConnectedDevice(endpointId, discoveredEndpointInfo))
            }

            override fun onEndpointLost(endpointId: String) {
                mDevicesList.remove(mDevicesList.find { it.endpointId == endpointId })
                // A previously discovered endpoint has gone away.
            }
        }
} 

屏幕:

@Composable
fun ScanDevicesScreen(
    navController: NavController
) {
    LazyColumn{
        items(NearbyConnectionsApi.mDevicesList){ item ->
            Row{
                Text(item.connectionInfo.serviceId)
                Text(item.connectionInfo.endpointName)
            }
        }
    }
}

我不知道为什么,但是列表没有自动更新(需要从字符串中取出,然后我回到那里重复,并在断开连接不起作用时删除项目,请建议,提前谢谢

4

0 回答 0