Giter Site home page Giter Site logo

cmdbluetooth's Introduction

CmdBluetooth

Version License Platform

communicate with bluetooth using a simple way, v0.1 already support for swift3.0

Pods Install

Swift 3.x pod 'CmdBluetooth', '~> 0.3'

Swift 4.x pod 'CmdBluetooth', '~> 4.1.1'

How to Use CmdBluetooth

Init Central

var centralManager = CmdCentralManager.manager

Scan devices

centralManager.scanWithServices(nil, duration: 5, discoveryHandle: { discovery in
	//deal with discovery
}, completeHandle: { 
	//scan finish in duration    
})

Connect

self.centralManager.connect(discovery, duration: 3, success: { (central, peripheral) in
	//connect discovery successful
}, fail: { error in
	//connect discovery fail with error 
})

Config CenterManager With Parser

Parser

befor Communicate with device, you need prepare a parser which providing ble information, such as read UUID Strings, write UUID Strings, you can do like this, just replace UUIDStr with yours

class MyParser: CmdBaseParser {

    static let notifyCharacterUUIDStr = "FFF1"
    static let writeCharacterUUIDStr = "FFF2"

    /**
     define types for writing data to BLE device, like this
     */
    func writeDataWithResponse(_ data: Data) {
        do {
            try super.writeData(data, characterUUIDStr: MyParser.writeCharacterUUIDStr, withResponse: true)
        } catch let error {
            print("[Error: ]__Write Data Error    " + "\(error)")
        }
    }
	/**
	write data to BLE without Response
	*/
    func writeDataWithoutResponse(_ data: Data) {
        do {
            try super.writeData(data, characterUUIDStr: MyParser.writeCharacterUUIDStr, withResponse: false)
        } catch let error {
            print("[Error: ]__Write Data Error    " + "\(error)")
        }
    }
	/**
	read data, such as read battery/ read heart rate
	*/
    func readData(_ characterUUIDStr: String) {
        do {
            try super.readCharacteristic(characterUUIDStr)
        } catch let error {
            print("[Error: ]__Read Data Error    " + "\(error)")
        }
    }
    //......Many....many ^_^!
}

ReceiveDataCenter

receive all data from ble by parser.dataComingMonitor, a Monitor is needed, you can create th monitor like this:

class ReceiveDataCenter: NSObject, ParserDataReceiveDelegate {

    func receiveData(_ data: Data, peripheral: CBPeripheral, characteristic: CBCharacteristic) {
    
		print("receive data: " + "\(data)")
    }
}

Advanced Init

let parser = MyParser()
let receiverCenter = ReceiveDataCenter()
parser.dataComingMonitor = receiverCenter
centerManager.parser = parser

cmdbluetooth's People

Contributors

zerofenglee avatar

Stargazers

TonyAng avatar 永丿卟╭訁丶弃 avatar BaiYuQiu avatar  avatar 长青空手道 avatar John Manos avatar  avatar Jing_Lee avatar  avatar FeelRules avatar 李学武 avatar cz3w avatar 5eky avatar shingohu avatar  avatar  avatar  avatar sandy183 avatar Zilla Chen avatar  avatar Torsten Curdt avatar Henry Liu avatar Qpc avatar  avatar  avatar Sheikh Imtiaz Hossain avatar  avatar  avatar  avatar

Watchers

 avatar James Cloos avatar  avatar

Forkers

bigcoww

cmdbluetooth's Issues

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.