我已经离开并设置我的CBCentralManager
来搜索设备,并准备好接收基本结构并检查更新的信息。
我似乎无法掌握如何制作CBPeripheralManager
ViewController以及如何通过按下按钮从单独的应用程序发送我的CBCentral
数据。最简单的方法是发送一些字符串。
这是我的CBCentralManager
ViewController。
class ViewController: NSViewController, CBCentralManagerDelegate,CBPeripheralDelegate {
let TRANSFER_SERVICE_UUID = "FB694B90-F49E-4597-8306-171BBA78F846"
let TRANSFER_CHARACTERISTIC_UUID = "EB6727C4-F184-497A-A656-76B0CDAC633A"
var centralManager: CBCentralManager?
var discoveredPeripheral: CBPeripheral?
override func viewDidLoad() {
super.viewDidLoad()
centralManager = CBCentralManager(delegate: self, queue: nil)
// Do any additional setup after loading the view.
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
if (central.state != .poweredOn) {
return
}
else{
let serviceUUID:[CBUUID] = [CBUUID(string: self.TRANSFER_SERVICE_UUID)]
centralManager!.scanForPeripherals(withServices: serviceUUID, options: nil)
}
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
print("Discovered a peripheral")
print(peripheral.identifier)
print(peripheral.name!)
print(RSSI)
if(discoveredPeripheral != peripheral){
discoveredPeripheral = peripheral
centralManager?.stopScan()
print("Connection to peripheral")
centralManager?.connect(peripheral, options: nil)
}
}
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
print(error!.localizedDescription)
centralManager?.cancelPeripheralConnection(peripheral)
}
func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
print("Connected")
peripheral.delegate = self
let serviceUUIDS:[CBUUID] = [CBUUID(string: self.TRANSFER_SERVICE_UUID)]
peripheral.discoverServices(nil)
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
if error != nil{
centralManager?.cancelPeripheralConnection(peripheral)
}
for service:CBService in peripheral.services as [CBService]!{
peripheral.discoverCharacteristics(nil, for: service)
}
}
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
if error != nil{
centralManager?.cancelPeripheralConnection(peripheral)
}
for characteristic:CBCharacteristic in service.characteristics as [CBCharacteristic]!{
if characteristic.uuid.isEqual(CBUUID(string:self.TRANSFER_CHARACTERISTIC_UUID)){
peripheral.setNotifyValue(true, for: characteristic)
}
}
}
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
let stringFromData:String = String(data: characteristic.value!, encoding: String.Encoding.utf8)!
//if
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
这样做完全相反吗? 我想确保我正朝着正确的方向看这个。
更新
在其他应用程序中,我试图像这样开始这个过程:
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
if peripheralManager?.state != .poweredOn {
return
} else {
let serviceUUId:CBUUID = CBUUID(string:self.TRANSFER_SERVICE_UUID)
let mutable:CBMutableService = CBMutableService(type: serviceUUId, primary: true)
peripheralManager?.add(mutable)
}
}
下一步是开始做广告吗?