CoreBluetooth之数据的读取与发送

此文要理清的的问题是:使用CoreBluetooth框架发送与读取数据。数据传输于  CBCentralManager CBPeripheral 之间。 首先明确iDevice是客户端,即 CBCentralManager端,是中心。CBPeripheral 为服务器端,是外围设备。然后按照逻辑一步步理下去:1.首先工程倒入CoreBluetooth框架: #import,这就是蓝牙开发
摘要由CSDN通过智能技术生成

此文要理清的的问题是:使用CoreBluetooth框架发送与读取数据。数据传输于  CBCentralManager <~~~> CBPeripheral 之间。 首先明确iDevice是客户端,即 CBCentralManager端,是中心。CBPeripheral 为服务器端,是外围设备。然后按照逻辑一步步理下去:

1.首先工程倒入CoreBluetooth框架: #import<CoreBluetooth/CoreBluetooth.h>,这就是蓝牙开发的第一步。

2.在声明文件中设置CBCentralManager的代理,导入协议,以便后续使用CBCentralManager的行为方法。

   @interface ViewController :UIViewController<CBCentralManagerDelegate>

3.CBCentralManager设置为属性:@property(strong,nonatomic)CBCentralManager *CM;

4.初始化iDevice的蓝牙管理中心:self.CM = [[CBCentralManageralloc]initWithDelegate:selfqueue:nil];其中的初始化方法提供了两个信息1)让本类做为代理,2)queue的参数        表示的是关于线程的,如果CBCentralManager如果在主线程上跑的话,我们就可以设为nil,如果需要处理更复杂的事情,我们可以在这里创建不同的线程,才会用到这个队列。

5.一但CBCentralManager初始化成功,就需要立马检查设备的蓝牙的状态,调用这个代理方法:-(void)centralManagerDidUpdateState:(CBCentralManager *)central;蓝牙一共有六种状态:

(1)CBCentralManagerStatePoweredOff    Bluetooth is currently powered off. 蓝牙电源处于关闭状态。

(2)CBCentralManagerStatePoweredOn   Bluetooth is currently powered on and available to use.蓝牙电源处于开启状态,可以使用。

(3)CBCentralManagerStateResetting  The connection with the system service was momentarily lost, update imminent. 连上系统服务突然断开,紧急更新中

(4)CBCentralManagerStateUnauthorized    The application is not authorized to use the Bluetooth Low Energy Central/Client role. 应用无权限使用低功耗角色

(5)CBCentralManagerStateUnknown  State unknown, update imminent.状态未知,紧急更新中

(6)CBCentralManagerStateUnsupported   The platform doesn't support the Bluetooth Low Energy Central/Client role.设备不支持蓝牙低功耗角色

6.为了接下来进行搜索,我们要把指定搜索的服务和特征的UUID定义出来,也可以全部搜索,不用定义。下面是我的宏定义:

#defineSERVICE_UUID    0xFFF0

#defineCHAR_UUID       0xFFF1

#defineNotify_UUID    0xfff4

也可以直接定义最初的uuidgen,这是别人别人定义的,如:


static

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值