一 concept :
1.Bluetooth Communication
central and peripheral devices
client server
central peripheral
2. peripheral advertising and a central scaning and listen
3.Peripherals contain many services , a service made up of characteristics or included services
4. local centrals and Remote Peripherals
CBCentralManager
A Local Peripheral’s Data Are Represented by CBMutableService and CBMutableCharacteristic Objects
二 怎么样 实现大多数蓝牙技术任务类型(来自central side)
- Start up a central manager object
- Discover and connect to peripheral devices that are advertising
- Explore the data on a peripheral device after you’ve connected to it
- Send read and write requests to a characteristic value of a peripheral’s service
- Subscribe to a characteristic’s value to be notified when it is updated
Starting Up a Central Manager
myCentralManager =[ [CBcentralManager alloc] initWithDelegate: self queue:nil option:nil];实现方法 centralManagerDidUpdateState: 检测是否支持蓝牙
Discovering Peripheral Devices That Are Advertising
[myCentralManager scanForPeripheralsWithServices:nil options:nil];
如果发现了周围service回调方法:
centralManager:didDiscoverPeripheral:advertisementData:RSSI:
[myCentralManager stopScan];//停止扫
Connecting to a Peripheral Device After You’ve Discovered It
[myCentralManager connectPeripheral:peripheral options:nil];
链接成功会回调
centralManager:didConnectPeripheral:
建立链接之后
Discovering the Services of a Peripheral That You’re Connected To
[peripheral discoverServices:nil];
发现之后也会有相应的回调,然后去
Discovering the Characteristics of a Service
当发现特征之后,也会有相应的回调:
peripheral:didDiscoverCharacteristicsForService:error:
然后读获取到的特征的值:readValueForCharacteristic:
然后也会有回调:
peripheral:didUpdateValueForCharacteristic:error:
Subscribing to a Characteristic’s Value
[peripheral setNotifyValue:YES forCharacteristic:interestingCharacteristic];
只要有操作,基本就有相应的回调。