ios - 外设和中央同时在同一个应用iOS11上
<p><p>我正在尝试制作一款用作蓝牙 watch (例如健身手环、智能 watch )主控件的应用。我已经对此进行了研究,尽管有些人设法做到了,但他们并没有提供有关该过程的很多细节。以下是我找到的一些“解决方案”:</p>
<p> <a href="https://stackoverflow.com/questions/24629243/is-it-possible-to-switch-central-and-peripheral-each-other-on-ios" rel="noreferrer noopener nofollow">Is it possible to switch central and peripheral each other on iOS?</a> </p>
<p> <a href="https://stackoverflow.com/questions/16985891/can-ios-do-central-and-peripheral-work-on-same-app-at-same-time?noredirect=1&lq=1" rel="noreferrer noopener nofollow">Can iOS do central and peripheral work on same app at same time?</a> </p>
<p> <a href="https://stackoverflow.com/questions/17732321/peripheral-and-central-at-the-same-time-on-ios" rel="noreferrer noopener nofollow">Peripheral and central at the same time on iOS</a> </p>
<p>所有这些都在 Objective-C 中,虽然我对它很熟悉,但这些帖子已经 3 年多了,所以关于代码的事情已经发生了变化。另一个问题是我需要将应用程序与另一个蓝牙设备一起使用,而不是像上面那样使用 iOS 设备,并且目前连接请求只能来自 iPhone,而不是来自蓝牙设备。</p >
<p>问题是是否有可能达到预期的结果,如果可以,最好的方法是什么?到目前为止,建议的解决方案之一是连接到设备,获取 UUID,然后将 iPhone 切换到外围模式,以便它可以宣传其服务。这是不可能的(在我看来),至少在现阶段是这样。 </p>
<p>iOS 已经有一个预定义的服务,当它们 2 连接时,设备可以发现和访问该服务(当前时间服务),无需我进行任何修改,因此应该有办法实现这一点。</p>
<p>我希望我对这个问题足够清楚,如果你相信我可以添加更多细节来澄清上下文,请告诉我。谢谢你的时间。 </p>
<p>我在下面发布了一些我发现外围设备的关键代码:</p>
<pre><code> override func viewDidAppear(_ animated: Bool) {
manager = CBCentralManager(delegate: self, queue: nil)
peripheralManager = CBPeripheralManager(delegate: self, queue: nil)
peripherals = []
if (manager?.state == CBManagerState.poweredOn) {
scanBLEDevices()
self.tableView.reloadData()
}
}
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
switch(peripheral.state)
{
case.unsupported:
print("Peripheral is not supported")
case.unauthorized:
print("Peripheral is unauthorized")
case.unknown:
print("Peripheral is Unknown")
case.resetting:
print("Peripheral is Resetting")
case.poweredOff:
print("Peripheral service is powered off")
case.poweredOn:
print("Peripheral service is powered on")
print("Start advertising.")
let serviceUUID:CBUUID = CBUUID(string: self.service_uuid_string)
let locationUUID:CBUUID = CBUUID(string: self.location_and_speed)
// Start with the CBMutableCharacteristic
self.locationCharacteristic = CBMutableCharacteristic(type: locationUUID, properties: .notify , value: nil, permissions: .readable)
// Then the service
let locationService = CBMutableService(type: serviceUUID, primary: true)
// Add the characteristic to the service
locationService.characteristics?.append(locationCharacteristic!)
// And add it to the peripheral manager
self.peripheralManager?.add(locationService)
peripheralManager?.startAdvertising()
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我正在以正确的方式实现所需功能。初始化 peripheralManager 后,创建一个 CBMutableService 并持有对它的引用(在类的顶部声明)。</p>
<p><code>var globalService:CBMutableService? = 无</code></p>
<p>下一步是检查 peripheralManager 的状态,并在收到 powerOn 状态后执行所有必需的工作:</p>
<pre><code>func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
switch(peripheral.state)
case.poweredOn:
print("Peripheral service is powered on")
createServiceWithCharacteristics()
}
func createServiceWithCharacteristics(){
let serviceUUID:CBUUID = CBUUID(string: self.service_uuid_string)
let featureCharacteristicUUID:CBUUID = CBUUID(string: self.feature_characteristic_uuid_string)
// Start with the CBMutableCharacteristic
let permissions: CBAttributePermissions = [.readable, .writeable]
let properties: CBCharacteristicProperties = [.notify, .read, .write]
self.featureCharacteristic = CBMutableCharacteristic(type: featureCharacteristicUUID, properties: properties , value: nil, permissions: permissions)
// Then the service
let localService = CBMutableService(type: serviceUUID, primary: true)
// Add the characteristic to the service
localService.characteristics =
globalService = localService
// And add it to the peripheral manager
self.peripheralManager?.add(globalService!)
print("Start advertising.")
peripheralManager?.startAdvertising()
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 外设和中央同时在同一个应用iOS11上,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/48405759/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/48405759/
</a>
</p>
页:
[1]