第一个蓝牙demo

作者: 纵横四海 | 来源:发表于2016-03-21 15:39 被阅读391次

1.用xcode 创建一个新的project ,语言选择oc

2.在ViewController  文件中#import"CoreBluetooth/CoreBluetooth.h"头文件

并添加委托方法<CBCentralManagerDelegate,CBPeripheralDelegate>

 @property(strong,nonatomic) CBCentralManager *MYCentralManager;

3.在ViewController.m的viewDidLoad方法中加入   

self.MYCentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];

4.在ViewController.m的viewDidLoad方法体中加入两个方法

-(void)centralManagerDidUpdateState:(CBCentralManager *)central{

    switch (central.state)

{       

    case CBCentralManagerStateUnknown:                       

       break;       

   case CBCentralManagerStateUnauthorized:           

        NSLog(@"模拟器不支持蓝牙测试");           

      break;       

    case CBCentralManagerStateUnsupported:                       

      break;       

    case CBCentralManagerStatePoweredOff:           

      NSLog(@"蓝牙处于关闭状态");           

      break;       

      case CBCentralManagerStatePoweredOn:           

      NSLog(@"蓝牙已开启");           

     [self.MYCentralManager scanForPeripheralsWithServices:nil options:nil];                     default:         

  break;   

}

}

-(void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary*)advertisementData RSSI:(NSNumber *)RSSI

{

NSLog(@"能发现设备:%@",peripheral.name);

}

5.真机测试 

打印结果:

注:蓝牙必须要用真机测试,模拟器是不行的

相关文章

  • 蓝牙开发

    iOS蓝牙开发 Bluetooth蓝牙CoreBluetooth 蓝牙中心设备的实现 蓝牙外设的实现 有Demo ...

  • iOS蓝牙开发 Bluetooth蓝牙CoreBluetooth

    iOS蓝牙开发 Bluetooth蓝牙CoreBluetooth 蓝牙中心设备的实现 蓝牙外设的实现 有Demo ...

  • 第一个蓝牙demo

    1.用xcode 创建一个新的project ,语言选择oc 2.在ViewController 文件中#impo...

  • 蓝牙Demo

    githubhttp://www.jianshu.com/p/38a4c6451d93 cocoachina ht...

  • ios蓝牙参考

    参考文章:iOS中的蓝牙开发iOS-BLE蓝牙开发demo 官网 转载 CenteralManager学习笔记

  • Android经典蓝牙通讯传输DEMO

    BlueUtils 项目地址:GitHub 经典蓝牙搜索,连接,数据传输小DEMO 通过经典模式 搜索 蓝牙应用。...

  • iOS蓝牙通信CoreBluetooth框架学习

    最近学习蓝牙,所以找了一些资料学习研究了一下蓝牙通讯的一个流程。写了一个小demo,demo效果见下图: 需要de...

  • 蓝牙

    1、Android 蓝牙官方demo:https://github.com/googlesamples/andro...

  • iOS 蓝牙开发

    不想写太多字、所以就直接粘贴代码了1、实现蓝牙单连接2、实现蓝牙重连3、实现蓝牙简单发送指令 这个Demo是16年...

  • iOS无法发现蓝牙设备

    问题描述 今天在创建iOS 蓝牙demo工程,iPhone作为无法发现周边广播的蓝牙设备 解决方案 将scanFo...

网友评论

本文标题:第一个蓝牙demo

本文链接:https://www.haomeiwen.com/subject/fptlkttx.html