小编ser*_*o32的帖子

如何在后台定期向 IOS(swift)应用程序上的 BLE 外设发送数据?

我对 iOS 编程相当陌生,我正在使用 Swift 3 中的核心蓝牙来连接特定(定制)ble 外设并交换数据。

\n\n

如果用户在应用程序中选择特定模式,即使用户打开另一个应用程序或关闭屏幕,我也需要定期(每分钟)或在特定时间向设备发送数据(写入特征)。如果应用程序处于活动状态,我可以使用预定计时器来实现此目的,但一旦应用程序变为非活动状态,它就会停止。如果应用程序进入前台,有什么方法可以继续向外设发送数据?这可能吗?

\n\n

编辑

\n\n

这是我的背景模式(如 Deepak Tagadiya 所描述):\n背景模式

\n\n

下面是我的控制器的一些代码:

\n\n
class MyDevice: UIViewController {\n\nvar centralManager: CBCentralManager?\nvar selectedPeripheral: CBPeripheral?\nvar mRXCharacteristic: CBCharacteristic?\nvar mTXCharacteristic: CBCharacteristic?\nvar auto_timer = Timer()\n\n\n@IBOutlet weak var AutoSwitch: UISwitch!\n@IBAction func AutoSwitch(_ sender: UISwitch) {\n        if(sender.isOn == true){\n            activateAutoModeTask()\n        } else {\n           disableAutoModeTask()\n        }\n\n}\n\noverride func viewDidLoad() {\n    super.viewDidLoad()\n\n    centralManager = CBCentralManager(delegate: self, queue: nil)   \n    (\xe2\x80\xa6)\n}\n\nfunc activateAutoModeTask(){\n\n\n    auto_timer.invalidate() // just in case this button is tapped multiple times\n\n    // start the …
Run Code Online (Sandbox Code Playgroud)

ios core-bluetooth bluetooth-lowenergy swift

5
推荐指数
1
解决办法
3181
查看次数

标签 统计

bluetooth-lowenergy ×1

core-bluetooth ×1

ios ×1

swift ×1