英文:
startLocalOnlyHotspot and create hotspot programaticaly
问题
startLocalOnlyHotspot 和创建热点的编程方式
我尝试以编程方式创建一个类似于此应用的热点,但问题是热点创建后立即消失。
以下是我正在使用的代码:
private val wifiManager: WifiManager by lazy {
(applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager)
}
startLocalOnlyHotspot(object : WifiManager.LocalOnlyHotspotCallback() {
override fun onStarted(reservation: WifiManager.LocalOnlyHotspotReservation) {
super.onStarted(reservation)
}
override fun onStopped() {
super.onStopped()
}
override fun onFailed(reason: Int) {
super.onFailed(reason)
}
}, Handler(Looper.getMainLooper()))
我还尝试过使用前台服务和 WorkManager,但没有成功。当我检查我的 logcat 时,我看到热点被创建,但立即消失,并显示以下错误消息:
系统资源无法调用 close。
英文:
startLocalOnlyHotspot and create hotspot programaticaly
I am trying to create a hotspot programmatically similar to this app my problem is hotspot
create and immediately disappear
Here is the code I am using:
private val wifiManager: WifiManager by lazy {
(applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager)
}
startLocalOnlyHotspot(object : WifiManager.LocalOnlyHotspotCallback() {
override fun onStarted(reservation: WifiManager.LocalOnlyHotspotReservation) {
super.onStarted(reservation)
}
override fun onStopped() {
super.onStopped()
}
override fun onFailed(reason: Int) {
super.onFailed(reason)
}
}, Handler(Looper.getMainLooper()))
I have also tried this code with foreground service and WorkManager but it did not work. When I check my logcat, I see that the hotspot is created but disappears immediately with the following error message:
> System resource failed to call close.
答案1
得分: 0
将reservation
对象保留为字段,像这样:
private WifiManager.LocalOnlyHotspotReservation reservation
英文:
keep the reservation
object as a field like this :
private WifiManager.LocalOnlyHotspotReservation reservation
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论