后臺(tái)運(yùn)行藍(lán)牙服務(wù)http://www.tuicool.com/articles/n22ea2V
iBeacon學(xué)習(xí)http://www.reibang.com/p/a502eae7b50a
iOS藍(lán)牙開(kāi)發(fā)之iBeaconhttp://www.reibang.com/p/1207c8727889
iOS之藍(lán)牙4.0 BLE相關(guān)http://www.reibang.com/p/0a80e10bf0fc
iBeacon--- 是蘋(píng)果公司2013年9月發(fā)布的移動(dòng)設(shè)備用OS(iOS7)上配備的新功能恳蹲。其工作方式是伶选,配備有 低功耗藍(lán)牙(BLE)通信功能的設(shè)備使用BLE技術(shù)向周?chē)l(fā)送自己特有的ID,接收到該ID的應(yīng)用軟件會(huì)根據(jù)該ID采取一些行動(dòng)。
監(jiān)聽(tīng)ibeacon設(shè)備:根據(jù)proximityUUID儿普、major 和 minor不同來(lái)區(qū)分不同的用戶(hù)再請(qǐng)求協(xié)議拿到用戶(hù)信息在把ID信息顯示出來(lái)再根據(jù)ID查找具體的信息內(nèi)容
這個(gè)場(chǎng)景是指當(dāng)你走到商家門(mén)前時(shí),你開(kāi)啟藍(lán)牙超燃,并用微信搖一搖周邊芍瑞,那么你可以搖到優(yōu)惠券和廣告等信息。這在O2O領(lǐng)域是應(yīng)用最廣泛的皮胡。
微信搖一搖周邊是基于iBeacon來(lái)實(shí)現(xiàn)的痴颊。
首先要有一個(gè)iBeacon硬件(newbeacon)和一部藍(lán)牙4.0以及ios7以上或安卓4.3以上系統(tǒng)的手機(jī)。(會(huì)有一個(gè)專(zhuān)門(mén)部署iBeacon基站的APP)
1.申請(qǐng)微信公眾號(hào)胸囱。服務(wù)提供者向微信后臺(tái)申請(qǐng)服務(wù)祷舀,微信后臺(tái)生成一個(gè)iBeaconID,并將其映射到服務(wù)提供者提供的服務(wù),再將iBeaconID告訴服務(wù)提供者烹笔;
2.服務(wù)提供者把第一步拿到的iBeaconID設(shè)置到iBeacon設(shè)備上裳扯,讓iBeacon設(shè)備廣播該iBeaconID;
3.用戶(hù)在該iBeacon設(shè)備的信號(hào)范圍內(nèi)打開(kāi)微信搖一搖周邊谤职,微信App拿到該iBeaconID饰豺;
4.微信通過(guò)第三步拿到的iBeaconID,向微信后臺(tái)拉取相應(yīng)的服務(wù)允蜈,展示在搖出來(lái)的結(jié)果上冤吨;
5.用戶(hù)點(diǎn)擊搖出來(lái)結(jié)果蒿柳,在微信內(nèi)嵌的瀏覽器上,會(huì)帶上用戶(hù)信息跳轉(zhuǎn)到服務(wù)提供者在第一步申請(qǐng)服務(wù)時(shí)填的url漩蟆,進(jìn)入應(yīng)用頁(yè)面垒探。
CoreBluetooth
在CoreBluetooth中,藍(lán)牙傳輸都分為兩個(gè)部分:
外圍設(shè)備CBPeripheral:
負(fù)責(zé)發(fā)布并廣播服務(wù)怠李,告訴周?chē)闹醒朐O(shè)備它的可用服務(wù)和特征圾叼,類(lèi)似于網(wǎng)絡(luò)通信中的服務(wù)端。
中央設(shè)備CBCentral:
負(fù)責(zé)和外圍設(shè)備建立連接捺癞,一旦連接成功就可以使用外圍設(shè)備的服務(wù)和特征夷蚊,類(lèi)似于網(wǎng)絡(luò)通信中的客戶(hù)端
//
// OneViewController.m
// text
//
// Created by Monster DD Imac on 17/3/11.
// Copyright ? 2017年 Monster DD Imac. All rights reserved.
//
#import "OneViewController.h"
#import <CoreBluetooth/CoreBluetooth.h>
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>
@interface OneViewController ()<CBCentralManagerDelegate,CBPeripheralDelegate>
@property(nonatomic,strong) CBCentralManager *centralManager;
@property(nonatomic,strong) CBPeripheral *peripheral;
@end
@implementation OneViewController{
UILabel * lab;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor grayColor];
lab = [UILabel new];
lab.backgroundColor = [UIColor whiteColor];
lab.frame = CGRectMake(20, 100, 280, 30);
[self.view addSubview:lab];
lab.text = @"準(zhǔn)備鏈接。髓介。惕鼓。";
[self initBluetooth];
}
- (void)initBluetooth{
_centralManager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];//創(chuàng)建CBCentralManager對(duì)象
}
- (void)scanBluetooth
{
NSLog(@"BluetoothBase scanBluetooth");
//CBCentralManagerScanOptionAllowDuplicatesKey值為 No,表示不重復(fù)掃描已發(fā)現(xiàn)的設(shè)備
NSDictionary *optionDic = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:CBCentralManagerScanOptionAllowDuplicatesKey];
[_centralManager scanForPeripheralsWithServices:nil options:optionDic];//如果你將第一個(gè)參數(shù)設(shè)置為nil唐础,Central Manager就會(huì)開(kāi)始尋找所有的服務(wù)箱歧。
}
- (void)stopScanBluetooth
{
[self.centralManager stopScan];
NSLog(@"BluetoothBase stopScanBluetooth,已經(jīng)連接外設(shè)停止掃描或者手動(dòng)停止掃描");
}
- (void)connectPeripheral:(CBPeripheral *)peripheral
{
[self.centralManager connectPeripheral:peripheral options:nil];
self.peripheral = peripheral;
peripheral.delegate = self; //連接時(shí)設(shè)置代理
}
#pragma CBCentralManagerDelegate
- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
NSLog(@"centralManagerDidUpdateState:%ld",(long)central.state);
switch (central.state) {
case CBCentralManagerStateUnknown:
NSLog(@"CBCentralManagerStateUnknown");
break;
case CBCentralManagerStateResetting:
NSLog(@"CBCentralManagerStateResetting");
break;
case CBCentralManagerStateUnsupported:
NSLog(@"CBCentralManagerStateUnsupported");
break;
case CBCentralManagerStateUnauthorized:
NSLog(@"CBCentralManagerStateUnauthorized");
break;
case CBCentralManagerStatePoweredOff:
NSLog(@"CBCentralManagerStatePoweredOff");
break;
case CBCentralManagerStatePoweredOn:
[self scanBluetooth]; //很重要彻犁,當(dāng)藍(lán)牙處于打開(kāi)狀態(tài)叫胁,開(kāi)始掃描。
break;
default:
NSLog(@"藍(lán)牙未工作在正確狀態(tài)");
break;
}
}
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI //找到外設(shè)的委托
{
NSLog(@"-------------------------------------");
NSMutableString* nsmstring=[NSMutableString stringWithString:@"\n"];
[nsmstring appendString:@"----發(fā)現(xiàn)外設(shè)----\n"];
[nsmstring appendString:@"Peripheral Info:\n"];
[nsmstring appendFormat:@"NAME: %@\n",peripheral.name];
[nsmstring appendFormat:@"UUID(identifier): %@\n",peripheral.identifier];
[nsmstring appendFormat:@"RSSI: %@\n",RSSI];
[nsmstring appendFormat:@"adverisement:%@\n",advertisementData];
NSLog(@"%@",nsmstring);
NSLog(@"-------------------------------------");
if ([peripheral.name isEqualToString:@"ky_0002"]) {
[self connectPeripheral:peripheral];
[self stopScanBluetooth];
}
}
-(void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral//連接外設(shè)成功的委托
{
NSLog(@"%@連接成功",peripheral.name);
lab.text = [NSString stringWithFormat:@"%@連接成功",peripheral.name];
//查找該周邊設(shè)備的服務(wù)
[peripheral discoverServices:nil];
}
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error//外設(shè)連接失敗的委托
{
NSLog(@"%@連接斷開(kāi)",peripheral.name);
}
-(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error//斷開(kāi)外設(shè)的委托
{
NSLog(@"斷開(kāi)外設(shè)");
lab.text = [NSString stringWithFormat:@"%@斷開(kāi)連接",peripheral.name];
//重連
[self connectPeripheral:peripheral];
//本地通知
[self addLocalNotification];
}
#pragma CBPeripheralDelegate
// 發(fā)現(xiàn)外設(shè)的服務(wù)后調(diào)用的方法
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(nullable NSError *)error{
NSLog(@"發(fā)現(xiàn)外圍設(shè)備的服務(wù)");
if (error)
{
NSLog(@"%s, line = %d, error = %@", __FUNCTION__, __LINE__, error.localizedDescription);
return;
}
for (CBService *service in peripheral.services)
{
NSLog(@"%@",service);
NSLog(@"%@",service.UUID);
// 發(fā)現(xiàn)服務(wù)后,讓設(shè)備再發(fā)現(xiàn)服務(wù)內(nèi)部的特征們
[peripheral discoverCharacteristics:nil forService:service];
}
}
//最終找到特征會(huì)自動(dòng)執(zhí)行這個(gè)回調(diào)
- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(nullable NSError *)error{
NSLog(@"發(fā)現(xiàn)外圍設(shè)備的特征");
if (error)
{
NSLog(@"%s, line = %d, %@", __FUNCTION__, __LINE__, [error description]);
return;
}
for (CBCharacteristic *chara in service.characteristics)
{
NSLog(@"%@",chara);
NSLog(@"%@",chara.UUID);
}
}
#pragma addLocalNotification
- (void)addLocalNotification {
// 1.創(chuàng)建一個(gè)本地通知
UILocalNotification *localNote = [[UILocalNotification alloc] init];
// 1.1.設(shè)置通知發(fā)出的時(shí)間
localNote.fireDate = [NSDate dateWithTimeIntervalSinceNow:1];
// 1.2.設(shè)置通知內(nèi)容
localNote.alertBody = @"這是一個(gè)推送:你的名片丟失了";
// 1.3.設(shè)置鎖屏?xí)r,字體下方顯示的一個(gè)文字
localNote.alertAction = @"趕緊找一找!!!!!";
localNote.hasAction = YES;
// 1.5.設(shè)置通過(guò)到來(lái)的聲音
localNote.soundName = @"alarm_device1.wav";
// 1.6.設(shè)置應(yīng)用圖標(biāo)左上角顯示的數(shù)字
localNote.applicationIconBadgeNumber = 999;
// 1.7.設(shè)置一些額外的信息
localNote.userInfo = @{@"qq" : @"704711253", @"msg" : @"success"};
// 2.執(zhí)行通知
[[UIApplication sharedApplication] scheduleLocalNotification:localNote];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end