Bag*_*yer 16 peripherals ios core-bluetooth
众所周知,iOS 6支持运行设备(iPhone 4s及更高版本,以及新iPad)作为BLE外设.WWDC 2012 Session 705中有一个名为"高级核心蓝牙"的演示.我问过Apple的源代码.他们发给我一个修改后的源代码版本(BTLE_Transfer_Draft).然后我:
问题是外围设备从未被发现过.所以我使用其他测试应用程序,包括从App Store下载的一些.所有人都未能发现外围设备.我认为问题应该在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];
}
}
Run Code Online (Sandbox Code Playgroud)
BLE处于开机状态,并调用startAdvertising.但是BLE中心永远无法发现它.
发布更新:
根据mttrb的建议,我在startAdvertising时添加了"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;
}
Run Code Online (Sandbox Code Playgroud)
永远不会调用didDiscoverPeripheral和didDiscoverServices.可能有什么不对?任何的想法?谢谢
我会尝试将startAdvertising:方法调用移至委托方法的末尾peripheralManagerDidUpdateState:,看看是否有帮助。
我还会CBAdvertisementDataLocalNameKey向您的方法调用添加一个键值对startAdvertising:。当广告没有名字时,我发现事情不可靠。
最后,我会投资App Store 中提供的BLExplr 应用程序,以帮助扫描您的外围设备。它消除了您的中央设备正常工作的假设。
| 归档时间: |
|
| 查看次数: |
22368 次 |
| 最近记录: |