蓝牙编程
来源:互联网 发布:数据库实训考试 编辑:程序博客网 时间:2024/05/20 20:05
iOS的蓝牙框架是支持蓝牙4.0协议的。
理解iOS CoreBluetooth两个很重要的概念,Central 和 Periperal Devices
这两个概念可以用传统的模式client-server来理解,central意思是中心,其作用类似server,periperal就是外设,一般携带有数据,我们需要去其中获取数据,下图是苹果官网的例子,peripheral是心跳仪,按期作用,我们去这个外设中取心跳数据,则心跳仪的作用就类似server了,我们的手机去心跳仪中获取数据,类似client。
Peripheral如何让central知道它的存在呢? peripheral.比如上图的心跳仪,通过不断广播自己的存在,并且在广播过程中附带广告包(advertising packet),不知道这样翻译是否正确,不对请指正一下。如同好像有个设备在喊,Here~我是心跳仪器,我是心跳仪器,这是我提供的服务(广告包)。Central发现有个设备在呐喊,就说:“那我看看需不需要你提供服务,拿来我看看”。这样你就发现了这个设备,并且获取到它提供的服务。
介绍完这些概念,我们来看看实际代码应该如何填写.这里对于我的蓝牙小票机来编写的,因为我要对蓝牙小票机器进行写操作,手机是搜索peripharal的,所以手机为central,小票机为peripharal 1.首先导入这个框架 2.该框架有主要有几个类值得我们注意,CBCentralManager,也就是我们之前提到的Central,可以用来发现外设的。
#
import
<corebluetooth corebluetooth.h=
""
>
@interface
ViewController () <cbcentralmanagerdelegate>
@property
(nonatomic, retain) CBCentralManager *centralManager;
@end
@implementation
ViewController
- (
void
)viewDidLoad {
[
super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
/*
Invoked whenever the central manager's state is updated.
*/
- (
void
)centralManagerDidUpdateState:(CBCentralManager *)central
{
NSString * state = nil;
switch
([central state])
{
case
CBCentralManagerStateUnsupported:
state = @
"The platform/hardware doesn't support Bluetooth Low Energy."
;
break
;
case
CBCentralManagerStateUnauthorized:
state = @
"The app is not authorized to use Bluetooth Low Energy."
;
break
;
case
CBCentralManagerStatePoweredOff:
state = @
"Bluetooth is currently powered off."
;
break
;
case
CBCentralManagerStatePoweredOn:
state = @
"work"
;
//开启扫描外设 services为nil表示扫描所有设备
[self.centralManagerscanForPeripheralsWithServices:niloptions:nil];
break
;
case
CBCentralManagerStateUnknown:
default
:
;
}
NSLog(@
"Central manager state: %@"
, state);
}
[self.centralManager scanForPeripheralsWithServices:@[[CBUUID UUIDWithString:@
"FFE0"
]] options:nil];
- (
void
)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
{
static
int
i =
0
;
NSString *str = [NSString stringWithFormat:@
"Did discover peripheral. peripheral: %@ rssi: %@, UUID: %@ advertisementData: %@ "
, peripheral, RSSI, peripheral.UUID, advertisementData];
NSLog(@
"%@"
,str);
[self.discoverdPeriparals addObject:peripheral];
}
4.连接发现的外设
- (IBAction)connect:(id)sender {
[self.centralManager connectPeripheral:[self.discoverdPeriparals firstObject] options:nil];
}
?/*
Invoked whenever a connection is succesfully created with the peripheral.
Discover available services on the peripheral
*/
- (
void
)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral
{
NSLog(@
"Did connect to peripheral: %@"
, peripheral);
peripheral.delegate = self;
[central stopScan];
[peripheral discoverServices:nil];
// 提供所有的服务传入nil就为所有的服务
}
<span style=
"font-size:18px;"
>[peripheral discoverServices:@[[CBUUID UUIDWithString:@
"FFE0"
]]];</span>
- (
void
)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{
if
(error)
{
NSLog(@
"Discovered services for %@ with error: %@"
, peripheral.name, [error localizedDescription]);
return
;
}
for
(CBService *service in peripheral.services)
{
NSLog(@
"Service found with UUID: %@"
, service.UUID);
if
([service.UUID isEqual:[CBUUID UUIDWithString:@
"FFE0"
]])
{
[peripheral discoverCharacteristics:nil forService:service];
}
}
}
[peripheral discoverCharacteristics:nil forService:service];
- (
void
)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error
{
if
(error)
{
NSLog(@
"Discovered characteristics for %@ with error: %@"
, service.UUID, [error localizedDescription]);
return
;
}
for
(CBCharacteristic * characteristic in service.characteristics)
{
DLog(@
"%@"
,<span style=
"font-family: Arial, Helvetica, sans-serif;"
>characteristic</span>);
if
( [characteristic.UUID isEqual:[CBUUID UUIDWithString:@
"FFE1"
]])
{
self.p = peripheral;
self.c = characteristic;
//read
//[testPeripheral readValueForCharacteristic:characteristic];
NSLog(@
"Found a Device Manufacturer Name Characteristic - Read manufacturer name"
);
}
}
}
- (
void
)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSString *RStr = @
"2764"
;
NSMutableString *str = [NSMutableString
new
];
[str appendFormat:@
"%c"
,
28
];
[str appendFormat:@
"%c"
,
33
];
[str appendFormat:@
"%c"
,
8
];
[self.p writeValue:[str dataUsingEncoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000)] forCharacteristic:self.c type:CBCharacteristicWriteWithResponse];
RStr = @
"吴水凤 你好呀!!!\n\n\n\n\n\n\n\n"
;
[self.p writeValue:[RStr dataUsingEncoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000)] forCharacteristic:self.c type:CBCharacteristicWriteWithResponse];
}
补充: 如果该特征是可以读特征,你可以对该特征进行订阅。
[peripheral setNotifyValue:YES forCharacteristic:interestingCharacteristic];
该方法回调peripheral:didUpdateValueForCharacteristic:error:方法
- 蓝牙编程
- 蓝牙编程
- 蓝牙编程
- 蓝牙编程
- [蓝牙]手机蓝牙编程简介
- 蓝牙编程-设备检索.
- 蓝牙编程下载
- 蓝牙编程-设备检索.
- 蓝牙编程-设备检索
- 蓝牙C语言编程
- linux蓝牙编程 bluez
- 手机蓝牙编程简介
- android平台蓝牙编程
- android平台蓝牙编程
- android平台蓝牙编程
- android蓝牙编程
- Android 蓝牙编程基础
- Android 蓝牙编程基础
- 一个登录页面的测试用例(借鉴他人的,方便查阅)
- oracle中char与varchar2的区别
- lucene更新索引失败
- iscroll5+appframework3.0上拉加载下拉刷新
- ArcGIS Engine加载数据
- 蓝牙编程
- linux shell 与sqlplus交互
- 头文件sys/types.h <sys/stat.h>
- Vericut04_多工序仿真
- CXF webService 对象属性值为null或空时生成xml缺少该字段节点 解决办法
- android: WheelView组件(滑轮组件)的应用!
- 在 5 分钟内使用 Node-RED 构建实时的聊天应用程序
- Android日志(一)
- AE添加SDE数据库