IOS蓝牙主动蓝牙音箱显示断开连接接调用怎么调用

一、服务端(也叫周边设备吧。。脑残的翻译)
1.实现类必须遵守协议&CBPeripheralManagerDelegate
2.需要的主要类有:
@property(strong,nonatomic) CBPeripheralManager *peripheraM
@property(strong,nonatomic) CBMutableCharacteristic *customerC
&@property (strong,nonatomic) CBMutableService *customerS
3.调用流程代码中有注释
ViewController.m
BlueToothDemo
Created by PSH_Chen_Tao on 7/3/13.
Copyright (c) 2013 wolfman. All rights reserved.
#import "ViewController.h"
static NSString *const kCharacteristicUUID = @"CCE62C0F--ADFA-C8FC7EA2EE90";
static NSString *const kServiceUUID = @"50BD367B-6B17-4E81-B6E9-FB";
@interface ViewController ()
@implementation ViewController
@synthesize peripheraM
@synthesize customerC
@synthesize customerS
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//初始化后会直接调用代理的
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral
peripheraManager = [[CBPeripheralManager alloc]initWithDelegate:self queue:nil];
[peripheraManager startAdvertising:nil];
-(void)setUp{
CBUUID *characteristicUUID = [CBUUID UUIDWithString:kCharacteristicUUID];
customerCharacteristic = [[CBMutableCharacteristic alloc]initWithType:characteristicUUID properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsReadable];
CBUUID *serviceUUID = [CBUUID UUIDWithString:kServiceUUID];
customerService = [[CBMutableService alloc]initWithType:serviceUUID primary:YES];
[customerService setCharacteristics:@[characteristicUUID]];
//添加后就会调用代理的- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error
[peripheraManager addService:customerService];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
mark -- CBPeripheralManagerDelegate
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{
switch (peripheral.state) {
//在这里判断蓝牙设别的状态
当开启了则可调用
setUp方法(自定义)
case CBPeripheralManagerStatePoweredOn:
NSLog(@"powered on");
[self setUp];
case CBPeripheralManagerStatePoweredOff:
NSLog(@"powered off");
- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error{
if (error == nil) {
//添加服务后可以在此向外界发出通告 调用完这个方法后会调用代理的
//(void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(NSError *)error
[peripheraManager startAdvertising:@{CBAdvertisementDataLocalNameKey : @"Service",CBAdvertisementDataServiceUUIDsKey : [CBUUID UUIDWithString:kServiceUUID]}];
- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(NSError *)error{
NSLog(@"in peripheralManagerDidStartAdvertisiong:error");
二、客户端(也叫中心设备吧)
1.实现类要遵守协议&CBCentralManagerDelegate,CBPeripheralDelegate&
2.主要用到的类有&
@property(strong,nonatomic)CBCentralManager *centralM
@property(strong,nonatomic)NSMutableData *mutableD
@property(strong,nonatomic)CBPeripheral *
&3.一般的流程
ViewController.m
BlueToothClient
Created by PSH_Chen_Tao on 7/3/13.
Copyright (c) 2013 wolfman. All rights reserved.
9 #import "ViewController.h"
10 static NSString *const kCharacteristicUUID = @"CCE62C0F--ADFA-C8FC7EA2EE90";
12 static NSString *const kServiceUUID = @"50BD367B-6B17-4E81-B6E9-FB";
13 @interface ViewController ()
17 @implementation ViewController
19 @synthesize centralM
20 @synthesize mutableD
21 @synthesize
23 - (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//初始化后会调用代理CBCentralManagerDelegate 的 - (void)centralManagerDidUpdateState:(CBCentralManager *)central
centralManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];
32 - (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
38 #pragma
mark -- CBCentralManagerDelegate
39 - (void)centralManagerDidUpdateState:(CBCentralManager *)central{
switch (central.state) {
//判断状态开始扫瞄周围设备 第一个参数为空则会扫瞄所有的可连接设备
//指定一个CBUUID对象 从而只扫瞄注册用指定服务的设备
//scanForPeripheralsWithServices方法调用完后会调用代理CBCentralManagerDelegate的
//- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI方法
case CBCentralManagerStatePoweredOn:
[centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:kServiceUUID]] options:@{CBCentralManagerScanOptionAllowDuplicatesKey : YES}];
56 - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
if (peripheral) {
self.peripheral =
//发现设备后即可连接该设备 调用完该方法后会调用代理CBCentralManagerDelegate的- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral表示连接上了设别
//如果不能连接会调用 - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
[centralManager connectPeripheral:peripheral options:@{CBConnectPeripheralOptionNotifyOnConnectionKey : YES}];
65 - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
NSLog(@"has connected");
[mutableData setLength:0];
self.peripheral.delegate =
//此时设备已经连接上了
你要做的就是找到该设备上的指定服务 调用完该方法后会调用代理CBPeripheralDelegate(现在开始调用另一个代理的方法了)的
//- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
[self.peripheral discoverServices:@[[CBUUID UUIDWithString:kServiceUUID]]];
76 - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
//此时连接发生错误
NSLog(@"connected periphheral failed");
82 #pragma mark -- CBPeripheralDelegate
83 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
if (error==nil) {
//在这个方法中我们要查找到我们需要的服务
然后调用discoverCharacteristics方法查找我们需要的特性
//该discoverCharacteristics方法调用完后会调用代理CBPeripheralDelegate的
//- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
for (CBService *service in peripheral.services) {
if ([service.UUID isEqual:[CBUUID UUIDWithString:kServiceUUID]]) {
[peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:kCharacteristicUUID]] forService:service];
96 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
if (error==nil) {
//在这个方法中我们要找到我们所需的服务的特性 然后调用setNotifyValue方法告知我们要监测这个服务特性的状态变化
//当setNotifyValue方法调用后调用代理CBPeripheralDelegate的- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
for (CBCharacteristic *characteristic in service.characteristics) {
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:kCharacteristicUUID]]) {
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
109 - (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
if (error==nil) {
//调用下面的方法后 会调用到代理的- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
[peripheral readValueForCharacteristic:characteristic];
117 - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
阅读(...) 评论()Pages: 1/2
主题 : iOS蓝牙不立即断开问题请教
级别: 新手上路
可可豆: 58 CB
威望: 48 点
在线时间: 123(时)
发自: Web Page
来源于&&分类
iOS蓝牙不立即断开问题请教&&&
有熟悉iOS蓝牙的大神吗,请教一下蓝牙断开的问题,执行[centralManager cancelPeripheralConnection:self.peripheral];这个方法后,回调有提示蓝牙已断开,但实际手机蓝牙还处在连接的状态,蓝牙小图标还是亮着的.等了5秒左右才变成灰色,真正断开了蓝牙[ 此帖被zhangming-07-27 16:47重新编辑 ]
当上总经理,出任CEO,迎取白富美,走上人生巅峰...
级别: 精灵王
UID: 332950
可可豆: 1272 CB
威望: 1058 点
在线时间: 398(时)
发自: Web Page
Beacon是什么?0.0.0.0.
级别: 新手上路
可可豆: 58 CB
威望: 48 点
在线时间: 123(时)
发自: Web Page
回 1楼(mushroom) 的帖子
信标,可以百度了解一下
当上总经理,出任CEO,迎取白富美,走上人生巅峰...
级别: 骑士
UID: 525253
可可豆: 475 CB
威望: 473 点
在线时间: 1283(时)
发自: Web Page
回 楼主(zhangming8006) 的帖子
楼主刚做蓝牙么
级别: 圣骑士
UID: 550455
可可豆: 1339 CB
威望: 1134 点
在线时间: 642(时)
发自: Web Page
     不会啊,我这很快的,比安卓要快很多。基本上那头关闭蓝牙,我这就直接掉线了
级别: 新手上路
可可豆: 58 CB
威望: 48 点
在线时间: 123(时)
发自: Web Page
回 3楼(DREAMF3) 的帖子
是的,做蓝牙不久
当上总经理,出任CEO,迎取白富美,走上人生巅峰...
级别: 新手上路
可可豆: 58 CB
威望: 48 点
在线时间: 123(时)
发自: Web Page
回 4楼(djc) 的帖子
如果已经连接时间超过8秒左右,你再执行断开的方法,它会立即断开,如果刚刚连接上才2-3秒,就回调取消连接,则iPhone上的蓝牙标志还是高亮连接状态,要过几秒才会暗下来.
当上总经理,出任CEO,迎取白富美,走上人生巅峰...
级别: 侠客
UID: 306616
可可豆: 216 CB
威望: 173 点
在线时间: 1513(时)
发自: Web Page
cancelPeripheralConnection调用这个方法可能没有完成喔,看看苹果的注释
图片:tmp1d59682f.png
级别: 骑士
UID: 593782
可可豆: 520 CB
威望: 468 点
在线时间: 67(时)
发自: Web Page
级别: 骑士
UID: 513687
可可豆: 889 CB
威望: 652 点
在线时间: 536(时)
发自: Web Page
回 8楼() 的帖子
有这样蹭经验的吗
Pages: 1/2
关注本帖(如果有新回复会站内信通知您)
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 关注CVP公众号
扫一扫 浏览移动版主题 : ios 蓝牙4.0 如何实现正确断开链接
级别: 新手上路
可可豆: 0 CB
威望: 1 点
在线时间: 0(时)
发自: Web Page
ios 蓝牙4.0 如何实现正确断开链接&&&
我打算在APP退出前台就断开链接,我在APPdelegate.m 的enterbackground 函数里直接调用了cancelPeripheralConnection:(CBPeripheral *)peripheral;但是总是出现程序死掉,哪位大侠能否帮我解答下。谢谢!
级别: 新手上路
可可豆: 25 CB
威望: 25 点
在线时间: 112(时)
发自: Web Page
我也遇到了这个问题。。求解
级别: 新手上路
可可豆: 11 CB
威望: 11 点
在线时间: 413(时)
发自: Web Page
你要把central设置成viewcontroller的属性,去调用断开链接的方法。然后在APPdelegate.m 的enterbackground 函数里获取到那个viewcontroller。
级别: 新手上路
可可豆: 94 CB
威望: 67 点
在线时间: 86(时)
发自: Web Page
centralManager不是同一个对象?原生的corebluetooth使用起来很麻烦,有很多委托,执行代码的位置非常跳跃。你可以试试这个蓝牙库BabyBluetooth,它简化了corebluetooth复杂的委托方法和来回跳跃式编程的问题。
关注本帖(如果有新回复会站内信通知您)
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 关注CVP公众号
扫一扫 浏览移动版扫码下载APP
随时选购服务
需求发布后1小时内收到服务商响应每个需求平均有10个服务商参与95%以上的需求得到了圆满解决所有需求不向雇主收取任何佣金双11电商狂欢 爆款服务超乎你想像
使用蓝牙耳机的连接断开触发报警的IOS软件开发
使用蓝牙耳机的连接断开触发报警的IOS软件开发
雇主预算:¥100.00
已收到 2 个服务商的文案稿件
有相似问题想解决?专业顾问来帮助您
通过猪八戒网实名认证,保证身份真实可靠
完成手机认证,保证能随时联系到服务商
该需求下的优秀交稿
TA的交稿:
您好,我们有七年的软件、网站开发经验,可以实现您的需求,欢迎选择我们为您提供比别家更专业的服务,QQ:,愿我们合作愉快!官方网站:http://www.chonour.c...
交易成功的需求
APP定制开发相关需求阅读(18028)
文章转自:/ctaodream/archive//3169962.html
一、服务端(也叫周边设备吧。。脑残的翻译)
1.实现类必须遵守协议&CBPeripheralManagerDelegate
2.需要的主要类有:
@property(strong,nonatomic)&CBPeripheralManager&*peripheraM
@property(strong,nonatomic) CBMutableCharacteristic *customerC
&@property (strong,nonatomic) CBMutableService *customerS
3.调用流程代码中有注释
ViewController.m
BlueToothDemo
Created by PSH_Chen_Tao on 7/3/13.
Copyright (c) 2013 wolfman. All rights reserved.
#import "ViewController.h"
static NSString *const kCharacteristicUUID = @"CCE62C0F--ADFA-C8FC7EA2EE90";
static NSString *const kServiceUUID = @"50BD367B-6B17-4E81-B6E9-FB";
@interface ViewController ()
@implementation ViewController
@synthesize peripheraM
@synthesize customerC
@synthesize customerS
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//初始化后会直接调用代理的
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral
peripheraManager = [[CBPeripheralManager alloc]initWithDelegate:self queue:nil];
[peripheraManager startAdvertising:nil];
-(void)setUp{
CBUUID *characteristicUUID = [CBUUID UUIDWithString:kCharacteristicUUID];
customerCharacteristic = [[CBMutableCharacteristic alloc]initWithType:characteristicUUID properties:CBCharacteristicPropertyNotify value:nil permissions:CBAttributePermissionsReadable];
CBUUID *serviceUUID = [CBUUID UUIDWithString:kServiceUUID];
customerService = [[CBMutableService alloc]initWithType:serviceUUID primary:YES];
[customerService setCharacteristics:@[characteristicUUID]];
//添加后就会调用代理的- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error
[peripheraManager addService:customerService];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
mark -- CBPeripheralManagerDelegate
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{
switch (peripheral.state) {
//在这里判断蓝牙设别的状态
当开启了则可调用
setUp方法(自定义)
case CBPeripheralManagerStatePoweredOn:
NSLog(@"powered on");
[self setUp];
case CBPeripheralManagerStatePoweredOff:
NSLog(@"powered off");
- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error{
if (error == nil) {
//添加服务后可以在此向外界发出通告 调用完这个方法后会调用代理的
//(void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(NSError *)error
[peripheraManager startAdvertising:@{CBAdvertisementDataLocalNameKey : @"Service",CBAdvertisementDataServiceUUIDsKey : [CBUUID UUIDWithString:kServiceUUID]}];
- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(NSError *)error{
NSLog(@"in peripheralManagerDidStartAdvertisiong:error");
二、客户端(也叫中心设备吧)
1.实现类要遵守协议&CBCentralManagerDelegate,CBPeripheralDelegate&
2.主要用到的类有&
@property(strong,nonatomic)CBCentralManager&*centralM
@property(strong,nonatomic)NSMutableData&*mutableD
@property(strong,nonatomic)CBPeripheral&*
&3.一般的流程
ViewController.m
BlueToothClient
Created by PSH_Chen_Tao on 7/3/13.
Copyright (c) 2013 wolfman. All rights reserved.
#import "ViewController.h"
static NSString *const kCharacteristicUUID = @"CCE62C0F--ADFA-C8FC7EA2EE90";
static NSString *const kServiceUUID = @"50BD367B-6B17-4E81-B6E9-FB";
@interface ViewController ()
@implementation ViewController
@synthesize centralM
@synthesize mutableD
@synthesize
- (void)viewDidLoad
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//初始化后会调用代理CBCentralManagerDelegate 的 - (void)centralManagerDidUpdateState:(CBCentralManager *)central
centralManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
mark -- CBCentralManagerDelegate
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
switch (central.state) {
//判断状态开始扫瞄周围设备 第一个参数为空则会扫瞄所有的可连接设备
//指定一个CBUUID对象 从而只扫瞄注册用指定服务的设备
//scanForPeripheralsWithServices方法调用完后会调用代理CBCentralManagerDelegate的
//- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI方法
case CBCentralManagerStatePoweredOn:
[centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:kServiceUUID]] options:@{CBCentralManagerScanOptionAllowDuplicatesKey : YES}];
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{
if (peripheral) {
self.peripheral =
//发现设备后即可连接该设备 调用完该方法后会调用代理CBCentralManagerDelegate的- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral表示连接上了设别
//如果不能连接会调用 - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error
[centralManager connectPeripheral:peripheral options:@{CBConnectPeripheralOptionNotifyOnConnectionKey : YES}];
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
NSLog(@"has connected");
[mutableData setLength:0];
self.peripheral.delegate =
//此时设备已经连接上了
你要做的就是找到该设备上的指定服务 调用完该方法后会调用代理CBPeripheralDelegate(现在开始调用另一个代理的方法了)的
//- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
[self.peripheral discoverServices:@[[CBUUID UUIDWithString:kServiceUUID]]];
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
//此时连接发生错误
NSLog(@"connected periphheral failed");
#pragma mark -- CBPeripheralDelegate
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error{
if (error==nil) {
//在这个方法中我们要查找到我们需要的服务
然后调用discoverCharacteristics方法查找我们需要的特性
//该discoverCharacteristics方法调用完后会调用代理CBPeripheralDelegate的
//- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
for (CBService *service in peripheral.services) {
if ([service.UUID isEqual:[CBUUID UUIDWithString:kServiceUUID]]) {
[peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:kCharacteristicUUID]] forService:service];
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{
if (error==nil) {
//在这个方法中我们要找到我们所需的服务的特性 然后调用setNotifyValue方法告知我们要监测这个服务特性的状态变化
//当setNotifyValue方法调用后调用代理CBPeripheralDelegate的- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
for (CBCharacteristic *characteristic in service.characteristics) {
if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:kCharacteristicUUID]]) {
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
if (error==nil) {
//调用下面的方法后 会调用到代理的- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
[peripheral readValueForCharacteristic:characteristic];
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
&原文链接:
阅读排行榜

我要回帖

更多关于 蓝牙鼠标自动断开 的文章

 

随机推荐