如何解决WifiP2PManager.WIFI_P2P_PEERS_CHANGED_ACTION 在连接到对等点时正在广播而不是 WIFI_P2P_CONNECTION_CHANGED_ACTION
我正在尝试在 android 中使用 wifip2pmanager 连接两个设备。 能够列出对等点并连接所选设备。
以下给定的代码片段在选择对等方进行连接时执行
val device = deviceArray[position]
val config = wifip2pConfig()
config.deviceAddress = device.deviceAddress
config.wps.setup = WpsInfo.PBC
channel?.also { channel ->
manager?.connect(channel,config,object : wifip2pManager.ActionListener {
override fun onSuccess() {
Toast.makeText(applicationContext,"Connected to " + device.deviceName,Toast.LENGTH_SHORT).show()
}
override fun onFailure(reason: Int) {
Toast.makeText(applicationContext,"Not Connected",Toast.LENGTH_SHORT).show()
}
}
)}
但是peer连接后,intent广播是WIFI_P2P_PEERS_CHANGED_ACTION而不是WIFI_P2P_CONNECTION_CHANGED_ACTION。在 android 版本 7 的设备上尝试。 我按照 android WifiDirect 中所述的所有步骤和代码进行操作。
这是我在 broadcastReceiver 实现中的 onReceive 代码部分。
override fun onReceive(context: Context,intent: Intent) {
val action: String? = intent.action
when (action) {
wifip2pManager.WIFI_P2P_STATE_CHANGED_ACTION -> {
// Check to see if Wi-Fi is enabled and notify appropriate activity
val state = intent.getIntExtra(wifip2pManager.EXTRA_WIFI_STATE,-1)
if (state == wifip2pManager.WIFI_P2P_STATE_ENABLED) {
Toast.makeText(context,"Wifi is ON",Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(context,"Wifi is OFF",Toast.LENGTH_SHORT).show()
}
}
wifip2pManager.WIFI_P2P_PEERS_CHANGED_ACTION -> {
// Call wifip2pManager.requestPeers() to get a list of current peers
if (manager != null) {
manager.requestPeers(channel,activity.peerListListener);
}
}
wifip2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION -> {
// Respond to new connection or disconnections
if (manager == null) {
return
}
if (isNetworkAvailable(context)) {
activity.connectionStatus.setText("Device Connected")
manager.requestConnectionInfo(channel,activity.connectionInfoListener)
} else {
activity.connectionStatus.setText("Device disconnected")
}
}
wifip2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION -> {
// Respond to this device's wifi state changing
}
}
}
任何帮助将不胜感激..
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。