- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在以前使用 MFi 设备的项目中使用 BLE。
目标是实现一种串行连接。
使用 EAaccessory,我有一个弹出框,要求连接设备并且工作正常。
使用 CB,我添加了一个 View ,我调用它来扫描并选择要连接的设备。
我可以很好地连接设备,但是当我返回主视图时,我失去了与外围设备的连接。
CoreBluetooth[WARNING] <CBConcretePeripheral: ... IsConnected = YES> is being dealloc'ed while connected
#import "ScanTableViewController.h"
#import <CoreBluetooth/CBCentralManager.h>
#import <CoreBluetooth/CBPeripheral.h>
#import "DiscoveredPeripheral.h"
#import "ScanCell.h"
typedef enum
{
SCAN_S_NOT_LOADED,
SCAN_S_DISAPPEARED,
SCAN_S_WILL_DISAPPEAR,
SCAN_S_APPEARED_IDLE,
SCAN_S_APPEARED_SCANNING
} SCAN_State;
@interface ScanTableViewController ()
@end
@implementation ScanTableViewController
{
SCAN_State state;
CBCentralManager *cbCentralManager;
NSMutableArray *discoveredPeripherals;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"Scan viewDidLoad");
cbCentralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
// Do any additional setup after loading the view from its nib.
CGRect tableViewFrame = self.view.bounds;
UITableView *tableview = [[UITableView alloc] initWithFrame:tableViewFrame style:UITableViewStylePlain];
self.myTableView = tableview;
self.myTableView.rowHeight = 60;
self.myTableView.dataSource = self;
self.myTableView.delegate = self;
//Make sure our table view resizes correctly
self.myTableView.autoresizingMask =
UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight;
[self.view addSubview:tableview];
discoveredPeripherals = [[NSMutableArray alloc] init];
//[cbCentralManager retrieveConnectedPeripherals];
state = SCAN_S_DISAPPEARED;
}
- (void)viewDidUnload
{
//[self setScanButton:nil];
[super viewDidUnload];
NSLog(@"Scan viewDidUnload");
cbCentralManager = nil;
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
state = SCAN_S_NOT_LOADED;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"Scan viewWillAppear");
[self.myTableView reloadData];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
NSLog(@"Scan viewDidAppear");
[self clearPeriph];
state = SCAN_S_APPEARED_IDLE;
[self.myTableView reloadData];
}
- (void)viewWillDisappear:(BOOL)animated
{
NSLog(@"Scan viewWillDisappear");
//[self scan: FALSE];
state = SCAN_S_WILL_DISAPPEAR;
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
NSLog(@"Scan viewDidDisappear");
state = SCAN_S_DISAPPEARED;
}
-(void) enterForeground
{
NSLog(@"Scan enterForeground");
[self clearPeriph];
state = SCAN_S_APPEARED_IDLE;
}
-(void) enterBackground
{
NSLog(@"Scan enterBackground");
[self scan: FALSE];
state = SCAN_S_DISAPPEARED;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) initWithPeripherals: (NSMutableArray*) dp
{
NSLog(@"Scan initWithPeripherals");
discoveredPeripherals = dp;
state = SCAN_S_NOT_LOADED;
}
#pragma mark - Table view data source
// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//NSLog(@"Nombre de sections");
return 2;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSInteger nRows;
switch(section)
{
case 0:
nRows = 1;
break;
case 1:
NSLog(@"Scan Nbre ligne section 1 : %i",discoveredPeripherals.count);
nRows = discoveredPeripherals.count;
break;
default:
nRows = 0;
break;
}
return nRows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//NSLog(@"Remplissage");
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
}
NSLog(@"Scan Section table:%u",indexPath.section);
switch(indexPath.section)
{
case 0:
{
cell.textLabel.text = @"";
if(state == SCAN_S_APPEARED_SCANNING)
{
cell.textLabel.text = @"Stop Scan";
//cell.labelInfo.text = @"Active";
//[cell.activityView startAnimating];
}
else
{
cell.textLabel.text = @"Start Scan";
//cell.labelInfo.text = @"Inactive";
//
//[cell.activityView stopAnimating];
}
break;
}
case 1:
{
if ( [discoveredPeripherals count] > 0)
{
DiscoveredPeripheral* discoveredPeripheral;
discoveredPeripheral = [discoveredPeripherals objectAtIndex:indexPath.row];
cell.textLabel.text =discoveredPeripheral.peripheral.name;
}
cell.detailTextLabel.textColor = [UIColor blackColor];
}
}
return cell;
}
- (NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *str;
switch(section)
{
case 0:
str = @"Bluetooth Low Energy Scanning";
break;
case 1:
str = @"Found Devices";
break;
default:
break;
}
return str;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if(cbCentralManager.state == CBCentralManagerStatePoweredOn)
{
//ScanCell* cell = (ScanCell*)[tableView cellForRowAtIndexPath:indexPath];
//static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(indexPath.section == 0)
{
if(state == SCAN_S_APPEARED_SCANNING)
{
[self scan: FALSE];
cell.textLabel.text = @"Start Scan";
//cell.labelInfo.text = @"Inactive";
//[cell.activityView stopAnimating];
state = SCAN_S_APPEARED_IDLE;
}
else if((state == SCAN_S_APPEARED_IDLE) &&
(cbCentralManager.state == CBCentralManagerStatePoweredOn))
{
[self scan: TRUE];
cell.textLabel.text = @"Stop Scan";
//cell.labelInfo.text = @"Active";
//[cell.activityView startAnimating];
state = SCAN_S_APPEARED_SCANNING;
}
}
else
{
DiscoveredPeripheral* dp = [discoveredPeripherals objectAtIndex:indexPath.row];
NSDictionary *dictionary;
switch (dp.state)
{
case DP_STATE_IDLE:
cell.textLabel.text = @"Connecting";
//[cell.activityView startAnimating];
dictionary = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:1] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey];
[cbCentralManager connectPeripheral:dp.peripheral options:dictionary];
dp.state = DP_STATE_CONNECTING;
break;
case DP_STATE_CONNECTED:
case DP_STATE_CONNECTING:
[cbCentralManager cancelPeripheralConnection:dp.peripheral];
cell.textLabel.text = @"";
//[cell.activityView stopAnimating];
cell.accessoryType = UITableViewCellAccessoryNone;
dp.state = DP_STATE_IDLE;
break;
default:
break;
}
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}
- (void) scan: (bool) enable
{
if(enable == TRUE)
{
NSLog(@"Scan Scan ON");
NSDictionary *dictionary = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:1] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
[cbCentralManager scanForPeripheralsWithServices:nil options:dictionary];
}
else
{
NSLog(@"Scan Scan Off");
[cbCentralManager stopScan];
}
}
- (IBAction)startScan:(id)sender {
if(state == SCAN_S_APPEARED_IDLE)
{
[self scan: TRUE];
state = SCAN_S_APPEARED_SCANNING;
}
else if(state == SCAN_S_APPEARED_SCANNING)
{
[self scan: FALSE];
state = SCAN_S_APPEARED_IDLE;
}
}
- (void) clearPeriphForRow: (NSInteger)row
{
DiscoveredPeripheral* dp = [discoveredPeripherals objectAtIndex:row];
//if( (dp.peripheral.isConnected == FALSE) &&
// ( (dp.state == DP_STATE_CONNECTED) || (dp.state == DP_STATE_DISCONNECTING)))
if(dp.peripheral.isConnected == FALSE)
{
dp.state = DP_STATE_IDLE;
}
else if( (dp.peripheral.isConnected == TRUE) &&
(dp.state != DP_STATE_CONNECTED))
{
dp.state = DP_STATE_CONNECTED;
}
if(dp.state == DP_STATE_IDLE)
{
[discoveredPeripherals removeObjectAtIndex:row];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:1];
//ScanCell* cell = (ScanCell*)[self.tableView cellForRowAtIndexPath:indexPath];
UITableViewCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath];
//[cell.activityView stopAnimating];
cell.accessoryType = UITableViewCellAccessoryNone;
[self.myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
- (void) clearPeriph
{
if(self->discoveredPeripherals.count > 0)
{
for(int i = discoveredPeripherals.count - 1; i >= 0 ; i--)
{
[self clearPeriphForRow:i];
}
}
[self.myTableView reloadData];
}
- (IBAction)clearPeripherals:(id)sender {
[self clearPeriph];
[self scan: FALSE];
state = SCAN_S_APPEARED_IDLE;
}
- (NSInteger)getRowForPeripheral: (CBPeripheral*)peripheral
{
NSInteger row = -1;
DiscoveredPeripheral* p;
for(int i = 0; (i < discoveredPeripherals.count) && (row == -1); i++)
{
p = [discoveredPeripherals objectAtIndex:i];
if([peripheral isEqual:p.peripheral] == TRUE)
{
row = i;
}
}
return row;
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSInteger row = [self getRowForPeripheral:peripheral];
if(row != -1)
{
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:row inSection:1];
UITableViewCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath];
//ScanCell* cell = (ScanCell*)[self.tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.text = [[NSString alloc] initWithFormat:@"Connected"];
//[cell.activityView stopAnimating];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
DiscoveredPeripheral* dp = [discoveredPeripherals objectAtIndex:row];
dp.state = DP_STATE_CONNECTED;
//[peripheral discoverServices:nil];
//[self scan:FALSE];
//[[self navigationController] popViewControllerAnimated:NO];
//[self.delegate didConnectedPeriph:dp];
}
}
- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSInteger row = [self getRowForPeripheral:peripheral];
if(row != -1)
{
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:row inSection:1];
UITableViewCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath];
//ScanCell* cell = (ScanCell*)[self.tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.text = [[NSString alloc] initWithFormat:@""];
cell.accessoryType = UITableViewCellAccessoryNone;
//[cell.activityView stopAnimating];
DiscoveredPeripheral* dp = [discoveredPeripherals objectAtIndex:row];
dp.state = DP_STATE_IDLE;
}
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
bool new = TRUE;
DiscoveredPeripheral* discPeripheral;
int row = -1;
if((state == SCAN_S_APPEARED_SCANNING) &&
(peripheral != nil))
{
for(int i = 0; (i < discoveredPeripherals.count) && (new == TRUE); i++)
{
NSLog(@"Scan Ajout periph");
discPeripheral = [discoveredPeripherals objectAtIndex:i];
if(discPeripheral.peripheral == peripheral)
{
new = false;
row = i;
discPeripheral.peripheral = peripheral;
}
}
if(new == TRUE)
{
discPeripheral = [[DiscoveredPeripheral alloc] initWithPeripheral:peripheral andAdvertisment:advertisementData andRssi:RSSI];
discPeripheral.rssi = RSSI;
if(peripheral.isConnected == TRUE)
{
discPeripheral.state = DP_STATE_CONNECTED;
}
[discoveredPeripherals addObject:discPeripheral];
NSLog(@"Scan Ajout periph, total:%i",[discoveredPeripherals count]);
NSLog(@"Scan %i: Add %@",[discoveredPeripherals count]-1, discPeripheral.peripheral.name);
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[discoveredPeripherals count]-1 inSection:1];
NSLog(@"Scan Nouveau periph, index:%i, section:%i",indexPath.row,indexPath.section);
UITableViewCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath];
cell.textLabel.text = discPeripheral.peripheral.name;
//[self.tableView insertRowsAtIndexPaths: [NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.myTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
else
{
discPeripheral.peripheral = peripheral;
discPeripheral.advertisment = advertisementData;
discPeripheral.rssi = RSSI;
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:row inSection:1];
UITableViewCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath];
//ScanCell* cell = (ScanCell*)[self.tableView cellForRowAtIndexPath:indexPath];
//NSLog(@"%i: Update %@", row, discPeripheral.peripheral.name);
cell.textLabel.text = discPeripheral.peripheral.name;
//cell.labelInfo.text = [[NSString alloc] initWithFormat:@"RSSI: %@", discPeripheral.rssi];
}
}
}
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
NSInteger row = [self getRowForPeripheral:peripheral];
if(row != -1)
{
NSIndexPath* indexPath = [NSIndexPath indexPathForRow:row inSection:1];
UITableViewCell *cell = [self.myTableView cellForRowAtIndexPath:indexPath];
//ScanCell* cell = (ScanCell*)[self.tableView cellForRowAtIndexPath:indexPath];
cell.textLabel.text = [[NSString alloc] initWithFormat:@""];
cell.accessoryType = UITableViewCellAccessoryNone;
//[cell.activityView stopAnimating];
DiscoveredPeripheral* dp = [discoveredPeripherals objectAtIndex:row];
dp.state = DP_STATE_IDLE;
}
}
- (void)centralManager:(CBCentralManager *)central didRetrieveConnectedPeripherals:(NSArray *)peripherals
{
//DiscoveredPeripheral* discPeripheral;
CBPeripheral* peripheral;
for(int i = 0; i < peripherals.count; i++)
{
peripheral = [peripherals objectAtIndex:i];
/*
discPeripheral = [[DiscoveredPeripheral alloc] initWithPeripheral:peripheral andAdvertisment:nil andRssi:nil];
if(peripheral.isConnected == TRUE)
{
discPeripheral.state = DP_STATE_CONNECTED;
}
[discoveredPeripherals addObject:discPeripheral];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[discoveredPeripherals count] - 1 inSection:1];
[self.tableView insertRowsAtIndexPaths: [NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
*/
//[cbCentralManager cancelPeripheralConnection:peripheral];
NSDictionary *dictionary;
dictionary = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:1] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey];
[cbCentralManager connectPeripheral:peripheral options:dictionary];
}
}
- (void)centralManager:(CBCentralManager *)central didRetrievePeripherals:(NSArray *)peripherals
{
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
//NSLog(@"Central Manager State: %d", [central state]);
if(central.state == CBCentralManagerStatePoweredOn)
{
[cbCentralManager retrieveConnectedPeripherals];
}
}
@end
最佳答案
您需要保留外围设备。一旦您传递的外围对象被释放,iOS 就会断开连接。保存 CBPeripheral
变量中的对象,并确保在扫描 View 关闭时将其传递回主视图。
关于ios - 核心蓝牙 : pass connected peripheral from view to another,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17559295/
使用 caret::train() 运行逻辑回归模型时出现问题。LR = caret::train(Satisfaction ~., data= log_train, method = "glm",
我正在尝试将nginx容器作为我所有网站和Web服务的主要入口点。我设法将portainer作为容器运行,并且可以从互联网上访问它。现在,我正在尝试访问由另一个Nginx容器托管的静态网站,但这样做失
我有一个在 Windows XP SP3 x86 上运行的 Visual Studio 2008 C# .NET 3.5 应用程序。在我的应用程序中,我有一个事件处理程序 OnSendTask 可以同
我在 Eclipse 中创建了作为独立程序执行的此类,它可以毫无问题地连接所有 http URL(例如:http://stackoverflow.com),但是当我尝试连接到 https(例如 htt
我在我的 nginx 错误日志中收到大量以下错误: connect() failed (111: Connection refused) while connecting to upstream 我的
我正在尝试将新的 log4j2 与 Socket Appender 一起使用,但我有点不走运。这是我的 XML 配置文件:
我目前正在尝试寻找 Android 应用程序后端的替代方案。目前,我使用 php servlet 来查询 Mysql 数据库。数据库(Mysql)托管在我大学的计算机上,因此我无法更改任何配置,因为我
类MapperExtension有一些方法,before_insert, before_update, ...都有一个参数connection. def before_insert(self, map
嗨,我正在尝试更改位于连接库 (v 5.5) 中的文档的文档所有者,我仍在等待 IBM 的回复,但对我来说可能需要太长时间,这就是我尝试的原因逆向工程。 我尝试使用标准编辑器 POST 请求将编辑器更
我在 nginx( http://52.xx.xx.xx/ )上访问我的 IP 时遇到 502 网关错误,日志只是这样说: 2015/09/18 13:03:37 [error] 32636#0: *
我要实现 Connected-Component Labeling但我不确定我应该以 4-connected 还是 8-connected 的方式来做。我已经阅读了大约 3 种 Material ,但
我在Resources ->JMS ->Connection Factories下有两个连接工厂。 1) 连接工厂 2)集成连接工厂 我想修改两个连接工厂下连接池的最大连接数。资源 ->JMS ->连
我在将 mongoengine 合并到我的 django 应用程序时遇到问题。以下是我收到的错误: Traceback (most recent call last): File "/home/d
上下文 我正在关注 tutorial on writing a TCP server last week in Real World Haskell .一切顺利,我的最终版本可以正常工作,并且能够在
我在访问我的域时遇到了这个问题:我看到了我的默认 http500 错误 django 模板正在显示。 我有 gunicorn 设置: command = '/usr/local/bin/gunicor
我更换了电脑,并重新安装了所有版本:tomcat 8 和 6、netbeans 8、jdk 1.7、hibernate 4.3.4,但是当我运行 Web 应用程序时,出现此错误。过去使用我的旧电脑时,
您好,我是这个项目的新手,我在 CentOS7 ec2 实例上托管它时遇到问题。当我访问我的域时出现此错误: 2017/02/17 05:53:35 [error] 27#27: *20 connec
在开始之前,我已经查看了所有我能找到的类似问题,但没有找到解决我的问题的方法。 我正在运行 2 个 docker 容器,1 个用于 nginx,1 个用于 nodejs api。我正在使用 nginx
使用 debian 包将 kaa -iot 平台配置为单节点时。我收到以下错误。 himanshu@himpc:~/kaa/deb$ sudo dpkg -i kaa-node-0.10.0.deb
我是我公司开发团队的成员,担任管理员角色。我可以通过 https://developer.apple.com/ 访问团队的成员(member)中心 但是,当我尝试在 https://itunescon
我是一名优秀的程序员,十分优秀!