Code Monkey home page Code Monkey logo

reactivecorebluetooth's Introduction

Synopsis

NOTE: Apple's CoreBluetooth library, which this project uses, only supports Bluetooth LE 4.0+. We're hoping to get future updates that support other versions of Bluetooth.

ReactiveCoreBluetooth is a library that wraps Apple's CoreBluetooth framework by providing ReactiveCocoa signals instead of delegates. This library is currently a work-in-progress that provides basic Bluetooth LE device management. There is a sample app included in this repo that shows how to make use of this wrapper.

Code Example

#import "ReactiveCoreBluetooth.h"

@property (nonatomic) BluetoothLEService* bluetoothLEService;

-(void) viewDidLoad
{
    self.bluetoothLEService = [[BluetoothLEService alloc] init];

    [bluetoothLEService.availableDevicesSignal subscribeNext:^(NSArray* devices) {
        for (CBPeripheral* p in devices)
        {
        	NSLog(@"%@", p.name);
        }
    }];

    [bluetoothLEService.peripheralConnectedSignal subscribeNext:^(CBPeripheral* device) {
    	NSLog(@"Connected to %@", device.name);
    }];

    [bluetoothLEService.peripheralDisconnectedSignal subscribeNext:^(CBPeripheral* device) {
    	NSLog(@"Disconnected from %@", device.name);
    }];

    [bluetoothLEService.scanningForDevicesSignal subscribeNext:^(NSNumber* x) {
    	BOOL isScanning = [x boolValue];
    	if (isScanning)
    	{
    		NSLog("Scanning for devices...");
    	}
    	else
    	{
    		NSLog("Not scanning for devices.");
    	}
	}];

    [bluetoothLEService.bluetoothStateSignal subscribeNext:^(NSNumber* x) {
        CBCentralManagerState state = (CBCentralManagerState)[x integerValue];
        NSString* status;
        switch (state)
        {
            case CBCentralManagerStatePoweredOff:
                status = @"Off";
                break;
            case CBCentralManagerStatePoweredOn:
                status = @"On";
                break;
            case CBCentralManagerStateResetting:
                status = @"Resetting";
                break;
            case CBCentralManagerStateUnauthorized:
                status = @"Unauthorized";
                break;
            case CBCentralManagerStateUnknown:
                status = @"Unknown";
                break;
            case CBCentralManagerStateUnsupported:
                status = @"Unsupported";
                break;
            default:
                status = @"Error: State Unknown";
                break;
        }

        NSLog(@"Bluetooth status: %@", status);
    }];

    [bluetoothLEService scanForAvailableDevices];
}

Motivation

We prefer to use ReactiveCocoa signals and blocks to manage asynchronous eventing instead of delegates. If you don't like implementing delegates everywhere, this is the Bluetooth LE library for you!

Installation

Add the following line to your Podfile:

pod 'ReactiveCoreBluetooth'

Then run the following in the same directory as your Podfile:

pod install

API Reference

Scanning for Devices

To start scanning for devices, call scanForAvailableDevices. You can listen on the scanningForDevicesSignal to determine if the device is currently scanning. To receive notifications about when the list of available devices changes, listen on the availableDevicesSignal. To stop scanning for devices, call stopScanningForDevices.

Device Connect and Disconnect

To capture device connections, subscribe to the peripheralConnectedSignal. To capture device disconnections, subscribe to the peripheralDisconnectedSignal.

Bluetooth Status

Listen on the bluetoothStateSignal.

Manually set cache settings

The cacheDurationForDevices setting allows you to change how long to wait while trying to connect to a device. The default is 5 seconds.

The cachePollingInterval setting allows you to change how frequently the cache is polled for expired devices. The default is 3 seconds.

Connect on Discovery

The connectOnDiscovery setting allows you to specify whether you want to connect to the device before it is added to the available devices signal. Some BLE devices need a connection to provide a name, while others might behave differently or cease advertising themselves once a connection is initiated.

Contributors

Matt Bowman (matt at citrrus dot com)

License

Copyright 2013 Citrrus, LLC.

Licensed under the MIT license.

reactivecorebluetooth's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.