2 Star 7 Fork 2

manofit / GJLightBlueTooth

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

GJLightBlueTooth

GJLightBlueToothis a lightweight bluetooth library.

effect

效果图可以在我的github上看到:https://github.com/manofit/GJLightBlueTooth

demo

You can try to use GJLightBlueTooth in Demo.

project architecture

The architecture of project is: user ——> GJLightBlueTooth ——> CoreBlueTooth ——> GJLightBlueTooth ——> user.

There:

  • GJLightBlueTooth:is agency between user and system CoreBlueTooth, you can send data and set callback here.
  • GJLBTCentralManager: all the communication with system CoreBlueTooth is here, send data and get callback.

In Demo, you can see the class MyBLETool, this aim to seperate page and service, you can call it Device-Class. So we needn't to set callback block in ViewControllers.

how to use

You should init GJLightBlueTooth after create ViewController, self.BLE = [[GJLightBlueTooth alloc] init].

After get Characteristic, you'd better mate CBCharacteristic in device with Characteristic you get, then save it in local for writing and reading.

[self.BLE setBlockWhenDiscoverCharacteristics:^(CBPeripheral *peripheral, CBService *service, NSError *error) {
        strongify(self);
        for (CBCharacteristic *cha in service.characteristics){
            if ([cha.UUID.UUIDString isEqualToString:CharacteristicUUIDWrite]){
                self.writeCharacter = cha;
            }
        }
        //[[NSNotificationCenter defaultCenter] postNotificationName:@"DiscoverCharacteristics" object:service];
    }];

scan

[self.BLE scan]

stop scan

[self.BLE stopScan]

connect

[self.BLE connectWithPeripheral:peri]

cancel connect

[self.BLE cancelConnectWithPeripheral:peri]

read RSSI

[self.BLE readRSSIWithPeriperal:peri]

send command

[self.BLE sendDataToPeriperal:peri WriteCharacteristic:self.writeCharacter Command:command NSEncoding:encoding]

reconnect & cancel reconnect

[self.BLE addReconnectPeriphearal:peri];
[self.BLE deleteReconnectPeriphearal:peri];

If you like to keep a heartbeat with device, there is a new thread. you can set max concurrent operation count by yourself.

NSData *cmdData = [[NSString stringWithFormat:@"%@",command] dataUsingEncoding:encoding];
    
    NSOperation *opration = [NSBlockOperation blockOperationWithBlock:^{
        [peripheral writeValue:cmdData
            forCharacteristic:writeCharacteristics
                         type:CBCharacteristicWriteWithoutResponse];
        /*
         * you can set thread time interval.but the order while delay when there are a lot of orders.
         */
        //[NSThread sleepForTimeInterval:SleepTimeGap];
    }];
    
    [self.writeQueue addOperation:opration];

You can also set time interval between orders, but the orders will delay if you update heartbeat quickly.

warning

  1. In newest iOS system, wo can't get RSSI throungh peripheral.RSSI. Instead, you should get callback after using [peripheral readRSSI], so we can't show RSSI when discover a lot of deivces. So I created a category CBPeripheral+RSSI, then added a property named rssi by using Runtime.
char nameKey;

