macOS-BLE蓝牙4.0开发
!!!中心模式 !!!
macOS
的BLE程序代码和iOS
差不多,只需要修改一些UI组件就可以把iOS
的代码放在macOS
上使用,下面列举移除不同之处。
1 蓝牙状态一直CBManagerStateUnsupported的问题
在Xcode
中打开targets中的沙盒蓝牙设置,具体路径: TARGETS - Signing & Capanilities - App Sandbox - Handware - Bluetooth
info.plist 里面添加NSBluetoothPeripheralUsageDescription
Value 根据具体业务详细说明使用蓝牙的目的,以提示用户开启蓝牙权限,避免审核被拒。
2 代码实现
2.1 导入蓝牙库
#import <CoreBluetooth/CoreBluetooth.h>
2.2 添加代理头
<
CBCentralManagerDelegate,
CBPeripheralDelegate
>
2.3 实例化对象
@property (strong, nonatomic) CBCentralManager *manager; // 中心管理器
@property (strong, nonatomic) CBPeripheral *peripheral; // 发现的设备