成长移动开发

iOS 蓝牙的封装和使用方法

2018-04-04  本文已影响78人  划落永恒

代码下载地址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);

}

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;

上一篇 下一篇

猜你喜欢

热点阅读