第3方类:https://github.com/coolnameismy/BabyBluetooth
蓝牙打印流程:http://blog.csdn.net/songrotek/article/details/46443965
Apple官方:https://developer.apple.com/bluetooth/
从名字上大家可以很清楚的知道,这个类是用来管理BLE的。我们也就是通过这个类来实现连接。
先创建一个:
<p class="p1"><span class="s1">CBCentralManager *centralManager;</span></p><p class="p1"><span class="s2">dispatch_queue_t</span><span class="s1"> centralQueue = </span><span class="s3">dispatch_queue_create</span><span class="s1">(</span><span class="s4">"com.manmanlai"</span><span class="s1">, </span><span class="s5">DISPATCH_QUEUE_SERIAL</span><span class="s1">);</span></p><p class="p1"><span class="s6">self</span><span class="s1">.centralManager = [[CBCentralManager alloc] initWithDelegate:</span><span class="s6">self</span><span class="s1"> queue:centralQueue];</span></p>
然后关键在于CBCentralManagerDelegate的使用。这个之后再讲。
<p class="p1"><span class="s1">[</span><span class="s2">self</span><span class="s1">.centralManager scanForPeripheralsWithServices:</span><span class="s3">@[]</span><span class="s1"> options:</span><span class="s2">nil</span><span class="s1">];</span></p>
这里的Service就是对应的UUID,如果为空,这scan所有service。
3、连接CBPeripheral
在上一步中,如果找到了设备,则CBCentralManager的delegate会调用下面的方法:
<p class="p1"><span class="s1">- (</span><span class="s2">void</span><span class="s1">)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(</span><span class="s3">NSDictionary</span><span class="s1"> *)advertisementData RSSI:(</span><span class="s3">NSNumber</span><span class="s1"> *)RSSI {</span></p><p class="p1"><span class="s1"> </span><span class="s4">NSLog</span><span class="s1">(</span><span class="s5">@"name:%@"</span><span class="s1">,peripheral);</span></p><p class="p1"><span class="s1"> </span><span class="s2">if</span><span class="s1">(!peripheral || !peripheral.name || ([peripheral.name isEqualToString:</span><span class="s5">@""</span><span class="s1">])) {</span></p><p class="p1"><span class="s1"> </span><span class="s2">return</span><span class="s1">; }</span></p><p class="p1"><span class="s1"> </span><span class="s2">if</span><span class="s1">(!</span><span class="s2">self</span><span class="s1">.peripheral || (</span><span class="s2">self</span><span class="s1">.peripheral.state == CBPeripheralStateDisconnected)) {</span></p><p class="p1"><span class="s1"> </span><span class="s2">self</span><span class="s1">.peripheral = peripheral;</span></p><p class="p1"><span class="s1"> </span><span class="s2">self</span><span class="s1">.peripheral.delegate = </span><span class="s2">self</span><span class="s1">;</span></p><p class="p2"><span class="s6"> NSLog(</span><span class="s1">@"connect peripheral"</span><span class="s6">);</span></p><p class="p1"><span class="s1"> [</span><span class="s2">self</span><span class="s1">.centralManager connectPeripheral:peripheral options:</span><span class="s2">nil</span><span class="s1">];</span></p><p class="p1"><span class="s1"> }</span></p><p class="p1"><span class="s1">}</span></p>
我们在这里创建了一个CBPeripheral的对象,然后直接连接
CBPeripheral的对象也需要设置delegate
如果Peripheral连接成功的话,就会调用delegate的方法:
<p class="p1"><span class="s1">- (</span><span class="s2">void</span><span class="s1">)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {</span></p><p class="p1"><span class="s1"> </span><span class="s2">if</span><span class="s1"> (!peripheral) {</span></p><p class="p1"><span class="s1"> </span><span class="s2">return</span><span class="s1">;</span></p><p class="p1"><span class="s1"> }</span></p><p class="p1"><span class="s1"> [</span><span class="s2">self</span><span class="s1">.centralManager stopScan];</span></p><p class="p2"><span class="s3"> </span><span class="s4">NSLog</span><span class="s3">(</span><span class="s1">@"peripheral did connect"</span><span class="s3">);</span></p><p class="p1"><span class="s1"> [</span><span class="s2">self</span><span class="s1">.peripheral discoverServices:</span><span class="s2">nil</span><span class="s1">];</span></p><p class="p1"><span class="s1">}</span></p>
我们这里先停止Scan,然后让Peripheral外设寻找其Service。
5、寻找Characteristic
找到Service后会调用下面的方法:
<p class="p1"><span class="s1">- (</span><span class="s2">void</span><span class="s1">)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(</span><span class="s3">NSError</span><span class="s1"> *)error {</span></p><p class="p1"><span class="s1"> </span><span class="s3">NSArray</span><span class="s1"> *services = </span><span class="s2">nil</span><span class="s1">;</span></p><p class="p1"><span class="s1"> </span><span class="s2">if</span><span class="s1">(peripheral != </span><span class="s2">self</span><span class="s1">.peripheral) {</span></p><p class="p2"><span class="s4"> NSLog(</span><span class="s1">@"Wrong Peripheral.\n"</span><span class="s4">);</span></p><p class="p1"><span class="s1"> </span><span class="s2">return</span><span class="s1">;</span></p><p class="p1"><span class="s1"> }</span></p><p class="p1"><span class="s1"> </span><span class="s2">if</span><span class="s1">(error != </span><span class="s2">nil</span><span class="s1">) {</span></p><p class="p1"><span class="s1"> </span><span class="s5">NSLog</span><span class="s1">(</span><span class="s6">@"Error %@\n"</span><span class="s1">, error);</span></p><p class="p1"><span class="s1"> </span><span class="s2">return</span><span class="s1"> ;</span></p><p class="p1"><span class="s1"> }</span></p><p class="p1"><span class="s1"> services = [peripheral services];</span></p><p class="p1"><span class="s1"> </span><span class="s2">if</span><span class="s1">(!services || ![services </span><span class="s5">count</span><span class="s1">]) {</span></p><p class="p2"><span class="s4"> </span><span class="s5">NSLog</span><span class="s4">(</span><span class="s1">@"No Services"</span><span class="s4">);</span></p><p class="p1"><span class="s1"> </span><span class="s2">return</span><span class="s1"> ;</span></p><p class="p1"><span class="s1"> }</span></p><p class="p1"><span class="s1"> </span><span class="s2">for</span><span class="s1"> (CBService *service </span><span class="s2">in</span><span class="s1"> services) {</span></p><p class="p1"><span class="s1"> NSLog(</span><span class="s6">@"service:%@"</span><span class="s1">,service.UUID);</span></p><p class="p1"><span class="s1"> [peripheral discoverCharacteristics:</span><span class="s2">nil</span><span class="s1"> forService:service];</span></p><p class="p1"><span class="s1"> }</span></p><p class="p1"><span class="s1">}</span></p>
我们根据找到的service寻找其对应的Characteristic。
6、找到Characteristic后读取数据
找到Characteristic后会调用下面的delegate方法:
<p class="p1"><span class="s1">- (</span><span class="s2">void</span><span class="s1">)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(</span><span class="s3">NSError</span><span class="s1"> *)error {</span></p><p class="p1"><span class="s1"> </span><span class="s4">NSLog</span><span class="s1">(</span><span class="s5">@"characteristics:%@"</span><span class="s1">,[service characteristics]);</span></p><p class="p1"><span class="s1"> </span><span class="s3">NSArray</span><span class="s1"> *characteristics = [service characteristics];</span></p><p class="p1"><span class="s1"> </span><span class="s2">if</span><span class="s1"> (peripheral != </span><span class="s2">self</span><span class="s1">.peripheral) {</span></p><p class="p2"><span class="s6"> NSLog(</span><span class="s1">@"Wrong Peripheral.\n"</span><span class="s6">);</span></p><p class="p1"><span class="s1"> </span><span class="s2">return</span><span class="s1"> ;</span></p><p class="p1"><span class="s1"> }</span></p><p class="p1"><span class="s1"> </span><span class="s2">if</span><span class="s1"> (error != </span><span class="s2">nil</span><span class="s1">) {</span></p><p class="p1"><span class="s1"> </span><span class="s4">NSLog</span><span class="s1">(</span><span class="s5">@"Error %@\n"</span><span class="s1">, error);</span></p><p class="p1"><span class="s1"> </span><span class="s2">return</span><span class="s1"> ;</span></p><p class="p1"><span class="s1"> }</span></p><p class="p1"><span class="s1"> </span><span class="s2">self</span><span class="s1">.characteristic = [characteristics firstObject];</span></p><p class="p3"><span class="s6"> </span><span class="s1">//[self.peripheral readValueForCharacteristic:self.characteristic];</span></p><p class="p1"><span class="s1"> [</span><span class="s2">self</span><span class="s1">.peripheral setNotifyValue:</span><span class="s2">YES</span><span class="s1"> forCharacteristic:</span><span class="s2">self</span><span class="s1">.characteristic];</span></p><p class="p1"><span class="s1">}</span></p>
这里我们可以使用readValueForCharacteristic:来读取数据。如果数据是不断更新的,则可以使用setNotifyValue:forCharacteristic:来实现只要有新数据,就获取。
7、处理数据
读到数据后会调用delegate方法:
<p class="p1"><span class="s1">- (</span><span class="s2">void</span><span class="s1">)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(</span><span class="s3">NSError</span><span class="s1"> *)error {</span></p><p class="p1"><span class="s1"> </span><span class="s3">NSData</span><span class="s1"> *data = characteristic.value;</span></p><p class="p2"><span class="s4"> </span><span class="s1">// Parse data...</span></p><p class="p1"><span class="s1">}</span></p>
8、向设备写数据
这个很简单,只要使用:
<p class="p1"><span class="s1">[</span><span class="s2">self</span><span class="s1">.peripheral writeValue:data forCharacteristic:</span><span class="s2">self</span><span class="s1">.characteristic type:CBCharacteristicWriteWithResponse];</span></p>
data是NSData类型。