iOS蓝牙框架封装

  app需要适配一款蓝牙手环,做了简单的调查,觉得CoreBluetooth.framework里边的方法用起来并不是很顺手,于是就自己稍微做了一下封装。说白了就是添加一些代理还有把蓝牙发送的数据加工成对象等。

  下边,就进行一些简单的介绍:

  首先,就是定义了两个代理

ALBleConnectDelegate​​

- (void)didConnectedWithBleDevice:(ALDeviceInfo *)deviceInfo;​​    //蓝牙设备连接成功以后调用该代理方法。​​

ALBleDataReceiveDelegate​

- (void)receivedData:(ALDataInfo *)data;    // 接收到数据

- (void)receivedRealtimeData:(ALRealtimeDataInfo *)data;    // 接收到实时数据

  主要的操作集中在ALBLEDeviceManager​中

// 获取实例对象

+ (instancetype)defaultLsBleManager;

// 蓝牙状态检测

-(void)checkBluetoothStatus:(void(^)(BOOL isOpenFlags))checkCompletion;

// 根据指定条件搜索设备

-(void)searchLsBleDevice:(BLEDeviceType)deviceType withTimer:(NSInteger)seconds searchCompletion:(SearchCompletion)completion;

// 连接手环

- (BOOL)connectWithBleDevice:(ALDeviceInfo *)alDeviceInfo;

// 断开手环

- (void)disConnectWithBleDevice:(ALDeviceInfo *)alDeviceInfo;

// 获取已经连接的手环

- (ALDeviceInfo *)getConnectedDeviceWithName:(NSString *)deviceName;

// 开始接收数据

- (void)startReceiveData:(ALDeviceInfo *)alDeviceInfo;

// 开始接收实时数据

- (void)startReceiveRealtimeData:(ALDeviceInfo *)alDeviceInfo;

// 停止接收数据

- (void)stopReceiveData;

// 停止接收实时数据

- (void)stopReceiveRealtimeData:(ALDeviceInfo *)alDeviceInfo;

  其实,分享这份代码,只是想分享一个思路,希望对大家有帮助。

  代码地址:https://github.com/taowang501/ALBLEFramework.git

  以上。

posted @ 2015-12-14 11:17  i左撇子  阅读(410)  评论(0编辑  收藏  举报