我目前正在尝试使2个iOS设备彼此连接,以便我可以在这两个设备之间发送GPS位置。现在,我正在使用corebluetooth
实现这一目标。
到目前为止,我所做的是创建了2个独立的项目,一个是接收者,一个是发送者。我遵循苹果公司的一些指令来播放iPad。但是,试图搜索蓝牙设备的项目似乎找不到它。我在打开蓝牙的情况下在单独的物理设备上运行了两个应用程序。在我使用过的代码下面(请记住,这是我编写的一些快速代码,试图查看是否可以真正找到该设备。)这也是我第一次使用蓝牙,因此这肯定是一个学习曲线。
仅供参考:我正计划使用GATT protocol
。我也想知道是否可以在没有配对握手的情况下以弹出窗口接受连接的方式完成此操作。我希望接收器自动接收和广播信号,并开始获取正在发送的数据,而无需其他步骤。还是我需要Multipeer Connectivity Framework
之类的东西?
接收器
import UIKit
import CoreBluetooth
import CoreLocation
class ViewController: UIViewController {
private let uuidBeacon = "A26962FC-F9DD-4251-B339-A25B3E808213"
var peripheral: CBPeripheralManager!
var peripheralData: NSMutableDictionary!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
peripheral = CBPeripheralManager(delegate: self, queue: nil)
}
override func viewDidAppear(_ animated: Bool) {
guard let region = createBeaconRegion() else { return }
advertiseDevice(region: region)
}
private func createBeaconRegion() -> CLBeaconRegion? {
let proximityUUID = UUID(uuidString: uuidBeacon)
let major : CLBeaconMajorValue = 100
let minor : CLBeaconMinorValue = 1
let beaconID = "com.example.myDeviceRegion"
return CLBeaconRegion(proximityUUID: proximityUUID!,
major: major, minor: minor, identifier: beaconID)
}
func advertiseDevice(region : CLBeaconRegion) {
peripheralData = region.peripheralData(withMeasuredPower: nil)
}
}
extension ViewController: CBPeripheralManagerDelegate {
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
switch peripheral.state {
case .poweredOn:
peripheral.startAdvertising(((peripheralData as NSDictionary) as! [String : Any]))
case .poweredOff:
break
// peripheral.stopAdvertising()
default:
break
}
}
}
广播公司
import UIKit
import CoreBluetooth
import CoreLocation
class ViewController: UIViewController {
var centralManager: CBCentralManager!
var sensorTag: CBPeripheral?
var keepScanning: Bool = false
private var timerScanInterval: TimeInterval = 5
override func viewDidLoad() {
super.viewDidLoad()
centralManager = CBCentralManager(delegate: self, queue: nil)
}
}
extension ViewController: CBCentralManagerDelegate {
func centralManagerDidUpdateState(_ central: CBCentralManager) {
switch central.state {
case .poweredOn:
keepScanning = true
_ = Timer(timeInterval: timerScanInterval, target: self, selector: #selector(pauseScan), userInfo: nil, repeats: false)
centralManager.scanForPeripherals(withServices: nil, options: nil)
break
case .poweredOff:
break
case .unsupported:
break
case .unauthorized:
break
case .resetting:
break
case .unknown:
break
}
}
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
peripheral.discoverServices(nil)
if let peripheralName = advertisementData[CBAdvertisementDataLocalNameKey] as? String {
print(peripheralName)
}
}
@objc private func pauseScan() {
centralManager.stopScan()
_ = Timer(timeInterval: 2, target: self, selector: #selector(resumeScan), userInfo: nil, repeats: false)
}
@objc private func resumeScan() {
if keepScanning {
centralManager.scanForPeripherals(withServices: nil, options: nil)
_ = Timer(timeInterval: timerScanInterval, target: self, selector: #selector(pauseScan), userInfo: nil, repeats: false)
}
}
}
extension ViewController: CBPeripheralDelegate {
}