蓝牙使用 iOS小结

做了一个单例
h文件

#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
NS_ASSUME_NONNULL_BEGIN
typedef void(^DidRefreshPeripherals)(void);
typedef void(^CentralManagerDidUpdateState)(CBManagerState state);
typedef void(^DidConnectPeripheral)(void);
typedef void(^DidFailToConnectPeripheral)(void);
typedef void(^DidDisconnectPeripheral)(void);

typedef void(^DidDiscoverServicesErr)(NSError *err);
typedef void(^GetConnectionStatus)(NSString *connectionStatus);


typedef void(^DidReceiveMSG)(int value);
@interface BluetoothCenter : NSObject<CBCentralManagerDelegate,CBPeripheralDelegate>
@property(strong,nonatomic) CBCentralManager *centerManager;//--------中心设备管理器

@property(strong,nonatomic) NSMutableArray *peripherals;//------------所有蓝牙外设

@property(strong,nonatomic) NSDictionary *currentPeripheral;//----------当前连接的外设

@property(copy)CentralManagerDidUpdateState centralManagerDidUpdateState;

@property(copy)DidRefreshPeripherals didRefreshPeripherals;

@property(copy)DidConnectPeripheral didConnectPeripheral;
@property(copy)DidFailToConnectPeripheral didFailToConnectPeripheral;
@property(copy)DidDisconnectPeripheral didDisconnectPeripheral;

@property(copy)DidDiscoverServicesErr didDiscoverServicesErr;

@property(copy)DidReceiveMSG didReceiveMSG;

@property (nonatomic ,assign) BOOL switchStatus;

- (instancetype)init NS_UNAVAILABLE;
+ (instancetype)new NS_UNAVAILABLE;
- (id)copy NS_UNAVAILABLE; // 没有遵循协议可以不写
- (id)mutableCopy NS_UNAVAILABLE; // 没有遵循协议可以不写

+ (instancetype)sharedCenter;
//第四步:连接蓝牙设备
- (void)connectPeripheral:(CBPeripheral *)peripheral;
- (void)startScanPeripheral;
- (void)stopScanPeripheral;

@end

NS_ASSUME_NONNULL_END

m文件



#import "BluetoothCenter.h"

@implementation BluetoothCenter
// 跟上面的方法实现有一点不同
+ (instancetype)sharedCenter {
    static BluetoothCenter *_sharedSingleton = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
          // 要使用self来调用
        _sharedSingleton = [[self alloc] init];
        [_sharedSingleton initCBCentralManager];
    });
    return _sharedSingleton;
}
#pragma mark  =====  init  =====
/*第一步:创建设备管理器
 创建完之后,会回掉CBCentralManagerDelegate中的方法:- (void)centralManagerDidU
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值