我正在尝试通过蓝牙低功耗进行通信,我们的CBCentralManager
实现已在Objective-C中完成,但我正在尝试通过Swift实现CBPeripheral
。
我无法建立连接。这就是我在CBPeripheral
实施中做广告的方式:
public func initialize()
{
let queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
perpheralManager = CBPeripheralManager(delegate: self, queue: queue)
startConnecting()
}
public func peripheralManagerDidUpdateState(peripheral: CBPeripheralManager)
{
switch peripheral.state
{
case CBPeripheralManagerState.PoweredOn:
setUpServices()
break
default:break
}
}
private func setUpServices()
{
self.slideIndexCharacterestics = CBMutableCharacteristic(type: CBUUID(string: UUID.transferSlideIndex), properties: [CBCharacteristicProperties.Read,CBCharacteristicProperties.NotifyEncryptionRequired],
value: nil, permissions: CBAttributePermissions.ReadEncryptionRequired)
let services = CBMutableService(type: CBUUID(string: UUID.transferService), primary: true)
services.characteristics = [self.slideIndexCharacterestics!]
self.perpheralManager!.addService(services)
}
public func peripheralManager(peripheral: CBPeripheralManager, didAddService service: CBService, error: NSError?)
{
startConnecting()
}
func startConnecting()
{
let adData = [CBAdvertisementDataServiceUUIDsKey:CBUUID(string:"D6087B90-F460-4BE1-A9C8-E1CD8E7858E9")]
self.perpheralManager!.stopAdvertising()
self.perpheralManager!.startAdvertising(adData)
}
注意:如果我在Objective-C两端实现,我能够成功通信。