
Security News
Meet Socket at Black Hat Europe and BSides London 2025
Socket is heading to London! Stop by our booth or schedule a meeting to see what we've been working on.
com.github.wandersnail:easyble
Advanced tools
dependencies {
...
implementation 'com.github.wandersnail:easyble:1.1.10'
}
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
maven { url 'https://dl.bintray.com/wandersnail/android/' }
}
}
Ble.instance.initialize(application)//在Application中初始化
BleLogger.logEnabled = true//只控制打印,不控制回调
BleLogger.logCallback = object : LogCallback {
override fun onLog(priority: Int, log: String) {
when (priority) {
Log.VERBOSE -> TODO()
Log.INFO -> TODO()
Log.DEBUG -> TODO()
Log.WARN -> TODO()
Log.ERROR -> TODO()
Log.ASSERT -> TODO()
}
}
}
//1. 搜索监听
private val scanListener = object : ScanListener {
override fun onScanStart() {
}
override fun onScanStop() {
}
override fun onScanResult(device: Device) {
//搜索到蓝牙设备
}
override fun onScanError(errorCode: Int, errorMsg: String) {
when (errorCode) {
ScanListener.ERROR_LACK_LOCATION_PERMISSION -> {//缺少定位权限
}
ScanListener.ERROR_LOCATION_SERVICE_CLOSED -> {//位置服务未开启
}
}
}
}
//2. 添加监听
Ble.instance.addScanListener(scanListener)
//3. 搜索设置
Ble.instance.bleConfig.scanConfig.setScanPeriodMillis(30000)
.setUseBluetoothLeScanner(true)
.setAcceptSysConnectedDevice(true)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Ble.instance.bleConfig.scanConfig.setScanSettings(ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build())
}
//4. 开始搜索
Ble.instance.startScan()
//停止搜索
Ble.instance.stopScan()
//连接设置
val config = ConnectionConfig()
config.setDiscoverServicesDelayMillis(500)
config.setAutoReconnect(autoReconnect)
...
//建立连接
Ble.instance.connect(device!!, config, object : ConnectionStateChangeListener {
override fun onConnectionStateChanged(device: Device) {
when (device.connectionState) {
IConnection.STATE_SCANNING -> {
}
IConnection.STATE_CONNECTING -> {
}
IConnection.STATE_CONNECTED -> {
}
IConnection.STATE_DISCONNECTED -> {
}
IConnection.STATE_SERVICE_DISCOVERING -> {
}
IConnection.STATE_SERVICE_DISCOVERED -> {
}
IConnection.STATE_RELEASED -> {
}
}
}
override fun onConnectFailed(device: Device?, type: Int) {
when (type) {
IConnection.CONNECT_FAIL_TYPE_NON_CONNECTABLE -> {}
IConnection.CONNECT_FAIL_TYPE_UNSPECIFIED_ADDRESS -> {}
IConnection.CONNECT_FAIL_TYPE_MAXIMUM_RECONNECTION -> {}
}
}
override fun onConnectTimeout(device: Device, type: Int) {
when (type) {
IConnection.TIMEOUT_TYPE_CANNOT_CONNECT -> {}
IConnection.TIMEOUT_TYPE_CANNOT_DISCOVER_SERVICES -> {}
}
}
})
//断开指定连接
Ble.instance.disconnectConnection(device)
//断开所有连接
Ble.instance.disconnectAllConnections()
//释放指定连接
Ble.instance.releaseConnection(device)
//释放所有连接
Ble.instance.releaseAllConnections()
//方式一:
//1. 实例化观察者
val eventObserver = object : EventObserver {
override fun onBluetoothStateChanged(state: Int) {
}
override fun onLogChanged(log: String, level: Int) {
}
override fun onCharacteristicRead(device: Device, tag: String, serviceUuid: UUID, characteristicUuid: UUID, value: ByteArray) {
}
override fun onRequestFailed(device: Device, tag: String, requestType: Request.RequestType, failType: Int, src: ByteArray?) {
}
override fun onCharacteristicWrite(device: Device, tag: String, serviceUuid: UUID, characteristicUuid: UUID, value: ByteArray) {
}
override fun onRemoteRssiRead(device: Device, tag: String, rssi: Int) {
}
override fun onDescriptorRead(device: Device, tag: String, serviceUuid: UUID, characteristicUuid: UUID, descriptorUuid: UUID, value: ByteArray) {
}
override fun onNotificationChanged(device: Device, tag: String, serviceUuid: UUID, characteristicUuid: UUID, descriptorUuid: UUID, isEnabled: Boolean) {
}
override fun onIndicationChanged(device: Device, tag: String, serviceUuid: UUID, characteristicUuid: UUID, descriptorUuid: UUID, isEnabled: Boolean) {
}
override fun onMtuChanged(device: Device, tag: String, mtu: Int) {
}
override fun onPhyRead(device: Device, tag: String, txPhy: Int, rxPhy: Int) {
}
override fun onPhyUpdate(device: Device, tag: String, txPhy: Int, rxPhy: Int) {
}
override fun onConnectionStateChanged(device: Device) {
}
override fun onConnectFailed(device: Device?, type: Int) {
}
override fun onConnectTimeout(device: Device, type: Int) {
}
override fun onCharacteristicChanged(device: Device, serviceUuid: UUID, characteristicUuid: UUID, value: ByteArray) {
}
}
//2. 注册观察者
Ble.instance.registerObserver(eventObserver)
//3. 调用相应方法
val connection = Ble.instance.getConnection(device)
connection?.readCharacteristic(tag, service, characteristic)
connection?.enableNotification(tag, service, characteristic)
connection?.disableNotification(tag, service, characteristic)
connection?.writeCharacteristic(tag, service, characteristic, byteArrayOf(0x05, 0x06))
connection?.readRssi(tag)
...
//取消注册观察者
Ble.instance.unregisterObserver(eventObserver)
//方式二:
val connection = Ble.instance.getConnection(device)
connection?.readCharacteristic(tag, service, characteristic, object : CharacteristicReadCallback {
@InvokeThread(RunOn.MAIN)
override fun onCharacteristicRead(device: Device, tag: String, serviceUuid: UUID, characteristicUuid: UUID, value: ByteArray) {
}
@InvokeThread(RunOn.BACKGROUND)
override fun onRequestFailed(device: Device, tag: String, requestType: Request.RequestType, failType: Int, src: ByteArray?) {
}
})
connection?.enableNotification(tag, service, characteristic, object : CharacteristicWriteCallback {
override fun onCharacteristicWrite(device: Device, tag: String, serviceUuid: UUID, characteristicUuid: UUID, value: ByteArray) {
}
override fun onRequestFailed(device: Device, tag: String, requestType: Request.RequestType, failType: Int, src: ByteArray?) {
}
})
...
Ble.instance.release()

FAQs
A framework for Android Bluetooth Low Energy (BLE)
We found that com.github.wandersnail:easyble demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Socket is heading to London! Stop by our booth or schedule a meeting to see what we've been working on.

Security News
OWASP’s 2025 Top 10 introduces Software Supply Chain Failures as a new category, reflecting rising concern over dependency and build system risks.

Research
/Security News
Socket researchers discovered nine malicious NuGet packages that use time-delayed payloads to crash applications and corrupt industrial control systems.