在这里插入代码片
## RN蓝牙beacon包的发送 ios部分
我在写公司的项目时有一个功能是需要蓝牙beacon包的收发
期间碰到过许多的问题
比如之前一版我将init只做初始化用 在发送包方法里再赋值UUID等参数导致项目使用一段时间后突然出现点击按钮关闭发包功能后再用init启用就发不了beacon包了(主要是想利用里面的关闭蓝牙时帮忙打开蓝牙的权限判断)仔细研究代码后发现是 NSDictionary *peripheralData = [region peripheralDataWithMeasuredPower:@(1)];
创建出来的结果是空的what??
这个东西怎么回事??而且每次登录初始化后再打开就没事
跟着追溯到了region 发现这个也是空的
在这里卡的时间特别久一直没法定位为什么是空的
最后还是想到从 region创建方法入手
最后发现这里面这个创建方法是会返回一个空值的当你的uuid为nul l的时候
难道我之前正常发送的beacon都是卡着时间在蓝牙状态在0-5之间的时间把值赋上的吗???
最后采用的方法是将init方法改成传参的在init阶段就将需要的参数赋值上去,试了下果然成功了并且按钮可以正常开关
上代码(android 端收发,iOS端收发均已完成待更新)
下面展示一些 内联代码片
。
##1.o c类 sendBeacon.m
//
// SendBeacon.m
// yunsiapp-beta
//
// Created by 刘先宇 on 2020/6/22.
// Copyright © 2020 Facebook. All rights reserved.
//
#import "SendBeacon.h"
#import <CoreBluetooth/CoreBluetooth.h>
#import <CoreLocation/CoreLocation.h>
@interface SendBeacon ()
<
CBPeripheralManagerDelegate
>
@property (nonatomic, strong) CBPeripheralManager *peripheralManager;
@property (nonatomic, strong) NSNumber *major;
@property (nonatomic, strong) NSNumber *minor;
@property (nonatomic, strong) NSString *UUID;
@end
@implementation SendBeacon
- (instancetype)initWithUUID:(NSString *)UUID major:(nonnull NSNumber *)major minor:(nonnull NSNumber *)minor
{
self = [super init];
if (self) {
self.UUID=UUID;
self.major = major;
self.minor = minor;
// self.UUID=@"FDA50693-F091-4129-937D-2A189A86D844";
// self.peripheralManager= [[CBPeripheralManager alloc] initWithDelegate:self queue:nil options:nil];
/**实例化蓝牙外围设备*/
if(!self.peripheralManager){
self.peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
NSLog(@"初始化");
}
}
return self;
}
- (void)setConfigWithMajor:(nonnull NSNumber *)major minor:(nonnull NSNumber *)minor UUID:(NSString*)UUID {
self.major = major;
self.minor = minor;
self.UUID=UUID;
NSLog(@"蓝牙相关配置成功");
}
- (void)startAdvertiseWithMajor:(NSNumber *)major minor:(NSNumber *)minor UUID:(NSString*)UUID {
if(self.peripheralManager.state != CBPeripheralManagerStatePoweredOn)
{
/**设备蓝牙未开启*/
NSLog(@"蓝牙未开启或不支持,");
NSLog(@"%ld", (long)self.peripheralManager.state);
return;
}
// NSLog(@"蓝牙开启");
self.major = major;
self.minor = minor;
self.UUID=UUID;
NSLog(@"蓝牙相关配置成功");
// [self.peripheralManager startAdvertising];
// [self.peripheralManager stopAdvertising];
/**开始广播信号*/
[self advertiseDevice:[self createBeaconRegion]];
}
- (void)stopAdvertise {
[self.peripheralManager stopAdvertising];
}
#pragma mark ----------- 开始通过蓝牙广播信标信号 -------------
- (void)advertiseDevice:(CLBeaconRegion *)region {
/**创建需要广播的信标数据*/
NSDictionary *peripheralData = [region peripheralDataWithMeasuredPower:@(1)];
NSLog(@"peripheralData %@", peripheralData);
NSLog(@"region %@",region);
if(peripheralData)
{
/**开始广播*/
[self.peripheralManager startAdvertising:peripheralData];
}
}
#pragma mark ----------- 创建信标区域 -------------
- (CLBeaconRegion *)createBeaconRegion {
/**uuid 用来标识公司*/
NSUUID *uuid = [[NSUUID alloc]initWithUUIDString:self.UUID];
// FDA50693-F091-4129-937D-2A189A86D844
/**主要值,用来识别一组相关联的 beacon,例如在连锁超市的场景中,每个分店的 beacon 应该拥有同样的 major。*/
// NSNumber *major = @(1);
/**次要值,则用来区分某个特定的 beacon*/
// NSNumber *minor = @(10001);
/**实例化信标区域*/
CLBeaconRegion *region;
if (@available(iOS 13.0, *)) {
NSLog(@"ios13");
region = [[CLBeaconRegion alloc]initWithUUID:uuid major:[self.major shortValue] minor:[self.minor shortValue] identifier:@"com.smarchit.allinone"];
} else {
NSLog(@"ios13以下");
region = [[CLBeaconRegion alloc]initWithProximityUUID:uuid major:[self.major shortValue] minor:[self.minor shortValue] identifier:@"com.smarchit.allinone"];
}
return region;
}
#pragma mark ----------- CBPeripheralManagerDelegate -------------
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral
{
if(self.peripheralManager.state != CBPeripheralManagerStatePoweredOn)
{
/**设备蓝牙未开启*/
NSLog(@"蓝牙未开启或不支持,");
NSLog(@"%ld", (long)self.peripheralManager.state);
return;
}
NSLog(@"%ld", (long)self.peripheralManager.state);
NSLog(@"蓝牙开启");
// [self.peripheralManager startAdvertising];
// [self.peripheralManager stopAdvertising];
/**开始广播信号*/
NSLog(@"%@", [self createBeaconRegion]);
[self advertiseDevice:[self createBeaconRegion]];
}
- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(nullable NSError *)error {
NSLog(@"%@",error);
if (!error) {
NSLog(@"开始广播信标信号");
}
}
@end
##2. sendBeacon.h
//
// SendBeacon.h
// yunsiapp-beta
//
// Created by 刘先宇 on 2020/6/22.
// Copyright © 2020 Facebook. All rights reserved.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface SendBeacon : NSObject
/**初始化*/
- (instancetype)initWithUUID:(NSString *)UUID major:(nonnull NSNumber *)major minor:(nonnull NSNumber *)minor;
/**开始广播信标信号*/
- (void)startAdvertiseWithMajor:(NSNumber *)major minor:(NSNumber *)minor UUID:(NSString*)UUID;
/**初始化相关信息*/
- (void)setConfigWithMajor:(nonnull NSNumber *)major minor:(nonnull NSNumber *)minor UUID:(NSString*)UUID;
/**停止广播*/
- (void)stopAdvertise;
@end
NS_ASSUME_NONNULL_END
##3. RN桥接
RCT_EXPORT_METHOD(initBlueTooh:(NSString*)UUID major:(nonnull NSNumber *)major minor:(nonnull NSNumber *)minor)
{
// NSLog(@"初始化");
self.iBeacon = [[SendBeacon alloc]initWithUUID:UUID major:major minor:minor];
}
RCT_EXPORT_METHOD(startBroadcast:(NSString*)UUID major:(nonnull NSNumber *)major minor:(nonnull NSNumber *)minor)
{
NSLog(@"配置major,minor");
[self.iBeacon startAdvertiseWithMajor:major minor:minor UUID:UUID];
}
RCT_EXPORT_METHOD(stopBroadcast)
{
NSLog(@"停止发广播");
[self.iBeacon stopAdvertise];
}
##4. RN端调用
const RNBridge = NativeModules.RNBridge;
初始化并发送beacon包
RNBridge.initBlueTooh(UUID,major,minor)(还需要其他参数可以照着改并不复杂吧)
已经初始化发送beacon包
RNBridge.startBroadcast(UUID,major,minor)
停止发送
RNBridge.stopBroadcast()