我创建了两个iOS应用程序;一个是广告服务的蓝牙LE外围设备,另一个是扫描广告服务的蓝牙LE中心。外围设备在我的iPhone5s上运行,中央设备在我的iPad Mini上运行。我最初将中心设置为扫描特定的广告服务,但后来将其更改为侦听任何服务。在任何一种情况下,作为中心的iPad Mini应用程序都不会检测任何广告服务。我不确定我设置外围管理器的方式是否有问题,或者我设置中央管理器扫描的方式有问题,还是设备配置问题。请提供我可以执行的建议或测试。
以下是作为外围设备的iPhone5s应用程序的相关代码:
CBPeripheralManager *peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:nil];
CBUUID *immediateAlertServiceUUID = [CBUUID UUIDWithString: IMMEDIATE_ALERT_SERVICE_UUID];
CBUUID *alertLevelCharacteristicUUID = [CBUUID UUIDWithString: ALERT_LEVEL_CHARACTERISTIC_UUID];
CBUUID *myCustomCharacteristicUUID = [CBUUID UUIDWithString: MY_CUSTOM_CHARACTERISTIC_UUID];
alertLevelCharacteristic =
[[CBMutableCharacteristic alloc] initWithType:alertLevelCharacteristicUUID
properties:CBCharacteristicPropertyRead
value: nil permissions:CBAttributePermissionsReadable];
myCustomCharacteristic =
[[CBMutableCharacteristic alloc] initWithType:myCustomCharacteristicUUID
properties:CBCharacteristicPropertyRead
value: nil permissions:CBAttributePermissionsReadable];
NSArray *myCharacteristics = @[alertLevelCharacteristic, myCustomCharacteristic];
// Now setup the service
myService = [[CBMutableService alloc] initWithType:immediateAlertServiceUUID primary:YES];
// Finally, associate the characteristic with the service. This is an array of characteristics
myService.characteristics = myCharacteristics;
[peripheralManager addService:myService];
... wait for user to push button to start advertising ...
// Start Advertising
[peripheralManager startAdvertising:@{ CBAdvertisementDataLocalNameKey : @"My Service",
CBAdvertisementDataServiceUUIDsKey : @[myService.UUID] }];
这是必要的委托方法。注意:委托方法peripheralManagerDidUpdateState触发并指示“CoreBluetooth BLE硬件已打开并准备就绪”(中央端也是如此)。委托方法peripheralManager:didAddService:错误触发而没有错误(参见下面的输出)。和委托方法peripheralManagerDidStartAdvertising:错误触发而没有错误)。这是从didAddService打印的服务信息:
<CBMutableService: 0x17008efb0 Primary = YES, UUID = 1802, Included Services = (null), Characteristics = (
"<CBMutableCharacteristic: 0x1702c1500 UUID = 2A06, Value = (null), Properties = 0x2, Permissions = 0x1, Descriptors = (null), SubscribedCentrals = (\n)>",
"<CBMutableCharacteristic: 0x1702c15e0 UUID = 66E613B5-7225-42C6-A9C2-11FADAE62899, Value = (null), Properties = 0x2, Permissions = 0x1, Descriptors = (null), SubscribedCentrals = (\n)>")>
CBPeripheralManager委托方法(对不起所有代码,只是想完成。):
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral {
// Determine the state of the peripheral
if ([peripheral state] == CBPeripheralManagerStatePoweredOff) {
NSLog(@"CoreBluetooth BLE hardware is powered off");
}
else if ([peripheral state] == CBPeripheralManagerStatePoweredOn) {
NSLog(@"CoreBluetooth BLE hardware is powered on and ready");
}
else if ([peripheral state] == CBPeripheralManagerStateUnauthorized) {
NSLog(@"CoreBluetooth BLE state is unauthorized");
}
else if ([peripheral state] == CBPeripheralManagerStateUnknown) {
NSLog(@"CoreBluetooth BLE state is unknown");
}
else if ([peripheral state] == CBPeripheralManagerStateUnsupported) {
NSLog(@"CoreBluetooth BLE hardware is unsupported on this platform");
}
}
- (void)peripheralManager:(CBPeripheralManager *)peripheral
didAddService:(CBService *)service
error:(NSError *)error {
if (error) {
NSLog(@"Error publishing service: %@", [error localizedDescription]);
return;
}
else {
NSLog(@"Hurray! Your Service has been successfully published as: %@", service);
}
}
- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral
error:(NSError *)error {
if (error == nil) {
NSLog(@"Your service is now advertising");
}
else {
NSLog(@"In peripheralManagerDidStartAdvertising: Your service advertising failed with error: %@", error);
}
}
以下是在iPad Mini上运行的相关中央代码:
// Scan for all available CoreBluetooth LE devices
NSArray *services = @[[CBUUID UUIDWithString:IMMEDIATE_ALERT_SERVICE_UUID]];
CBCentralManager *centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
//[centralManager scanForPeripheralsWithServices:services options:nil];
[centralManager scanForPeripheralsWithServices:nil options:nil];
self.centralManager = centralManager;
这是中央委托方法之一。除了centralManagerDidUpdateState:之外,没有任何委托方法触发。
// CBPeripheralDelegate - Invoked when you discover the peripheral's available services.
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
NSLog(@"Did Discover Services");
for (CBService *service in peripheral.services) {
[peripheral discoverCharacteristics:nil forService:service];
}
}
// CBCentralManagerDelegate - This is called with the CBPeripheral class as its main input parameter. This contains most of the information there is to know about a BLE peripheral.
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
NSLog(@"Did Discover Peripheral");
NSString *localName = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey];
if (![localName isEqual:@"My Service"]) {
// We found the Device
[self.centralManager stopScan];
self.myPeripheral = peripheral;
peripheral.delegate = self;
[self.centralManager connectPeripheral:peripheral options:nil];
}
}
作为最后一点,我质疑BLE是否适用于我的设备。我在iPhone和iPad Mini上加载了几个不同的iBeacon应用程序,看看我是否可以让这两个设备识别iBeacons(一个发送,一个接收),但他们也没有发现iBeacons。我也试过两个iPhone。我还关闭了蓝牙。我还尝试关闭/打开设备电源。两台设备都在前台运行。仍然没有运气。请帮忙。
答案 0 :(得分:1)
我将在这里连接所有评论:
使用 LightBlue 或 BLE Utility 等应用可以帮助您查找问题是在外围还是中心,因为您自己开发双方。
在查找CBServices
之前,您必须连接到CBPeripheral
。
你事先显示的方法,似乎并不明显。
此外,在使用CBCentralManager
开始扫描之前,您必须检查其state
,并且必须是CBPeripheralManagerStatePoweredOn
。