iOS Core Bluetooth Not asking for Pair(iOS Core蓝牙不要求配对)
问题描述
在我最近的项目中,我需要与硬件通信(蓝牙低功耗).我已经实现了所有委托方法代码.我能够连接硬件和设备,但我没有收到配对警报(附加屏幕截图).为什么不要求配对?谢谢你.
In My recent project, I need to communicate a Hardware (Bluetooth Low energy).I have implement all the delegate methods code. I am able to Connect hardware and device, But I am not getting pairing alert (Attached screen shot). Why not it is asking for pairing? Thank you.
#import "BTWCentralConnectionManager.h"
@implementation BTWCentralConnectionManager
@synthesize cbcManager;
@synthesize discoveredPeripheral;
@synthesize findMeServiceCharacteristic;
@synthesize findMeService;
@synthesize delegate=_delegate;
static NSString *kFindMeServiceUUID=@"1802";
static NSString *kFindMeCharacteristicUUID=@"2A06";
static BTWCentralConnectionManager* connectionManager = nil;
+(BTWCentralConnectionManager *)sharedConnectionManager{
@synchronized(self)
{
if (!connectionManager){
connectionManager=[[self alloc] init];
}
return connectionManager;
}
return nil;
}
-(void)findMe {
Byte code=0x02;
if(self.discoveredPeripheral){
[self.discoveredPeripheral writeValue:[NSData dataWithBytes:&code length:1] forCharacteristic:self.findMeServiceCharacteristic type:CBCharacteristicWriteWithoutResponse];
}else{
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Test" message:@"Invalid Charactersitcs" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alertView show];
alertView=nil;
}
}
-(void)searchForDevices{
self.cbcManager=[[CBCentralManager alloc] initWithDelegate:self queue:nil];
}
-(void)connect {
NSDictionary* connectOptions = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:CBConnectPeripheralOptionNotifyOnDisconnectionKey];
[self.cbcManager connectPeripheral:self.discoveredPeripheral options:connectOptions];
}
-(void)disconnect{
[self cleanup];
}
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
switch (central.state) {
case CBCentralManagerStatePoweredOn:{
[self.cbcManager scanForPeripheralsWithServices:@[ [CBUUID UUIDWithString:kFindMeServiceUUID] ] options:@{CBCentralManagerScanOptionAllowDuplicatesKey : @NO }];
}
break;
// Scans for any peripheral
default:{
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Test" message:@"Cental Manager did change state" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alertView show];
alertView=nil;
}
break;
}
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
// Stops scanning for peripheral
[self.cbcManager stopScan];
if (self.discoveredPeripheral != peripheral) {
self.discoveredPeripheral = peripheral;
[self.delegate didDeviceDiscoverd:self.discoveredPeripheral.name];
}
}
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
{
[self.delegate didDeviceConnectionFailed:error];
[self cleanup];
}
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
[self.delegate didDeviceConnected];
[self.discoveredPeripheral setDelegate:self];
[self.discoveredPeripheral discoverServices:@[[CBUUID UUIDWithString:kFindMeServiceUUID]]];
}
- (void)peripheral:(CBPeripheral *)aPeripheral didDiscoverServices:(NSError *)error {
if (error) {
NSString *strMsg=[NSString stringWithFormat:@"didDiscoverServices: %@", error];
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Test" message:strMsg
delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alertView show];
alertView=nil;
[self cleanup];
return;
}
for (CBService *service in aPeripheral.services) {
if ([service.UUID isEqual:[CBUUID UUIDWithString:kFindMeServiceUUID]]) {
self.findMeService=service;
[self.discoveredPeripheral discoverCharacteristics:@[[CBUUID UUIDWithString:kFindMeCharacteristicUUID]] forService:self.findMeService];
}
}
}
- (void) peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
if(error){
NSString *strMsg=[NSString stringWithFormat:@"didDiscoverCharacteristicsForService: %@", error];
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Test" message:strMsg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alertView show];
alertView=nil;
}
for(CBCharacteristic *character in [service characteristics])
{
if([[service UUID] isEqual:[CBUUID UUIDWithString:kFindMeServiceUUID]] &&
[[character UUID] isEqual:[CBUUID UUIDWithString:kFindMeCharacteristicUUID]])
{
NSString *strMsg=[NSString stringWithFormat:@"didDiscoverCharacteristicsForService: %@", character];
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Test" message:strMsg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alertView show];
alertView=nil;
self.findMeServiceCharacteristic = character;
}
}
}
- (void) peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
NSString *strMsg=[NSString stringWithFormat:@"Did update value for characteristic %@, new value: %@, error: %@", characteristic, [characteristic value], error];
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Test" message:strMsg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alertView show];
alertView=nil;
}
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
if (error) {
NSLog(@"Error changing notification state: %@", error.localizedDescription);
}
// Exits if it's not the transfer characteristic
if (![characteristic.UUID isEqual:[CBUUID UUIDWithString:kFindMeCharacteristicUUID]]) {
return;
}
NSString *strMsg=[NSString stringWithFormat:@"didUpdateNotificationStateForCharacteristic %@, reason: %@", characteristic, error];
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Test" message:strMsg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alertView show];
alertView=nil;
}
- (void) peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
if (error)
{
NSString *strMsg=[NSString stringWithFormat:@"Failed to write value for characteristic %@, reason: %@", characteristic, error];
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Test" message:strMsg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alertView show];
alertView=nil;
}
else
{
NSString *strMsg=[NSString stringWithFormat:@"Did write value for characterstic %@, new value: %@", characteristic, [characteristic value]];
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"Test" message:strMsg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alertView show];
alertView=nil;
}
}
- (void)cleanup
{
if (!self.discoveredPeripheral.isConnected) {
return;
}
if (self.discoveredPeripheral.services != nil) {
for (CBService *service in self.discoveredPeripheral.services) {
if (service.characteristics != nil) {
for (CBCharacteristic *characteristic in service.characteristics) {
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:kFindMeServiceUUID]]) {
if (characteristic.isNotifying) {
[self.discoveredPeripheral setNotifyValue:NO forCharacteristic:characteristic];
return;
}
}
}
}
}
}
[self.cbcManager cancelPeripheralConnection:self.discoveredPeripheral];
[self.delegate didDeviceDisconnected];
}
@end
`
推荐答案
如果我理解正确,您可以成功地将值写入特征,但您没有收到配对请求.
If I understand you right you can write a value successfully to a characteristic but you don't get a pairing request.
配对由外设触发.这意味着外围设备必须拒绝您的中心对特性的写入或读取请求.您的中央获得拒绝未经授权的身份验证",然后尝试与外围设备配对并显示您正在等待的配对警报弹出窗口.这一切都是由核心蓝牙自动完成的.您唯一需要做的就是更改外围设备中的特征选项和权限.这是触发配对的苹果示例代码:
The pairing is triggered by the peripheral. Meaning the peripheral has to refuse the write or read request of your central to a characteristic. Your central gets the refusal "unauthorized authentication" and then tries to pair with the peripheral and showing the pairing alert pop up you are waiting for. This is all done by core bluetooth automatically. The only thing you need to do is change the characteristics options and permissions in your peripheral. This is the apple sample code to trigger a pairing:
emailCharacteristic = [[CBMutableCharacteristic alloc]
initWithType:emailCharacteristicUUID
properties:CBCharacteristicPropertyRead
| CBCharacteristicPropertyNotifyEncryptionRequired
value:nil permissions:CBAttributePermissionsReadEncryptionRequired];
来源:CoreBluetooth
_concepts iOS7
预览
source: CoreBluetooth
_concepts iOS7
preview
还可以查看 WWDC 2012 高级核心蓝牙视频 28 分钟他们解释了配对的概念.
Also check out the WWDC 2012 advanced core bluetooth video at 28 minutes they explain the concept of pairing.
这篇关于iOS Core蓝牙不要求配对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:iOS Core蓝牙不要求配对
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01