Ios 将中央连接至peipheral,以改变灯的颜色或打开/关闭灯
我想为IOS使用Ios 将中央连接至peipheral,以改变灯的颜色或打开/关闭灯,ios,iphone,bluetooth,Ios,Iphone,Bluetooth,我想为IOS使用BLE设备,该设备具有开/关灯和彩灯。我正在使用核心蓝牙框架连接蓝牙 现在我想打开/关闭或改变灯的颜色。如何从作为应用程序的中央设备到作为蓝牙设备的外围设备执行此操作,执行此功能所需的所有方法是什么?1)使用无服务id扫描(如果您在后台使用,则需要服务id) 2) 扫描 3) 您可以使用此委托方法获取设备 - (void) centralManager:(CBCentralManager *)central didDiscoverPeripheral:
BLE
设备,该设备具有开/关灯和彩灯。我正在使用核心蓝牙框架连接蓝牙
现在我想打开/关闭或改变灯的颜色。如何从作为应用程序的中央设备到作为蓝牙设备的外围设备执行此操作,执行此功能所需的所有方法是什么?1)使用无服务id扫描(如果您在后台使用,则需要服务id)
2) 扫描
3) 您可以使用此委托方法获取设备
- (void) centralManager:(CBCentralManager *)central
didDiscoverPeripheral:(CBPeripheral *)peripheral
advertisementData:(NSDictionary *)advertisementData
RSSI:(NSNumber *)RSSI {
// check advertisementData for service UUID
[self.centralManager connectPeripheral:peripheral options:nil];
}
4) 论成功连接
- (void) centralManager:(CBCentralManager *)central
didConnectPeripheral:(CBPeripheral *)peripheral {
[peripheral discoverServices:nil];
}
5) 关于服务发现
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
if (error) {
NSLog(@"Error discovering services: %@", [error localizedDescription]);
return;
}
// Discover the characteristic we want...
// Loop through the newly filled peripheral.services array, just in case there's more than one.
for (CBService *service in peripheral.services) {
[peripheral discoverCharacteristics:nil forService:service];
}
}
6) 关于检索字符集
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
// Deal with errors (if any)
if (error) {
NSLog(@"Error discovering characteristics: %@", [error localizedDescription]);
return;
}
// Again, we loop through the array, just in case.
for (CBCharacteristic *characteristic in service.characteristics) {
// And check if it's the right one
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"FFF1"]]) {
scannedcharacteristic = characteristic;
NSString *str = writeValue.length == 0 ? @"z" : writeValue;
DDLogVerbose(@"Writing value %@", str);
NSData *expectedData = [str dataUsingEncoding:NSUTF8StringEncoding];
[peripheral writeValue:expectedData forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
}
// [peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
}
该设备是由您创建的吗?1) BLE设备可以具有服务和特性。2) 当特征值更改时,您可以从应用程序访问这些服务,并在BLE设备上修改特征3)发光二极管否。我有用于更改灯光颜色的字符串数据。我不知道如何写入将更改灯光颜色的值。请在我运行代码应用程序时提供代码片段帮助模拟器,我如何使用BLE设备在模拟器中进行测试。我的代码如下。请创建一个更具体的问题。写下你具体在做什么,你在做什么,预期的结果是什么,你目前的结果是什么。同时添加所有相关信息,例如设备具有哪些服务和特性。至于您所写的内容,您似乎要求提供iOS实现的BLE教程。[mgr scanForPeripheralsWithServices:[NSArray arrayWithObject:[CBUUID UUIDWithString:@“180A”]]选项:无];我怎么知道我的BLE设备是180A?谢谢。1。我没有得到->[CBUUID UUIDWithString:@“FFF1”]??2.如果我想要这个NSData*expectedData=[str DATAUSINGENCODE:NSUTF8STRINGENCONDING];[peripheral writeValue:expectedData for characteristic:characteristic type:CBCharacteristicWriteWithResponse];点击按钮?您使用的是什么BLE设备?为eg()安装一个BLE services explorer应用程序,并检查服务列表。(FFF1只是一个示例,使用NSLog查找UUID)在我的应用程序中,使用3个按钮每个按钮都有特定的数据,如何使用writeValue发送数据:在每个按钮单击事件上?使用库列出属性和写入特征如何将数据从中央设备发送到外围设备?
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
// Deal with errors (if any)
if (error) {
NSLog(@"Error discovering characteristics: %@", [error localizedDescription]);
return;
}
// Again, we loop through the array, just in case.
for (CBCharacteristic *characteristic in service.characteristics) {
// And check if it's the right one
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"FFF1"]]) {
scannedcharacteristic = characteristic;
NSString *str = writeValue.length == 0 ? @"z" : writeValue;
DDLogVerbose(@"Writing value %@", str);
NSData *expectedData = [str dataUsingEncoding:NSUTF8StringEncoding];
[peripheral writeValue:expectedData forCharacteristic:characteristic type:CBCharacteristicWriteWithResponse];
}
// [peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
}