iOS 蓝牙的封装和使用方法

代码下载地址https://github.com/18840851520/MyShow

调用方法:

创建实例对象:

     self.blueTools = [[ZBlueToothTools alloc] init];

    self.blueTools.delegate = self;    //连接mac地址为 78a50457f38b|| 78:a5:04:57:f3:8b 蓝牙

通过蓝牙返回的mac地址连接(这是需要蓝牙设备支持的)连接成功代理回调成功状态,就可以发送指令

    [self.blueTools connectBlueToothWithMACAdress:@"78a50457f38b"];

代理方法:

//蓝牙状态

typedefenum: NSUInteger {

    BlueToothStateUnknown,          //0.无法识别蓝牙

    BlueToothStateResetting,        //1.蓝牙重置

    BlueToothStateUnsupported,      //2.不支持蓝牙

    BlueToothStateUnauthorized,    //3.未授权

    BlueToothStatePoweredOff,      //4.蓝牙未开启

    BlueToothStatePoweredOn,        //5.蓝牙已开启

    BlueToothStateConnectSuccess,  //6.蓝牙连接成功

    BlueToothStateNotify,          //7.订阅消息    蓝牙特征值 characteristicInstance

    BlueToothStateConnectFail,      //7.蓝牙连接失败

    BlueToothStateDisConnect,      //8.蓝牙连接断开

} BlueToothState;

/*

 当前蓝牙的连接状态

 * @param      state 蓝牙状态

 */

- (void)blueToothConnectDeviceStated:(BlueToothState)state{

    if(state == BlueToothStateNotify){

        //获取蓝牙特征值后发送指令

        [self.blueTools sendValue:@"a1b10203ec000402010100010106" AndBlock:nil];

    }

}

/*

蓝牙写入状态

 * @param      state 读写状态

 */

- (void)blueToothWriteStatus:(BOOL)isSuccess{

//指令发送是否成功

    NSLog(@"writeIsSuccess=%d",isSuccess);

}

/*

 蓝牙数据接收

 * @param data二进制数据 error 错误

 */

- (void)blueToothUpdateForCharacteristicValue:(NSData *)data withError:(NSError *)error;

ZBlueToothTools.m的方法

/*

 * @brief 连接蓝牙

 */

- (void)connectBlueToothWithMACAdress:(NSString*)macAdress;

/*

 * @brief 重连蓝牙

 */

- (void)reConnectBlueTooth;

/*

 * @brief 关闭蓝牙

 */

- (void)closeConnectBlueTooth;

/*

 * @brief 发送指令

 * @param value 指令

 * @param characteristic

 */

- (void)sendValue:(NSString*)value AndBlock:(BlueToothWriteBlock)writeBlock;


**如果解决了你的问题,点个赞呗!**

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本文主要以蓝牙4.0做介绍,因为现在iOS能用的蓝牙也就是只仅仅4.0的设备 用的库就是core bluetoot...
    暮雨飞烟阅读 866评论 0 2
  • 最近做一项目,本来蓝牙通讯这块不是我负责的,但是负责这块的同事要走,只好咬咬牙学习了。呜呜呜。。。。。 现将创建蓝...
    傻傻小萝卜阅读 2,415评论 1 4
  • iOS BLE开发调用的是CoreBluetooth系统原生库,基本用到的类有: CBCentralManager...
    BrumeLoong阅读 2,176评论 0 0
  • 本文介绍了蓝牙的概念以及具体的使用步骤. 一.蓝牙概念 蓝牙2.0为传统蓝牙,传统蓝牙也称为经典蓝牙.蓝牙4.0因...
    o惜乐o阅读 18,178评论 41 59
  • 前几个月做了一个蓝牙项目,后面一直没有时间总结,这几天心血来潮,想来做个总结,一来为了以后自己用时直接拿来用,二来...
    android的那点事阅读 2,231评论 0 3