文章目录
- 一、 说明
- 二、设置蓝牙外设
- 1. 初始化外设中心
- 2. 创建外设服务
- 3. 获取外设蓝牙状态
- 4. 发送蓝牙数据
- 三、外设代理
- 1. 状态变更回调
- 2. 中心设备读取数据的时候回调
- 3. 中心设备写入数据的时候回调
- 4. 订阅提示
- 四、发送广播数据
- 1. 发送不同广播
- 2. beacon广播
- 3. 停止广播
一、 说明
任意苹果设备都可以作为蓝牙外设进行设置。
苹果蓝牙后台的限制,原本广播会有两个段分别是localName和serviceUUID这两块,但现在后台广播时,是不发送在这两段的。
二、设置蓝牙外设
1. 初始化外设中心
let peripheralManager = CBPeripheralManager.init(delegate: self, queue: DispatchQueue.main)
2. 创建外设服务
public func setupServiceAndCharacteristics() {// 创建服务let serviceID = CBUUID(string: kServiceUUID)let service = CBMutableService(type: serviceID, primary: true)// 创建服务中的特征let characteristicID = CBUUID(string: kChARACTERISTICUUID)let characteristic = CBMutableCharacteristic(type: characteristicID, properties: [.read, .write, .notify], value: nil, permissions: [.readable, .writeable])// 特征添加进服务service.characteristics = [characteristic]// 服务加入管理self.peripheralManager?.add(service)}
3. 获取外设蓝牙状态
let tempState = self.peripheralManager?.state ?? .unknown
4. 发送蓝牙数据
let sendSuccess: Bool = self.peripheralManager.updateValue(self.textField.text.data(using: NSUTF8StringEncoding), forCharacteristic: self.characteristic, onSubscribedCentrals: nil)
三、外设代理
CBPeripheralManagerDelegate
1. 状态变更回调
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {print(peripheral.state)delegate?.peripheralUpdateState(peripheral)}
2. 中心设备读取数据的时候回调
func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveRead request: CBATTRequest) {// 请求中的数据,这里把文本框中的数据发给中心设备
// request.value = self.textField.text.data(using: NSUTF8StringEncoding)// 成功响应请求
// peripheral.respondToRequest(request, withResult: CBATTErrorSuccess)}
3. 中心设备写入数据的时候回调
func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveWrite requests: [CBATTRequest]) {// 写入数据的请求let request = requests.lastlet data = request?.valuedelegate?.peripheralDidReceiveWrite(data)}
4. 订阅提示
func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didSubscribeTo characteristic: CBCharacteristic) {print(#function)}// 取消订阅回调func peripheralManager(_ peripheral: CBPeripheralManager, central: CBCentral, didUnsubscribeFrom characteristic: CBCharacteristic) {print(#function)}
}
四、发送广播数据
1. 发送不同广播
self.peripheralManager?.startAdvertising(periData)
2. beacon广播
注意⚠️:测试时beacon广播CLBeacon未检测到iPhone设备。 即使使用 Turning an iOS device into an iBeacon device 也没有结果(如果帮助不胜感激)
/// 开始发送广播public func startBroadcasting(major: UInt16 = 0, minor: UInt16 = 0, peripheralData: [String: Any]? = nil) {// 为beacon基站创建一个唯一标示guard let myUUID = UUID(uuidString: kBeaconUUIDString) else {return}let constraint = CLBeaconIdentityConstraint(uuid: myUUID, major: major, minor: minor)let myBeaconRegion = CLBeaconRegion.init(beaconIdentityConstraint: constraint, identifier: kBeaconidentifier)// 获取该Beacon区域的信号信息var periData = myBeaconRegion.peripheralData(withMeasuredPower: nil) as? [String: Any]peripheralData?.forEach { (key: String, value: Any) inperiData?[key] = value}// 创建并广播Beacon信号self.peripheralManager?.startAdvertising(periData)}
3. 停止广播
self.peripheralManager?.stopAdvertising()