我正在编写一个应用程序,该应用程序允许我通过BLE将图像发送到其他人的应用程序。

为了正确测试它,我需要能够连接到设备,但是我扫描的外围设备始终断开连接,并且值为零。我不确定我是否在这样做。我一直在阅读指南,并且遵循正确的程序,所以有人可以指出我可能做错了什么吗?我正在检测哪些外围设备?

输出:

<CBPeripheral: 0x165b9c90, identifier = 6B74A074-6F5B-0E3A-94EB-6E7BB890569C, name = (null), state = disconnected>
<CBPeripheral: 0x165a0940, identifier = A35AC32E-5668-BD0D-4DBC-D4BF959B9242, name = (null), state = disconnected>
<CBPeripheral: 0x166a8220, identifier = 4D9FA1A1-0090-465F-B53D-363B0F3BBD27, name = (null), state = disconnected>


编辑:添加了更多代码

这是我的代码

import Foundation
import CoreBluetooth
import UIKit

class BluetoothController: UITableViewController, CBCentralManagerDelegate, CBPeripheralManagerDelegate,  CBPeripheralDelegate {

    var centralManager: CBCentralManager!
    var peripheralManager: CBPeripheralManager!
    var service : CBMutableService!

    let uuid:CBUUID = CBUUID(string: "09d921ff-b80c-47b1-bc2b-5bbaadf62010")
    let charaUuid:CBUUID = CBUUID(string: "09d921ff-b80c-47b1-bc2b-5bbaadf62021")

    override func viewDidLoad() {
        print("Initialzing managers")
        peripheralManager = CBPeripheralManager(delegate: self, queue: nil)
        centralManager = CBCentralManager(delegate: self, queue: nil)
        service = CBMutableService(type: uuid, primary: true) //<--Probably not causing it but without a service...

        let properties: CBCharacteristicProperties = [.notify, .read, .write]
        let permissions: CBAttributePermissions = [.readable, .writeable]
        let characteristic = CBMutableCharacteristic(
            type: charaUuid,
            properties: properties,
            value: nil,
            permissions: permissions)

        service.characteristics = [characteristic];
        peripheralManager.add(service)
    }

    func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
        print("peripheral state updated")
        print("\(peripheral.description)")
    }

    func peripheralManagerDidStartAdvertising(_ peripheral: CBPeripheralManager, error: Error?)
    {
        print("started advertising")
    }

    // Check if the bluetooth is powered on
    // Start looking for devices
    func centralManagerDidUpdateState(_ central: CBCentralManager) {
        if central.state == .poweredOn {
            print("Scanning for devices")
            centralManager.scanForPeripherals(withServices: nil, options: nil)
            startAdvert()
        }  else {
            print("Bluetooth not available.")
        }
    }

    func startAdvert(){
        let advertisingData = [CBAdvertisementDataLocalNameKey:"Test Device", CBAdvertisementDataServiceUUIDsKey: uuid] as [String : Any]
        peripheralManager.startAdvertising(advertisingData)
    }

    func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber){

        print(peripheral)
        //didReadPeripheral(peripheral, rssi: RSSI)
    }

}

最佳答案

正如Paulw11所指出的,您没有连接,因为您从未调用connect()。您会发现周围的随机BLE设备与您的应用无关。那是因为您正在扫描所有可能的服务:

centralManager.scanForPeripherals(withServices: nil, options: nil)


您几乎永远都不要那样做。这对电池不利,而且很少是您想要的。您要扫描您的服务。

centralManager.scanForPeripherals(withServices: [uuid], options: nil)


然后,当您发现其他宣传您的服务的设备时,您需要连接到它们。

10-08 11:39