从CBPheripheral IOS获取电话名称

问题描述 投票:0回答:1

我正在开发一个应用程序IOS应用程序使用CoreBluetooth框架,我试图从“didDiscoverPheripheral”方法的CBPeripheral对象获取手机的名称,并将其添加到数组,以便我可以在tableView上显示它。我的意思是“手机的名字”就像“丹的iPhone”。稍后我想按下tableView上的行,这样我就可以连接到那个设备了。

我试过了 :

 - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral    *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {

NSLog(@"Discovered %@ at %@", peripheral.name, RSSI);

if (_discoveredPeripheral != peripheral) {
    // Save a local copy of the peripheral, so CoreBluetooth doesn't get rid of it
    _discoveredPeripheral = peripheral;

    // And connect
    NSLog(@"Connecting to peripheral %@", peripheral);
   // here i want to add the name of the device to an array
}
}

有任何想法吗??

ios bluetooth core-bluetooth
1个回答
0
投票

为什么在发现之后不添加外围设备,而是在蓝牙类中创建另一种方法并调用它以从TableView连接到所选设备:

 - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral    *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {

     NSLog(@"Discovered %@ at %@", peripheral.name, RSSI);
      // add found devices to class variable NSMutableArray<CBPeripheral> *foundPeripherals
     // do not forget to initialize it
     [self.foundPeripherals addObject:peripheral];

}

// create a connect method where you pass the device selected identifier
- (void) connect:(NSString*)uuidString {
    CBPeripheral* peripheral;
    for (peripheral in self.foundPeripherals){
        if(peripheral.state == CBPeripheralStateDisconnected){
            // this will invoke didConnectPeripheral once connection 
            // set successfully
            [self connectPeripheral:peripheral];
        }
    }
}

// store the connected peripheral
- (void) centralManager:(CBCentralManager *)central didConnectPeripheral:     (CBPeripheral *)peripheral
{
    NSLog(@"Attempted connection to peripheral %@ succeed.", [peripheral name]);
    // store the connected device info in class property
    self.peripheral = peripheral;
}
© www.soinside.com 2019 - 2024. All rights reserved.