All Projects → manofit → Gjlightbluetooth

manofit / Gjlightbluetooth

自己封装的基于CoreBluetooth的蓝牙开发库,内附CoreBluetooth使用demo。数据传递基本上使用Block,还使用了Runtime等技术。

Programming Languages

swift
15916 projects
swift4
162 projects

Projects that are alternatives of or similar to Gjlightbluetooth

bluenet
Bluenet is the in-house firmware on Crownstone hardware. Functions: switching, dimming, energy monitoring, presence detection, indoor localization, switchcraft.
Stars: ✭ 79 (+58%)
Mutual labels:  bluetooth, bluetooth-low-energy
Blynk Library
Blynk library for embedded hardware. Works with Arduino, ESP8266, Raspberry Pi, Intel Edison/Galileo, LinkIt ONE, Particle Core/Photon, Energia, ARM mbed, etc.
Stars: ✭ 3,305 (+6510%)
Mutual labels:  bluetooth-low-energy, bluetooth
bluer
BlueR — Official BlueZ Bindings for Rust
Stars: ✭ 43 (-14%)
Mutual labels:  bluetooth, bluetooth-low-energy
IoT-iBeacon
An Ionic app for indoor localization and navigation using BLE iBeacons.
Stars: ✭ 39 (-22%)
Mutual labels:  bluetooth, bluetooth-low-energy
Gort
Command Line Interface (CLI) for RobotOps
Stars: ✭ 425 (+750%)
Mutual labels:  bluetooth-low-energy, bluetooth
JDY-08
JDY-08 Bluetooth transparent transmission module, with resource for KiCAD
Stars: ✭ 48 (-4%)
Mutual labels:  bluetooth, bluetooth-low-energy
Ble examples
Additional examples to compliment TI's Bluetooth Low Energy Stack offerings.
Stars: ✭ 289 (+478%)
Mutual labels:  demo, bluetooth
deskbluez
Connects to a low energy actuator system via bluetooth and allows remote control via command line or internal managed interface.
Stars: ✭ 56 (+12%)
Mutual labels:  bluetooth, bluetooth-low-energy
Bleak
Bluetooth Low Energy platform Agnostic Klient for Python
Stars: ✭ 415 (+730%)
Mutual labels:  bluetooth-low-energy, bluetooth
Ios Pods Dfu Library
OTA DFU Library for Mac and iOS, compatible with nRF5x SoCs
Stars: ✭ 349 (+598%)
Mutual labels:  bluetooth-low-energy, bluetooth
IOS-DFU-Library
OTA DFU Library for Mac and iOS, compatible with nRF5x SoCs
Stars: ✭ 400 (+700%)
Mutual labels:  bluetooth, bluetooth-low-energy
Uart Smartwatch
Android App, Firmware and Circuit for a DIY Smartwatch working with Bluetooth Low Energy
Stars: ✭ 30 (-40%)
Mutual labels:  bluetooth-low-energy, bluetooth
pirowflo
All-in-one data interface for your Waterrower S4 Monitor or Smartrow
Stars: ✭ 73 (+46%)
Mutual labels:  bluetooth, bluetooth-low-energy
cannon-bluetooth-remote
Python script to emulate Canon's BR-E1 remote
Stars: ✭ 22 (-56%)
Mutual labels:  bluetooth, bluetooth-low-energy
rpi3-bluetooth
Raspberry Pi 3 on-board Bluetooth example
Stars: ✭ 17 (-66%)
Mutual labels:  bluetooth, bluetooth-low-energy
pyscrlink
Scratch-link for Linux written in python
Stars: ✭ 88 (+76%)
Mutual labels:  bluetooth, bluetooth-low-energy
pybluez-examples
Example Bluetooth tasks using the Python PyBluez module
Stars: ✭ 59 (+18%)
Mutual labels:  bluetooth, bluetooth-low-energy
ruuvitag-demo
Demo of reading Bluetooth Low Energy sensor measurements of RuuviTag environmental sensors and feeding them to MQTT, a database and dashboards
Stars: ✭ 14 (-72%)
Mutual labels:  bluetooth, bluetooth-low-energy
Blegattcoroutines
Functional Bluetooth GATT for Android (all the meanings)
Stars: ✭ 343 (+586%)
Mutual labels:  bluetooth-low-energy, bluetooth
Bleu
BLE (Bluetooth LE) for U🎁 Bleu is the best in the Bluetooth library.
Stars: ✭ 481 (+862%)
Mutual labels:  bluetooth-low-energy, bluetooth

GJLightBlueTooth

GJLightBlueTooth是一个轻量级蓝牙库。

FOR ENGLISH DOC IN https://github.com/manofit/GJLightBlueTooth/blob/master/ReadMe_EN.md

demo

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

项目架构

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

其中:

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

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

怎么使用

在创建页面后,你应该初始化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];
    }];

扫描

[self.BLE scan]

停止扫描

[self.BLE stopScan]

连接

[self.BLE connectWithPeripheral:peri]

断开连接

[self.BLE cancelConnectWithPeripheral:peri]

读取信号量

[self.BLE readRSSIWithPeriperal:peri]

发送指令

[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];

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

注意

  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];
    }];
  1. 在发送心跳包时候,计时器的mode需要设置为NSRunLoopCommonModes,防止页面滑动造成计时器停止。
-(NSTimer *)timer{
if (!_timer){
_timer = [NSTimer timerWithTimeInterval:0.2 target:self selector:@selector(sendHeartBeat) userInfo:nil repeats:YES];

// 将定时器加入循环。mode为NSRunLoopCommonModes,防止页面滑动造成定时器停止。
// set mode NSRunLoopCommonModes, or timer will stop when page scroll.
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}

return _timer;
}

效果图

DeviceList DataSend

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].