从 CBPheripheral IOS 获取手机名称
Posted
技术标签:
【中文标题】从 CBPheripheral IOS 获取手机名称【英文标题】:get phone's name from CBPheripheral IOS 【发布时间】:2015-01-01 14:53:36 【问题描述】:我正在开发一个使用 CoreBluetooth 框架的应用程序 ios 应用程序,我正在尝试从“didDiscoverPheripheral”方法的 CBPeripheral 对象中获取手机的名称并将其添加到数组中,以便可以在 tableView 上显示它。我的意思是“电话名称”类似于“Dan 的 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
有什么想法吗??
【问题讨论】:
NSLog(@"设备名称: %@", [[UIDevice currentDevice] name]); 运行这段代码会发生什么? 【参考方案1】:你为什么不在你发现之后添加外围设备,而不是在你的蓝牙类中创建另一个方法并调用它来从 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;
【讨论】:
以上是关于从 CBPheripheral IOS 获取手机名称的主要内容,如果未能解决你的问题,请参考以下文章
如何以编程方式从 iOS 设备获取自己的手机号码? [复制]