{"id":1197,"url":"https://github.com/coolnameismy/BabyBluetooth","last_synced_at":"2025-07-30T20:32:51.895Z","repository":{"id":35729320,"uuid":"40007772","full_name":"coolnameismy/BabyBluetooth","owner":"coolnameismy","description":":baby: The easiest way to use Bluetooth (BLE )in ios/os ,even bady can use . 一个非常容易使用的蓝牙库,适用于ios和os","archived":false,"fork":false,"pushed_at":"2021-03-10T06:54:03.000Z","size":618,"stargazers_count":4730,"open_issues_count":132,"forks_count":1003,"subscribers_count":166,"default_branch":"master","last_synced_at":"2024-10-29T15:34:11.199Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/coolnameismy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-07-31T13:57:36.000Z","updated_at":"2024-10-24T07:35:21.000Z","dependencies_parsed_at":"2022-07-10T14:17:19.119Z","dependency_job_id":null,"html_url":"https://github.com/coolnameismy/BabyBluetooth","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coolnameismy%2FBabyBluetooth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coolnameismy%2FBabyBluetooth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coolnameismy%2FBabyBluetooth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/coolnameismy%2FBabyBluetooth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/coolnameismy","download_url":"https://codeload.github.com/coolnameismy/BabyBluetooth/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222275450,"owners_count":16959401,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-01-05T20:15:41.047Z","updated_at":"2024-12-04T20:31:06.509Z","avatar_url":"https://github.com/coolnameismy.png","language":"Objective-C","funding_links":[],"categories":["Hardware","第三方库","Objective-C","OOM-Leaks-Crash"],"sub_categories":["Bluetooth","Other free courses"],"readme":"\n![](logo.png)\n\nThe easiest way to use Bluetooth (BLE )in ios,even bady can use. 简单易用的蓝牙库，基于CoreBluetooth的封装，并兼容ios和mac osx.\n\n**为什么使用它？**\n\n- 1：基于原生CoreBluetooth框架封装的轻量级的开源库，可以帮你更简单地使用CoreBluetooth API。\n- 2：CoreBluetooth所有方法都是通过委托完成，代码冗余且顺序凌乱。BabyBluetooth使用block方法，可以重新按照功能和顺序组织代码，并提供许多方法减少蓝牙开发过程中的代码量。\n- 3:链式方法体，代码更简洁、优雅。\n- 4:通过channel切换区分委托调用，并方便切换\n- 5:便利的工具方法\n- 6:完善的文档，且项目处于活跃状态，不断的更新中\n- 7:github上star最多的纯Bluetooth类库\n- 8:包含多种类型的demo和ios蓝牙开发教程\n- 9:同时支持蓝牙设备中心模式和外设模式（central model and peripheral model）\n\n当前版本 0.7.0 \n\n[更新说明](https://github.com/coolnameismy/BabyBluetooth/wiki/%E6%9B%B4%E6%96%B0%E8%AF%B4%E6%98%8E)\n\n\n详细文档请参考wiki The full documentation of the project is available on its wiki.\n# [english readme link,please click it!](https://github.com/coolnameismy/BabyBluetooth/blob/master/README_en.md)\n\n# Table Of Contents\n\n* [QuickExample](#user-content-QuickExample)\n* [如何安装](#如何安装)\n* [如何使用](#如何使用)\n* [示例程序说明](#示例程序说明)\n* [兼容性](#兼容性)\n* [后期更新](#后期更新)\n* [蓝牙学习资源](#蓝牙学习资源)\n* [期待](#期待)\n* [常见问题](https://github.com/coolnameismy/BabyBluetooth/issues)\n\n# QuickExample\n\n## 中心模式 central model \n\u003e   app作为中心，连接其他BLE4.0外设\n\n```objc\n\n//导入.h文件和系统蓝牙库的头文件\n#import \"BabyBluetooth.h\"\n//定义变量\nBabyBluetooth *baby;\n\n-(void)viewDidLoad {\n    [super viewDidLoad];\n\n    //初始化BabyBluetooth 蓝牙库\n    baby = [BabyBluetooth shareBabyBluetooth];\n    //设置蓝牙委托\n    [self babyDelegate];\n    //设置委托后直接可以使用，无需等待CBCentralManagerStatePoweredOn状态\n    baby.scanForPeripherals().begin();\n}\n\n//设置蓝牙委托\n-(void)babyDelegate{\n\n    //设置扫描到设备的委托\n    [baby setBlockOnDiscoverToPeripherals:^(CBCentralManager *central, CBPeripheral *peripheral, NSDictionary *advertisementData, NSNumber *RSSI) {\n        NSLog(@\"搜索到了设备:%@\",peripheral.name);\n    }];\n   \n    //过滤器\n    //设置查找设备的过滤器\n    [baby setFilterOnDiscoverPeripherals:^BOOL(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI) {\n        //最常用的场景是查找某一个前缀开头的设备 most common usage is discover for peripheral that name has common prefix\n        //if ([peripheralName hasPrefix:@\"Pxxxx\"] ) {\n        //    return YES;\n        //}\n        //return NO;\n        //设置查找规则是名称大于1 ， the search rule is peripheral.name length \u003e 1\n        if (peripheralName.length \u003e1) {\n            return YES;\n        }\n        return NO;\n    }];\n\n    //.......\n}\n\n```\n\n更多蓝牙操作方法和委托请参考[wiki](https://github.com/coolnameismy/BabyBluetooth/wiki)\n\n中心模式使用示例请参考:[BabyBluetoothAppDemo](https://github.com/coolnameismy//BabyBluetoothExamples/BabyBluetoothAppDemo)\n\n## 外设模式 peripheral model \n\u003e   app模拟一个，BLE4.0外设，可以被其他设备连接和使用\n\n\n模拟一个有2个service和6个characteristic的外设\n\n````objc\n//导入.h文件和系统蓝牙库的头文件\n#import \"BabyBluetooth.h\"\n//定义变量\nBabyBluetooth *baby;\n\n-(void)viewDidLoad {\n    [super viewDidLoad];\n\n    //配置第一个服务s1\n    CBMutableService *s1 = makeCBService(@\"FFF0\");\n    //配置s1的3个characteristic\n    makeCharacteristicToService(s1, @\"FFF1\", @\"r\", @\"hello1\");//读\n    makeCharacteristicToService(s1, @\"FFF2\", @\"w\", @\"hello2\");//写\n    makeCharacteristicToService(s1, genUUID(), @\"rw\", @\"hello3\");//可读写,uuid自动生成\n    makeCharacteristicToService(s1, @\"FFF4\", nil, @\"hello4\");//默认读写字段\n    makeCharacteristicToService(s1, @\"FFF5\", @\"n\", @\"hello5\");//notify字段\n    //配置第一个服务s2\n    CBMutableService *s2 = makeCBService(@\"FFE0\");\n    makeStaticCharacteristicToService(s2, genUUID(), @\"hello6\", [@\"a\" dataUsingEncoding:NSUTF8StringEncoding]);//一个含初值的字段，该字段权限只能是只读\n   \n    //实例化baby\n    baby = [BabyBluetooth shareBabyBluetooth];\n    //配置委托\n    [self babyDelegate];\n    //启动外设\n    baby.bePeripheral().addServices(@[s1,s2]).startAdvertising();\n}\n\n//设置蓝牙外设模式的委托\n-(void)babyDelegate{\n\n     //设置添加service委托 | set didAddService block\n    [baby peripheralModelBlockOnPeripheralManagerDidUpdateState:^(CBPeripheralManager *peripheral) {\n        NSLog(@\"PeripheralManager trun status code: %ld\",(long)peripheral.state);\n    }];\n    \n    //设置添加service委托 | set didAddService block\n    [baby peripheralModelBlockOnDidStartAdvertising:^(CBPeripheralManager *peripheral, NSError *error) {\n        NSLog(@\"didStartAdvertising !!!\");\n    }];\n    \n    //设置添加service委托 | set didAddService block\n    [baby peripheralModelBlockOnDidAddService:^(CBPeripheralManager *peripheral, CBService *service, NSError *error) {\n        NSLog(@\"Did Add Service uuid: %@ \",service.UUID);\n    }];\n\n    //.....\n}\n\n````\n  \n更多蓝牙外设模式委托请参考[wiki](https://github.com/coolnameismy/BabyBluetooth/wiki)\n\n中心模式使用示例请参考:[BluetoothStubOnIOS](https://github.com/coolnameismy//BabyBluetoothExamples/BluetoothStubOnIOS)\n\n# 如何安装\n\n##1 手动安装\nstep1:将项目Classes/objc 文件夹中的文件直接拖入你的项目中即可\n\nstep2:导入.h文件\n\n````objc\n#import \"BabyBluetooth.h\"\n````\n\n##2 cocoapods\nstep1:add the following line to your Podfile:\n````\npod 'BabyBluetooth','~\u003e 0.7.0'\n````\n\nstep2:导入.h文件\n````objc\n#import \"BabyBluetooth.h\"\n````\n\n# 如何使用\n[用法请见wiki](https://github.com/coolnameismy/BabyBluetooth/wiki)\n\n# 示例程序说明\n\n**BabyBluetoothExamples/BabyBluetoothAppDemo** :一个类似lightblue的程序，蓝牙操作全部使用BabyBluetooth完成。\n功能：\n- 1：扫描周围设备\n- 2：连接设备，扫描设备的全部services和characteristic\n- 3：显示characteristic，读取characteristic的value，和descriptors以及Descriptors对应的value\n- 4：写0x01到characteristic\n- 5：订阅/取消订阅 characteristic的notify\n\n**BabyBluetoothExamples/BluetoothStubOnIOS** : 一个iOS程序，启动后会用手机模拟一个外设，提供2个服务和若干characteristic。\n该程序作为Babybluetooth 外设模式使用的示例程序\n\n**BabyBluetoothExamples/BabyBluetoothOSDemo** :一个mac os程序，因为os和ios的蓝牙底层方法都一样，所以BabyBluetooth可以ios/os通用。但是os程序有个好处就是直接可以在mac上跑蓝牙设备，不像ios，必须要真机才能跑蓝牙设备。所以不能真机调试时可以使用os尝试蓝牙库的使用。\n\n功能：\n- 1：扫描周围设备、连接设备、显示characteristic，读取characteristic的value，和descriptors以及Descriptors对应的value的委托设置，并使用nslog打印信息。\n\n**BabyBluetoothExamples/BluetoothStubOnOSX** :一个mac os程序，该程序可以作为蓝牙外设使用，解决学习蓝牙时没有外设可用的囧境，并且可以作为peripheral model模式的学习示例。改程序用swift编码。\n\n\n\n\n功能：\n- 1：作为蓝牙外设使用，可以被发现，连接，读写，订阅\n- 2：提供1个service，包含了3个characteristic，分别具有读、读写、订阅功能\n\n# 兼容性\n- 蓝牙4.0，也叫做ble，ios6以上和iPhone4s以上可以自由使用\n- os和ios通用\n- 蓝牙设备相关程序必须使用真机才能运行。如果不能使用真机调试的情况，可以使用os程序调试蓝牙。可以参考示例程序中的BabyBluetoothOSDemo\n- 本项目和示例程序是使用ios 8.3开发，使用者可以自行降版本，但必须大于6.0 \n\n\n# 后期更新\n \n- 增加对Carthage Install的支持\n- [swift版本开发,目前项目已经兼容swift，详细见这里](https://github.com/coolnameismy/BabyBluetooth-swift)\n\n已经更新的版本说明，请在wiki中查看\n\n# 蓝牙学习资源\n\u003e  温馨提示：零基础做蓝牙开发很困难，所以就算使用babybluetooth降低了代码量,仍然任很有必要花上几天时间把蓝牙的基础概念弄明白后才开始动手~\n\n- [ios蓝牙开发（一）蓝牙相关基础知识](http://liuyanwei.jumppo.com/2015/07/17/ios-BLE-1.html)\n- [ios蓝牙开发（二）蓝牙中心模式的ios代码实现](http://liuyanwei.jumppo.com/2015/08/14/ios-BLE-2.html)\n- [ios蓝牙开发（三）app作为外设被连接的实现](http://liuyanwei.jumppo.com/2015/09/07/ios-BLE-3.html)\n- [ios蓝牙开发（四）BabyBluetooth蓝牙库介绍](http://liuyanwei.jumppo.com/2015/09/11/ios-BLE-4.html)\n- 暂未完成-ios蓝牙开发（五）BabyBluetooth实现原理\n- 待定...\n- [官方CoreBuetooth支持页](https://developer.apple.com/bluetooth)\n- [Bluetooth Accessory Design Guidelines for Apple Products](https://developer.apple.com/hardwaredrivers/BluetoothDesignGuidelines.pdf)\n- [知乎live：一小时蓝牙应用开发科普 文字整理](http://liuyanwei.jumppo.com/2017/01/23/zhihu-live-a-hour-for-bluetooth-0.html)\n\n# 组织和交流\n- qq交流群6: 284341984(满）\n- qq交流群5: 426082944(满）\n- qq交流群4: 313084771(满）\n- qq交流群3：530142592(满）\n- qq交流群2：168756967(满）\n- qq交流群1：426603940(满)\n- 个人微信公众号\n\n![个人微信公众号](qrcode.jpg)\n\n# 招聘\n\n  阿里云-iot事业部招人，p6 p7 前端,android,iOS,风光无限 一起造作，有兴趣的投简历到 coolnameismy@gmail.com\n\n# 期待\n\n  - 蓝牙库写起来很辛苦，不要忘记点击右上角小星星star和[follow](https://github.com/coolnameismy)支持一下~\n  - 如果在使用过程中遇到BUG，或发现功能不够用，希望你能Issues我，谢谢\n  - 如果你的app用到了Babybluetooth，希望你能发邮件（[coolnameismy@hotmail.com](mailto:coolnameismy@hotmail.com?subject=Babybluetooth用在的了我们的app中)）告诉我，我会在readme中展现出来。\n  - 期待大家也能一起为BabyBluetooth输出代码，这里我只是给BabyBluetooth开了个头，他可以增加和优化的地方还是非常多。也期待和大家在Pull Requests一起学习，交流，成长。\n  \n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoolnameismy%2FBabyBluetooth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcoolnameismy%2FBabyBluetooth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcoolnameismy%2FBabyBluetooth/lists"}