从CBPeripheral IOS获取电话名称

时间:2015-01-01 14:53:36

标签: ios bluetooth core-bluetooth

我正在开发一个使用CoreBluetooth框架的应用程序IOS应用程序,我试图从“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
}
}

任何想法??

1 个答案:

答案 0 :(得分: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;
}