我一直致力于实现蓝牙模块以连接BLE设备。说到执行,它表明这里有
EXC_BREAKPOINT(EXC_ARM_BREAKPOINT .. that)
并在单击表格单元格调用方法didSelectRowAtIndexPath时突然退出。
是否有其他替代方案可以连接BLE设备或加载BLE设备的更多信息。似乎如果BLE设备已经与其他iOS设备配对,我们就无法启动配对和连接。真的吗 ?
停止线是
_currentperipheral = [_foundPeripherals objectAtIndex:indexPath.row];
以下是我的代码
#import "BluetoothTableViewController.h"
#import "BTSentCommandViewController.h"
#import "BTDevice.h";
@interface BluetoothTableViewController ()
@end
@implementation BluetoothTableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_pendingInit = YES;
_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
_currentperipheral=[[CBPeripheral alloc]init];
_founddevice=FALSE;
_foundPeripherals = [[NSMutableArray alloc] init];
_connectedServices = [[NSMutableArray alloc] init];
}
- (void)viewWillDisappear:(BOOL)animated {
[_centralManager stopScan];
NSLog(@"Scanning stopped");
[super viewWillDisappear:animated];
}
#pragma mark - Table view data source
- (void)tableView: (UITableView *)tableView
didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
if( [_centralManager isEqual:nil ] ){
NSLog(@"Bluetooth central manager is nil , cannot instantiate");
}else{
if( _centralManager.state != CBCentralManagerStatePoweredOn)
{
NSLog(@"Bluetooth not turned on");
}else{
_currentperipheral = [_foundPeripherals objectAtIndex:indexPath.row];
[_centralManager connectPeripheral: _currentperipheral options:nil]; // it's wrong
BTSentCommandViewController* sliderVC= [self.storyboard instantiateViewControllerWithIdentifier:@"BTSentCommandViewController"];
sliderVC.centralManager=_centralManager;
sliderVC.periperhal= _currentperipheral;
// sliderVC.delegate = self;
[self presentViewController:sliderVC animated:NO completion:nil];
}
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
return [_foundPeripherals count];
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
// You should test all scenarios
if (central.state != CBCentralManagerStatePoweredOn) {
return;
}
if (central.state == CBCentralManagerStatePoweredOn) {
// Scan for devices
[_centralManager scanForPeripheralsWithServices:nil options:nil];
NSLog(@"Scanning started");
}
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
// NSLog(@"Discovered %@ at %@", peripheral.name, RSSI);
if ( ![_foundPeripherals containsObject:peripheral]) {
// BTDevice *myDevice=[[BTDevice alloc] init];
// [myDevice setName: peripheral.name];
// NSString * macAddress = [NSString stringWithFormat:@"%@" , peripheral.identifier];
// [myDevice setMacAddress: macAddress];
[_foundPeripherals addObject: peripheral ] ;
[self.tableView reloadData];
// And connect
}
}
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
NSLog(@"Failed to connect");
[self cleanup];
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral {
NSLog(@"Connected");
[_centralManager stopScan];
NSLog(@"Scanning stopped");
// [_data setLength:0];
peripheral.delegate = self;
[peripheral discoverServices:@[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]]];
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error {
if (error) {
[self cleanup];
return;
}
for (CBService *service in peripheral.services) {
[peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]] forService:service];
}
// Discover other characteristics
}
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error {
if (error) {
[self cleanup];
return;
}
for (CBCharacteristic *characteristic in service.characteristics) {
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]]) {
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
}
}
}
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
if (error) {
NSLog(@"Error");
return;
}
NSString *stringFromData = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
// Have we got everything we need?
if ([stringFromData isEqualToString:@"EOM"]) {
//[_textview setText:[[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding]];
[peripheral setNotifyValue:NO forCharacteristic:characteristic];
[_centralManager cancelPeripheralConnection:peripheral];
}
//[_data appendData:characteristic.value];
}
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
if (![characteristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]]) {
return;
}
if (characteristic.isNotifying) {
NSLog(@"Notification began on %@", characteristic);
} else {
// Notification has stopped
[_centralManager cancelPeripheralConnection:peripheral];
}
}
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
_foundPeripherals = nil;
[_centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:TRANSFER_SERVICE_UUID]] options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES }];
}
- (void)cleanup {
// See if we are subscribed to a characteristic on the peripheral
if (_currentperipheral.services != nil) {
for (CBService *service in _currentperipheral.services) {
if (service.characteristics != nil) {
for (CBCharacteristic *characteristic in service.characteristics) {
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:TRANSFER_CHARACTERISTIC_UUID]]) {
if (characteristic.isNotifying) {
[_currentperipheral setNotifyValue:NO forCharacteristic:characteristic];
return;
}
}
}
}
}
}
[_centralManager cancelPeripheralConnection:_currentperipheral];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"reuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryNone;
if (cell == nil) {
cell = [ [UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuseIdentifier"] ;
cell.selectionStyle = UITableViewCellSelectionStyleGray;
}
CBPeripheral *peripheral=[_foundPeripherals objectAtIndex:indexPath.row];
NSString * macAddress = [NSString stringWithFormat:@"%@%@" ,peripheral.name , peripheral.UUID ];
cell.textLabel.text = macAddress;
// Configure the cell...
// Configure the cell...
return cell;
}
答案 0 :(得分:1)
为未知异常添加一些“前缀” ...修改了代码..试试这个...其自我解释
- (void)tableView: (UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
if(_centralManager){
if( _centralManager.state != CBCentralManagerStatePoweredOn)
{
NSLog(@"Bluetooth not turned on");
}
else
{
if ([_foundPeripherals count]> indexpath.row) {
_currentperipheral = [_foundPeripherals objectAtIndex:indexPath.row];
[_centralManager connectPeripheral: _currentperipheral options:nil]; // it's wrong
BTSentCommandViewController* sliderVC= [self.storyboard instantiateViewControllerWithIdentifier:@"BTSentCommandViewController"];
sliderVC.centralManager=_centralManager;
sliderVC.periperhal= _currentperipheral;
// sliderVC.delegate = self;
[self presentViewController:sliderVC animated:NO completion:nil];
}
else{
NSLog(@"Value doesn't exist in array");
}
}
}
else
{
NSLog(@"Bluetooth central manager is nil , cannot instantiate");
}
}
答案 1 :(得分:1)
你得到的是因为你的应用正在创建一个CBPeripheral。所有CBPeripherals都应该由CoreBluetooth框架创建。
在CBCentralManager上使用以下方法获取CBPeripheral:
- (void)scanForPeripheralsWithServices:(NSArray *)serviceUUIDs options:(NSDictionary *)options
在您的代理人身上,这将调用centralManager:didDiscoverPeripheral:advertisementData:RSSI:
并向您传递对CBPeripheral的引用。
- (NSArray *)retrieveConnectedPeripheralsWithServices:(NSArray *)serviceUUIDs
- (NSArray *)retrievePeripheralsWithIdentifiers:(NSArray *)identifiers
这两个CBPeripherals的返回数组(当前已连接或先前已连接)。
永远不要使用[[CBPeripheral alloc] init]
或[CBPeripheral new]
创建CBPeripheral,否则在发布CBPeripheral时会出现此错误。