{"id":37028125,"url":"https://github.com/wandersnail/easyble","last_synced_at":"2026-01-14T03:21:21.220Z","repository":{"id":57723957,"uuid":"161329582","full_name":"wandersnail/easyble","owner":"wandersnail","description":"A simple framework for Android Bluetooth Low Energy (BLE)","archived":false,"fork":false,"pushed_at":"2022-07-06T01:31:57.000Z","size":3857,"stargazers_count":43,"open_issues_count":0,"forks_count":13,"subscribers_count":5,"default_branch":"master","last_synced_at":"2023-07-01T02:17:32.242Z","etag":null,"topics":["android","ble","bluetooth","framework"],"latest_commit_sha":null,"homepage":null,"language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wandersnail.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":"2018-12-11T12:19:44.000Z","updated_at":"2023-04-20T20:44:09.000Z","dependencies_parsed_at":"2022-09-02T06:22:38.069Z","dependency_job_id":null,"html_url":"https://github.com/wandersnail/easyble","commit_stats":null,"previous_names":[],"tags_count":13,"template":null,"template_full_name":null,"purl":"pkg:github/wandersnail/easyble","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wandersnail%2Feasyble","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wandersnail%2Feasyble/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wandersnail%2Feasyble/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wandersnail%2Feasyble/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wandersnail","download_url":"https://codeload.github.com/wandersnail/easyble/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wandersnail%2Feasyble/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28408825,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T01:52:23.358Z","status":"online","status_checked_at":"2026-01-14T02:00:06.678Z","response_time":107,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["android","ble","bluetooth","framework"],"created_at":"2026-01-14T03:21:20.619Z","updated_at":"2026-01-14T03:21:21.213Z","avatar_url":"https://github.com/wandersnail.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 发现还有挺多人不知道怎么用kotlin，所以用java重构了，这个库就不维护了。[点击跳转新库](https://github.com/wandersnail/easyble-x)\n\n[English](https://github.com/wandersnail/easyble/blob/master/README_EN.md)\n\n## 学习提高\n[![](https://img.shields.io/badge/下载-java、python、vue、js、c++等各种视频教程-blue.svg)](https://p.zhekou.life)\n\n## 最新版本\n[![](https://jitpack.io/v/wandersnail/easyble.svg)](https://jitpack.io/#wandersnail/easyble)\n[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.github.wandersnail/easyble/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.github.wandersnail/easyble)\n\n## 功能\n- 支持多设备同时连接\n- 支持连接同时配对\n- 支持搜索已连接设备\n- 支持搜索器设置\n- 支持自定义搜索过滤条件\n- 支持自动重连、最大重连次数限制、直接重连或搜索到设备再重连控制\n- 支持请求延时及发送延时设置\n- 支持分包大小设置、最大传输单元设置\n- 支持注册和取消通知监听\n- 支持回调方式，支持使用注解@InvokeThread控制回调线程。注意：观察者监听和回调只能取其一！\n- 支持发送设置（是否等待发送结果回调再发送下一包）\n- 支持写入模式设置\n- 支持设置连接的传输方式\n- 支持连接超时设置\n\n## 配置\n\n1. module的build.gradle中的添加依赖，自行修改为最新版本，同步后通常就可以用了：\n```\ndependencies {\n\t...\n\timplementation 'com.github.wandersnail:easyble:1.1.10'\n}\n```\n\n2. 如果从jcenter下载失败。在project的build.gradle里的repositories添加内容，最好两个都加上，有时jitpack会抽风，同步不下来。添加完再次同步即可。\n```\nallprojects {\n\trepositories {\n\t\t...\n\t\tmaven { url 'https://jitpack.io' }\n\t\tmaven { url 'https://dl.bintray.com/wandersnail/android/' }\n\t}\n}\n```\n\n## 使用方法(kotlin)\n\n1. 初始化SDK\n```\nBle.instance.initialize(application)//在Application中初始化\nBleLogger.logEnabled = true//只控制打印，不控制回调\nBleLogger.logCallback = object : LogCallback {\n\toverride fun onLog(priority: Int, log: String) {\n\t\twhen (priority) {\n\t\t\tLog.VERBOSE -\u003e TODO()\n\t\t\tLog.INFO -\u003e TODO()\n\t\t\tLog.DEBUG -\u003e TODO()\n\t\t\tLog.WARN -\u003e TODO()\n\t\t\tLog.ERROR -\u003e TODO()\n\t\t\tLog.ASSERT -\u003e TODO()\n\t\t}\n\t}\n}\n```\n\n2. 蓝牙搜索\n```\n//1. 搜索监听\nprivate val scanListener = object : ScanListener {\n\toverride fun onScanStart() {\n\t\t\n\t}\n\n\toverride fun onScanStop() {\n\t\t\n\t}\n\n\toverride fun onScanResult(device: Device) {\n\t\t//搜索到蓝牙设备\n\t}\n\n\toverride fun onScanError(errorCode: Int, errorMsg: String) {\n\t\twhen (errorCode) {\n\t\t\tScanListener.ERROR_LACK_LOCATION_PERMISSION -\u003e {//缺少定位权限\n\t\t\t\t\n\t\t\t}\n\t\t\tScanListener.ERROR_LOCATION_SERVICE_CLOSED -\u003e {//位置服务未开启\n\t\t\t\t\n\t\t\t}\n\t\t}\n\t}\n}\n\n//2. 添加监听\nBle.instance.addScanListener(scanListener)\n\n//3. 搜索设置\nBle.instance.bleConfig.scanConfig.setScanPeriodMillis(30000)\n\t\t.setUseBluetoothLeScanner(true)\n\t\t.setAcceptSysConnectedDevice(true)\nif (Build.VERSION.SDK_INT \u003e= Build.VERSION_CODES.LOLLIPOP) {\n\tBle.instance.bleConfig.scanConfig.setScanSettings(ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build())\n}\n\n//4. 开始搜索\nBle.instance.startScan()\n\n//停止搜索\nBle.instance.stopScan()\n\n```\n\n3. 连接\n```\n//连接设置\nval config = ConnectionConfig()\nconfig.setDiscoverServicesDelayMillis(500)\nconfig.setAutoReconnect(autoReconnect)\n...\n\n//建立连接\nBle.instance.connect(device!!, config, object : ConnectionStateChangeListener {\n\toverride fun onConnectionStateChanged(device: Device) {\n\t\twhen (device.connectionState) {\n\t\t\tIConnection.STATE_SCANNING -\u003e {\n\n\t\t\t}\n\t\t\tIConnection.STATE_CONNECTING -\u003e {\n\n\t\t\t}\n\t\t\tIConnection.STATE_CONNECTED -\u003e {\n\n\t\t\t}\n\t\t\tIConnection.STATE_DISCONNECTED -\u003e {\n\n\t\t\t}\n\t\t\tIConnection.STATE_SERVICE_DISCOVERING -\u003e {\n\n\t\t\t}\n\t\t\tIConnection.STATE_SERVICE_DISCOVERED -\u003e {\n\n\t\t\t}\n\t\t\tIConnection.STATE_RELEASED -\u003e {\n\n\t\t\t}\n\t\t}\n\t}\n\n\toverride fun onConnectFailed(device: Device?, type: Int) {\n\t\twhen (type) {\n\t\t\tIConnection.CONNECT_FAIL_TYPE_NON_CONNECTABLE -\u003e {}\n\t\t\tIConnection.CONNECT_FAIL_TYPE_UNSPECIFIED_ADDRESS -\u003e {}\n\t\t\tIConnection.CONNECT_FAIL_TYPE_MAXIMUM_RECONNECTION -\u003e {}\n\t\t}\n\t}\n\n\toverride fun onConnectTimeout(device: Device, type: Int) {\n\t\twhen (type) {\n\t\t\tIConnection.TIMEOUT_TYPE_CANNOT_CONNECT -\u003e {}\n\t\t\tIConnection.TIMEOUT_TYPE_CANNOT_DISCOVER_SERVICES -\u003e {}\n\t\t}\n\t}\n})\n\n//断开指定连接\nBle.instance.disconnectConnection(device)\n//断开所有连接\nBle.instance.disconnectAllConnections()\n\n//释放指定连接\nBle.instance.releaseConnection(device)\n//释放所有连接\nBle.instance.releaseAllConnections()\n```\n\n4. 读写特征值、开启Notify\n```\n//方式一：\n//1. 实例化观察者\nval eventObserver = object : EventObserver {\n\toverride fun onBluetoothStateChanged(state: Int) {\n\t}\n\n\toverride fun onLogChanged(log: String, level: Int) {\n\t}\n\n\toverride fun onCharacteristicRead(device: Device, tag: String, serviceUuid: UUID, characteristicUuid: UUID, value: ByteArray) {\n\t}\n\n\toverride fun onRequestFailed(device: Device, tag: String, requestType: Request.RequestType, failType: Int, src: ByteArray?) {\n\t}\n\n\toverride fun onCharacteristicWrite(device: Device, tag: String, serviceUuid: UUID, characteristicUuid: UUID, value: ByteArray) {\n\t}\n\n\toverride fun onRemoteRssiRead(device: Device, tag: String, rssi: Int) {\n\t}\n\n\toverride fun onDescriptorRead(device: Device, tag: String, serviceUuid: UUID, characteristicUuid: UUID, descriptorUuid: UUID, value: ByteArray) {\n\t}\n\n\toverride fun onNotificationChanged(device: Device, tag: String, serviceUuid: UUID, characteristicUuid: UUID, descriptorUuid: UUID, isEnabled: Boolean) {\n\t}\n\n\toverride fun onIndicationChanged(device: Device, tag: String, serviceUuid: UUID, characteristicUuid: UUID, descriptorUuid: UUID, isEnabled: Boolean) {\n\t}\n\n\toverride fun onMtuChanged(device: Device, tag: String, mtu: Int) {\n\t}\n\n\toverride fun onPhyRead(device: Device, tag: String, txPhy: Int, rxPhy: Int) {\n\t}\n\n\toverride fun onPhyUpdate(device: Device, tag: String, txPhy: Int, rxPhy: Int) {\n\t}\n\n\toverride fun onConnectionStateChanged(device: Device) {\n\t}\n\n\toverride fun onConnectFailed(device: Device?, type: Int) {\n\t}\n\n\toverride fun onConnectTimeout(device: Device, type: Int) {\n\t}\n\n\toverride fun onCharacteristicChanged(device: Device, serviceUuid: UUID, characteristicUuid: UUID, value: ByteArray) {\n\t}\n}\n//2. 注册观察者\nBle.instance.registerObserver(eventObserver)\n//3. 调用相应方法\nval connection = Ble.instance.getConnection(device)\nconnection?.readCharacteristic(tag, service, characteristic)\nconnection?.enableNotification(tag, service, characteristic)\nconnection?.disableNotification(tag, service, characteristic)\nconnection?.writeCharacteristic(tag, service, characteristic, byteArrayOf(0x05, 0x06))\nconnection?.readRssi(tag)\n...\n//取消注册观察者\nBle.instance.unregisterObserver(eventObserver)\n\n//方式二：\nval connection = Ble.instance.getConnection(device)\nconnection?.readCharacteristic(tag, service, characteristic, object : CharacteristicReadCallback {\n\t@InvokeThread(RunOn.MAIN)\n\toverride fun onCharacteristicRead(device: Device, tag: String, serviceUuid: UUID, characteristicUuid: UUID, value: ByteArray) {\n\t}\n\n\t@InvokeThread(RunOn.BACKGROUND)\n\toverride fun onRequestFailed(device: Device, tag: String, requestType: Request.RequestType, failType: Int, src: ByteArray?) {\n\t}\n})\nconnection?.enableNotification(tag, service, characteristic, object : CharacteristicWriteCallback {\n\toverride fun onCharacteristicWrite(device: Device, tag: String, serviceUuid: UUID, characteristicUuid: UUID, value: ByteArray) {\n\t}\n\n\toverride fun onRequestFailed(device: Device, tag: String, requestType: Request.RequestType, failType: Int, src: ByteArray?) {\n\t}\n})\n...\n```\n\n5. 释放SDK\n```\nBle.instance.release()\n```\n## 基于此库的BLE调试app\n[![](https://img.shields.io/badge/Download-App%20Store-yellow.svg)](http://app.mi.com/details?id=cn.zfs.bledebugger)\n[![](https://img.shields.io/badge/Download-APK-blue.svg)](https://raw.githubusercontent.com/wandersnail/myapks/master/bleutility-v2.1.apk)\n\n![image](https://github.com/wandersnail/easyble/blob/master/screenshot/demo.gif)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwandersnail%2Feasyble","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwandersnail%2Feasyble","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwandersnail%2Feasyble/lists"}