gpt4 book ai didi

ios - 将 iOS 6 设备作为 BLE 外围设备运行

转载 作者:IT王子 更新时间:2023-10-29 08:19:40 24 4
gpt4 key购买 nike

据我们所知,iOS 6 支持运行设备(iPhone 4s 及以上,以及新的 iPad)作为 BLE 外设。 WWDC 2012 Session 705 中有一个名为“高级核心蓝牙”的演示。我向Apple要了源代码。他们向我发送了源代码的修改版本 (BTLE_Transfer_Draft)。然后我:

  • 在 iPhone 5 (iOS 6) 的“外设模式”下运行应用程序并启动“广告”
  • 在新 iPad (iOS 5.1.1) 中以“中央模式”运行应用

问题是根本没有发现外围设备。所以我使用其他测试应用程序,包括一些从 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];
}
}

BLE处于开机状态,调用了startAdvertising。但是 BLE Central 永远无法发现它。

帖子更新:

根据 mttrb的建议我在开始广告时添加了“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。有什么问题吗?任何想法?谢谢

最佳答案

还有一个名为 LightBlue 的高质量免费应用程序你可以用它来测试你的代码。它应该能够在外围模式下接收所有广告设备,如果您想确保您的设备正常工作,它甚至可以将自己变成一个广告外围设备。

关于ios - 将 iOS 6 设备作为 BLE 外围设备运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13041930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com