【发布时间】:2017-01-06 11:50:02
【问题描述】:
我试图将 iOS iPhone 设置为蓝牙外围设备,以便我可以从另一个 iDevice 看到它并发送信息。
我想我是在为自己做广告,但我在其他 iPhone、iPad 或 Mac 上都看不到它。控制台输出表明它是广告?我是否遗漏了一些明显的东西,(我环顾四周,用谷歌搜索过这里)但没有运气。
控制台:
self.peripheralManager powered on.
Service Added
peripheralManagerDidStartAdvertising(_:error:)
Advertising
代码:
import UIKit
import CoreBluetooth
class ViewController: UIViewController,CBPeripheralManagerDelegate {
var peripheralManager:CBPeripheralManager!
var transferCharacteristic: CBMutableCharacteristic?
let MyP_Service_UUID = CBUUID(string: "1B981746-2064-4F68-BBB8-69A185314FC6")
let MyP_Characteristics_UUID = CBUUID(string: "4271CEC1-652D-489B-8484-7C3550C6075E")
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.peripheralManager = CBPeripheralManager(delegate: self, queue: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
// Don't keep it going while we're not showing.
peripheralManager.stopAdvertising()
}
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
if (peripheral.state != .poweredOn) {
return
}
print("self.peripheralManager powered on.")
// ... so build our service.
let myCharacteristic = CBMutableCharacteristic(type: MyP_Characteristics_UUID, properties: [.notify], value: nil, permissions: .readable)
let myService = CBMutableService(type: MyP_Service_UUID, primary: true)
myService.characteristics?.append(myCharacteristic)
peripheralManager.add(myService)
peripheralManager.startAdvertising([ CBAdvertisementDataServiceUUIDsKey: [MyP_Service_UUID], CBAdvertisementDataLocalNameKey : "MyP"])
}
func peripheralManager(_ peripheral: CBPeripheralManager, didAdd service: CBService, error: Error?) {
print(error ?? "Service Added")
}
func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: Error?) {
print(#function)
print(error ?? "Advertising")
}
func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didSubscribeTo characteristic: CBCharacteristic) {
print(#function)
}
func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didUnsubscribeFrom characteristic: CBCharacteristic) {
print(#function)
}
func peripheralManagerIsReady(toUpdateSubscribers peripheral: CBPeripheralManager) {
print(#function)
}
}
【问题讨论】:
-
您如何尝试查看广告设备?您使用的是自己的代码还是 LightBlue 等应用程序
-
我想我现在明白了。我需要另一个设备来监听我的广告,因为它是 BLE。所以它不会出现在标准蓝牙设备中。也许我应该删除这个问题!
标签: ios bluetooth swift3 core-bluetooth