如我们所知,iOS 6支持运行设备(iPhone 4s和更高版本,以及新的iPad)作为BLE外围设备。在WWDC 2012会议上有一个名为“高级核心蓝牙”的演示。我问苹果公司的源代码。他们给我发了一个修改过的源代码(BTLE_Transfer_Draft)版本。然后我:
问题是外围设备根本就没有被发现。因此,我使用其他测试应用程序,包括从App下载的一些应用程序。都没有发现外设。我认为问题应该在BTLE_Transfer_Draft中解决。因为我不确定是否允许我展示整个源代码。所以我只是在这里展示了“外围模式”部分:
- (void)viewDidLoad {
[super viewDidLoad];
// Start up the CBPeripheralManager
_peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
}
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral {
// Opt out from any other state
if (peripheral.state != CBPeripheralManagerStatePoweredOn) {
return;
}
// We're in CBPeripheralManagerStatePoweredOn state...
NSLog(@"self.peripheralManager powered on.");
// ... so build our service.
// Start with the CBMutableCharacteristic
self.transferCharacteristic = [[CBMutableCharacteristic alloc] initWithType:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]
properties:CBCharacteristicPropertyNotify
value:nil
permissions:CBAttributePermissionsReadable];
// Then the service
CBMutableService *transferService = [[CBMutableService alloc] initWithType:[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]
primary:YES];
// Add the characteristic to the service
transferService.characteristics = @[self.transferCharacteristic];
// And add it to the peripheral manager
[self.peripheralManager addService:transferService];
}
/** Start advertising
*/
- (IBAction)switchChanged:(id)sender
{
if (self.advertisingSwitch.on) {
// All we advertise is our service's UUID
[self.peripheralManager startAdvertising:@{ CBAdvertisementDataServiceUUIDsKey : @[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]] }];
}
else {
[self.peripheralManager stopAdvertising];
}
}
BLE处于开机状态,startAdvertising被调用。但是BLE中心永远不会发现它。
更新:
根据姆特里布的建议,我在CBAdvertisementDataLocalNameKey中添加了"CBAdvertisementDataLocalNameKey“,但我的服务仍然无法被大多数应用程序发现,包括来自应用商店的一些应用程序。唯一能发现我的服务的应用程序是来自应用商店的一个名为"BLE扫描仪”的应用程序。
我的问题是:这是否意味着我的应用程序是作为外设工作的?但是为什么我自己的代码不能发现这个服务呢?我该怎么调试它?
我在中央模式下的代码如下:
- (void)viewDidLoad
{
[super viewDidLoad];
// Start up the CBCentralManager
_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
if (central.state != CBCentralManagerStatePoweredOn) {
return;
}
[self.centralManager scanForPeripheralsWithServices:nil options:nil];
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
......
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
if (error) {
NSLog(@"Error discovering services: %@", [error localizedDescription]);
return;
}
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
// Deal with errors (if any)
if (error) {
NSLog(@"Error discovering characteristics: %@", [error localizedDescription]);
return;
}
}
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSLog(@"Peripheral Disconnected");
self.discoveredPeripheral = nil;
}
didDiscoverPeripheral和didDiscoverServices从未被调用过。有什么不对的?有什么想法吗?谢谢
发布于 2012-11-06 23:37:24
还有一个高质量的免费应用程序叫做LightBlue,你可以用它来测试你的代码。它应该能够获得所有的设备广告在外围模式,它甚至可以把自己变成一个广告周边,如果你想确保你的设备正常工作。
发布于 2012-10-24 04:01:44
我会尝试将startAdvertising:
方法调用移到peripheralManagerDidUpdateState:
委托方法的末尾,看看这是否有帮助。
我还将向您的CBAdvertisementDataLocalNameKey
方法调用添加一个startAdvertising:
键值对。当广告没有名字的时候,我发现事情是不可靠的。
最后,我将投资于App中可用的BLExplr应用程序,以帮助扫描您的外围设备。它消除了你的中心正常工作的假设。
发布于 2013-01-08 10:43:14
这个Git项目也为Git提供了一些启示。叫PeripheralModeTest。这一行对于设置广告数据特别有用。
NSDictionary *advertisingData = @{CBAdvertisementDataLocalNameKey : @"Device Name", CBAdvertisementDataServiceUUIDsKey : @[[CBUUID UUIDWithString:CBUUIDGenericAccessProfileString]]};
虽然我还没有在苹果iOS开发者库中看到任何官方文档。更具体地说,任何关于设置广告重复周期的内容。
https://stackoverflow.com/questions/13041930
复制相似问题