iOS连接蓝牙

作者: Billy_W | 来源:发表于2016-11-15 14:32 被阅读237次

首先从targets------>Build Phases------>导入corebluetooth.framework的框架。

然后在ViewController.h的文件中添加两个协议:CBCentralManagerDelegate和CBPeripheralDelagate具体代码如下:

#import

@protocolCBCentralManagerDelegate;

@protocolCBPeripheralDelagate;

@interfaceViewController : UIViewController

@end

然后在ViewController.m文件中导入

#import"CoreBluetooth/CoreBluetooth.h"蓝牙框架的头文件,并且实现继承协议,和定义蓝牙连接的指针

#import"ViewController.h"

#import"CoreBluetooth/CoreBluetooth.h"

@interfaceViewController ()

@propertyCBCentralManager*manager;

@propertyNSMutableArray*peripherals;

@propertyCBPeripheral*peripheral;

@propertyCBCharacteristic*characteristic;

@propertyNSString*data;

@end

这些设置完毕之后开始连接蓝牙代码:在@implementationViewController和@end中实现对象方法

(1)创建一个中心;创建中心是蓝牙连接的开始,如果想要添加一个按钮控制蓝牙的话,可以设置按钮点击触发中心创建,我是把它放在了viewDidLoad里

- (void)viewDidLoad {

[superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

//1环境搭好(.h和.m文件的参数/协议/框架)创建一个中心

_manager= [[CBCentralManageralloc]initWithDelegate:selfqueue:nil];

self.manager.delegate=self;

//创建数组来管理外设

self.peripherals= [NSMutableArrayarray];

}

(2)在连接蓝牙前先检查中心的蓝牙是否打开(比如,针对ipad的app开发就是检查你的ipad的蓝牙是否打开),确认蓝牙打开后开始扫描蓝牙。

-(void) centralManagerDidUpdateState:(CBCentralManager*)central{

switch(central.state){

caseCBCentralManagerStatePoweredOn:

//扫描周边蓝牙外设.

//CBCentralManagerScanOptionAllowDuplicatesKey为true表示允许扫到重名,false表示不扫描重名的。

NSLog(@"蓝牙已打开,准备扫描外设");

[_managerscanForPeripheralsWithServices:niloptions:nil];//扫描语句:写nil表示扫描所有蓝牙外设,如果传上面的kServiceUUID,那么只能扫描出FFEO这个服务的外设

break;

caseCBCentralManagerStateUnauthorized:

NSLog(@"这个应用程序是无权使用蓝牙低功耗");

break;

caseCBCentralManagerStatePoweredOff:

NSLog(@"蓝牙目前已关闭");

break;

default:

break;

}

(3)成功扫描到了蓝牙会自动进入:didDiscoverPeripheral这个函数。peripheral.name和RSSI是你所扫描到的蓝牙的名字和距离,这个函数里面可以看到你所搜索到的蓝牙的信息,我是只连接特定的蓝牙:HC-08。这个函数有多少个蓝牙就会被调用几次。

-(void)centralManager:(CBCentralManager*)centraldidDiscoverPeripheral:(CBPeripheral*)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber*)RSSI{

if([peripheral.nameisEqualToString:@"HC-08"]) {//只连接HC-08的蓝牙

//尝试着连接蓝牙

self.manager.delegate=self;//?委托?

NSLog(@"尝试连接蓝牙:%@", peripheral.name);

self.peripheral=peripheral;//useful

[self.managerconnectPeripheral:peripheraloptions:nil];

}

(4)检测蓝牙是否成功连接:如果你的蓝牙板子是有指示灯的话,你看指示灯就可以啦。在程序里面蓝牙已经成功连接的话,会自动进入didConnectPeripheral。蓝牙成功连接之后,蓝牙停止扫描。

-(void)centralManager:(CBCentralManager*)central didConnectPeripheral:(CBPeripheral*)peripheral{

//检测是否连接到设备

NSLog(@"did connect to peripheral:%@", peripheral);

//停止扫描

[_managerstopScan];

//发现服务

self.peripheral= peripheral;

self.peripheral.delegate=self;

[self.peripheraldiscoverServices:nil];

}

如果程序成功执行会成功   执行NSLog(@"did connect to peripheral:%@", peripheral);后台可以看到已经连上的蓝牙的信息

//如果蓝牙没有连接会调用此函数,错误也会在后台显示

-(void)centralManager:(CBCentralManager*)central didDisconnectPeripheral:(CBPeripheral*)peripheral error:(NSError*)error{

NSLog(@"did not  connect to peripheral:%@", error);

}

相关文章

  • iOS蓝牙开发(三):App作为外设被连接的实现

    iOS蓝牙开发(三):App作为外设被连接的实现 iOS蓝牙开发(三):App作为外设被连接的实现

  • iOS蓝牙的开发专题

    iOS蓝牙的开发专题 最近做一个iOS蓝牙设备的项目,需要用iOS连接外部蓝牙设备,就对iOS蓝牙相关技术做了一些...

  • iOS蓝牙总结

    ios中蓝牙的几种实现 ios中提供了4个框架用于实现蓝牙连接GameKit.framework只能用于ios设备...

  • iOS-蓝牙(CoreBlueTooth)

    iOS蓝牙须知 iOS提供了4个框架用于实现蓝牙连接 1.gameKit.framework(用法简单) 只能用于...

  • iOS连接蓝牙设备后95秒断开连接

    公司蓝牙设备,部分设备在iOS端,连接蓝牙设备后95秒断开连接, 获取到的断开连接error为未知错误 code ...

  • iOS 关于蓝牙通信的4种框架

    iOS中蓝牙的实现方案 iOS中提供了4个框架用于实现蓝牙连接 一、GameKit.framework(用法简单)...

  • iOS中的蓝牙

    iOS中的蓝牙 概述 iOS中提供了4个框架用于实现蓝牙连接 1.GameKit.framework(用法简单)只...

  • 重点 (三十一) : 蓝牙/传感器

    iOS中蓝牙的实现方案 iOS中提供了4个框架用于实现蓝牙连接 GameKit.framework(用法简单) 只...

  • 蓝牙

    iOS中蓝牙的实现方案iOS中提供了4个框架用于实现蓝牙连接GameKit.framework(用法简单)只能用于...

  • CoreBluetooth

    iOS-BLE蓝牙开发持续更新 - 简书 蓝牙打印小票 一个第三方 IOS BLE4.0蓝牙和外设连接和收发数据的...

网友评论

    本文标题:iOS连接蓝牙

    本文链接:https://www.haomeiwen.com/subject/uozkpttx.html