- (void)setRssi:(NSNumber *)rssi{
    objc_setAssociatedObject(self, &nameKey, rssi, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSNumber *)rssi{
    return objc_getAssociatedObject(self, &nameKey);
}
  1. In MyBLETool, you need to set callback GJLBTCentralManager working flow. In case of cycle retain, you'd better set weak-strong dance.
weakify(self);

[self.BLE setBlockWhenDiscoverCharacteristics:^(CBPeripheral *peripheral, CBService *service, NSError *error) {
        strongify(self);
        for (CBCharacteristic *cha in service.characteristics){
            if ([cha.UUID.UUIDString isEqualToString:CharacteristicUUIDWrite]){
                self.writeCharacter = cha;
            }
        }
        //[[NSNotificationCenter defaultCenter] postNotificationName:@"DiscoverCharacteristics" object:service];
    }];

GJLightBlueTooth

GJLightBlueTooth是一个轻量级蓝牙库。

demo

你可以在Demo中查看如何使用GJLightBlueTooth

project architecture

整个蓝牙库的架构是:用户 ——> GJLightBlueTooth ——> CoreBlueTooth ——> GJLightBlueTooth ——> 用户。

其中:

  • GJLightBlueTooth:相当于一个中介,架起来自页面用户的指令和系统CoreBlueTooth交互的桥梁,这里的交互包括向蓝牙设备发送指令和设置回调。
  • GJLBTCentralManager:所有与系统CoreBlueTooth的沟通都在这里进行,这里将指令发出去,也在这里获取回调,通过block回传。

而在Demo中,你还会看到MyBLETool这个类,这是为了将Demo项目中页面与业务分离而单独出来的一个类,可以理解为设备类。为了我们不需要在具体的页面中去设置回传的block。

how to use

在创建页面后,你应该初始化GJLightBlueTooth蓝牙工具:self.BLE = [[GJLightBlueTooth alloc] init]

在获取到Characteristic后,你应该根据实际读写的Characteristic匹配出设备上的CBCharacteristic,保存在本地,用于后面的写与读。

[self.BLE setBlockWhenDiscoverCharacteristics:^(CBPeripheral *peripheral, CBService *service, NSError *error) {
        strongify(self);
        for (CBCharacteristic *cha in service.characteristics){
            if ([cha.UUID.UUIDString isEqualToString:CharacteristicUUIDWrite]){
                self.writeCharacter = cha;
            }
        }
        //[[NSNotificationCenter defaultCenter] postNotificationName:@"DiscoverCharacteristics" object:service];
    }];

scan

[self.BLE scan]

stop scan

[self.BLE stopScan]

connect

[self.BLE connectWithPeripheral:peri]

cancel connect

[self.BLE cancelConnectWithPeripheral:peri]

read RSSI

[self.BLE readRSSIWithPeriperal:peri]

send command

[self.BLE sendDataToPeriperal:peri WriteCharacteristic:self.writeCharacter Command:command NSEncoding:encoding]

添加断开重连 & 取消断开重连

[self.BLE addReconnectPeriphearal:peri];
[self.BLE deleteReconnectPeriphearal:peri];

这里针对现在很多公司提出需要手机与设备有心跳的要求,开启了一个线程队列。该队列设置能够同时存在的指令数为3。

NSData *cmdData = [[NSString stringWithFormat:@"%@",command] dataUsingEncoding:encoding];
    
    NSOperation *opration = [NSBlockOperation blockOperationWithBlock:^{
        [peripheral writeValue:cmdData
            forCharacteristic:writeCharacteristics
                         type:CBCharacteristicWriteWithoutResponse];
        /*
         * you can set thread time interval.but the order while delay when there are a lot of orders.
         */
        //[NSThread sleepForTimeInterval:SleepTimeGap];
    }];
    
    [self.writeQueue addOperation:opration];

你也可以设置指令间隔时间,但是这样会造成因心跳刷新过快造成的延迟发送。

warning

  1. 在新版本的iOS中,已经不允许通过peripheral.RSSI来获取设备的信号量,必须在用[peripheral readRSSI]后,使用回调来获取。这就会造成在扫描到设备时候无法显示信号量。所以专门创建了一个分类CBPeripheral+RSSI,利用Runtime来动态给peripheral创建了一个rssi属性。
char nameKey;

- (void)setRssi:(NSNumber *)rssi{
    objc_setAssociatedObject(self, &nameKey, rssi, OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (NSNumber *)rssi{
    return objc_getAssociatedObject(self, &nameKey);
}
  1. 在MyBLETool中,需要设置GJLBTCentralManager回调流,这里为了防止循环引用,需要进行weak-strong dance。
weakify(self);

[self.BLE setBlockWhenDiscoverCharacteristics:^(CBPeripheral *peripheral, CBService *service, NSError *error) {
        strongify(self);
        for (CBCharacteristic *cha in service.characteristics){
            if ([cha.UUID.UUIDString isEqualToString:CharacteristicUUIDWrite]){
                self.writeCharacter = cha;
            }
        }
        //[[NSNotificationCenter defaultCenter] postNotificationName:@"DiscoverCharacteristics" object:service];
    }];

空文件

简介

A LightWeight Bluetooth Development Library 轻量级蓝牙开发库 展开 收起
Objective-C
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Objective-C
1
https://gitee.com/cowa/GJLightBlueTooth.git
git@gitee.com:cowa/GJLightBlueTooth.git
cowa
GJLightBlueTooth
GJLightBlueTooth
master

搜索帮助