1. Create a hub device and set up a proxy
一般情况下,手机是中心设备,蓝牙设备是外围设备。
Self.centralmanager = [[Cbcentralmanager alloc] initwithdelegate:self Queue:nil];
1.创建之后,会进入到改变蓝牙状态的代理方法中。
- (void) Centralmanagerdidupdatestate: (Cbcentralmanager *) Central;
```
In this method, you can determine the status of Bluetooth, the corresponding operation.
CBCentralManagerStateUnknown = CBManagerStateUnknown,CBCentralManagerStateResetting = CBManagerStateResetting,CBCentralManagerStateUnsupported = CBManagerStateUnsupported,CBCentralManagerStateUnauthorized = CBManagerStateUnauthorized,CBCentralManagerStatePoweredOff = CBManagerStatePoweredOff,CBCentralManagerStatePoweredOn = CBManagerStatePoweredOn,
2. When the Bluetooth status is on, start scanning peripheral devices. If Bluetooth support background mode, then to specify the service UUID, or in the background after disconnecting, not connected to Bluetooth, do not support the background, it is set to nil. Option:cbcentralmanagerscanoptionallowduplicateskey If yes, the central device keeps listening for messages from external devices, No. If yes, it may be very power-hungry, as required.
if (central.state == CBCentralManagerStatePoweredOn) { [self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:kServiceUUID]] options:nil]; }
3. When scanning to peripheral devices, the following methods will be entered.
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<<span style="color: #00b1ff">NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI;
In this method, we can filter the peripheral devices we need to connect according to different conditions, and my side is filtered by name. After matching the device you need, start connecting to the device.
if ([peripheral.name isEqualToString:kPeripheralName]) { peripheral.delegate = self; self.selectedPeripheral = peripheral; // 开始连接设备 [self.centralManager connectPeripheral:self.selectedPeripheral options:nil]; }
4. Connection success and failure will go to the proxy method.
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error ;- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral;
Pause here,
If you are near a need to connect Bluetooth, then directly determine the name to connect, but if like a Bluetooth lock, there may be a lot of Bluetooth near the name, then how do you know which Bluetooth you want to connect? Then, at this time, it will be based on the Bluetooth MAC address to differentiate. Since IOS7, Apple will not be able to obtain the MAC address of the device, so now Bluetooth most of the Bluetooth MAC address is written in the Bluetooth device system Sevrice inside, the general UUID is 180A (device information), and the characteristics of the UUID is 2a23 ( System ID). Connect based on the MAC address of the Bluetooth you need to connect to.
Okay, just go ahead and scan the corresponding features.
for (CBService *service in peripheral.services) { if ([service.UUID isEqual:[CBUUID UUIDWithString:@"180A"]]) { [self.selectedPeripheral discoverCharacteristics:nil forService:service]; } }
5. After scanning to the feature, enter the proxy method
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error;```在这里就可以进行读写的操作了。读数据有两种方式,一种是read,一种是notify。我们可以用LightBlue来看到这个特征是否是支持notify,一般固定不变的数据都是用read,会有改变的数据用notify。
For (cbcharacteristic *characteristic in service.characteristics) {
if ([characteristic. UUID isequal:[cbuuid uuidwithstring:@ "2a23"]) {
Here is the read MAC address, data fixed, with readvalueforcharacteristic, without Setnotifyvalue:setnotifyvalue
[Self.selectedperipheral readvalueforcharacteristic:characteristic];
}
}
6.读取到数据的时候,进入代理方法。
- (void) Peripheral: (cbperipheral ) Peripheral didupdatevalueforcharacteristic: (cbcharacteristic ) Characteristic error: (Nserror *) error;
```
It is good to parse the data here according to the data rules set by the company.
My side of the MAC address parsing out, when the example to see it.
NSString *orstr = characteristic.value.description;if ([characteristic. UUID isequal:[cbuuid uuidwithstring:@ "2a23"]) {nsmutablestring *macstring = [[Nsmutablestring alloc] init]; [Macstring appendstring:[[orstr Substringwithrange:nsmakerange (2)] uppercasestring]; [Macstring appendstring:@ ":"]; [Macstring appendstring:[[orstr Substringwithrange:nsmakerange (2)] uppercasestring]; [Macstring appendstring:@ ":"]; [Macstring appendstring:[[orstr Substringwithrange:nsmakerange (2)] uppercasestring]; [Macstring appendstring:@ ":"]; [Macstring appendstring:[[orstr Substringwithrange:nsmakerange (5, 2)] uppercasestring]; [Macstring appendstring:@ ":"]; [Macstring appendstring:[[orstr Substringwithrange:nsmakerange (3, 2)] uppercasestring]; [Macstring appendstring:@ ":"]; [Macstring appendstring:[[orstr Substringwithrange:nsmakerange (1, 2)] uppercasestring]];}
7. Writing data
iOS Learning note Bluetooth (about Bluetooth device MAC address processing) 2