You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.6 KiB
Kotlin

package com.common.bluetooth
import android.content.Context
import android.provider.Settings
import java.nio.charset.StandardCharsets
import java.util.*
/**
* BT静态方法
*
* @author Alex Wang
* @since 2021-10-14
*/
object BtConstants {
val DEFAULT_CHARSET = StandardCharsets.UTF_8
/**
* 蓝牙类型
*/
enum class BLUETOOTH_TYPE {
/**
* 经典蓝牙
*/
CLASSIC,
/**
* 低功耗蓝牙
*/
BLE
}
private const val STATUS_BASE = 0x1
const val CONNECT_SUCCESS = STATUS_BASE shl 1
const val CONNECT_ERROR = STATUS_BASE shl 2
const val READ_SUCCESS = STATUS_BASE shl 3
const val READ_ERROR = STATUS_BASE shl 4
const val WRITE_SUCCESS = STATUS_BASE shl 5
const val WRITE_ERROR = STATUS_BASE shl 6
const val CREATE_SERVER_SUCCESS = STATUS_BASE shl 7
const val CREATE_SERVER_ERROR = STATUS_BASE shl 8
const val DISCONNECT = STATUS_BASE shl 9
enum class EXCEPTION(s: String) {
NOT_CONNECTED("not connect"),
NULL_SERVICE("service is null"),
NULL_CHARACTERISTIC("characteristic is null"),
READ_EXCEPTION("characteristic read error"),
WRITE_EXCEPTION("characteristic write error"),
NOTIFY_OPEN_EXCEPTION("notification open error"),
NOTIFY_CLOSE_EXCEPTION("notification close error")
}
// Message types sent from the BluetoothChatService Handler
const val MESSAGE_STATE_CHANGE = 1
const val MESSAGE_READ = 2
const val MESSAGE_WRITE = 3
const val MESSAGE_DEVICE_NAME = 4
const val MESSAGE_TOAST = 5
// Key names received from the BluetoothChatService Handler
const val DEVICE_NAME = "device_name"
const val TOAST = "toast"
/**
* 最大的单包发送字节数
*/
const val MAX_MTU = 35
/**
* 经典蓝牙连接UUID
*/
val CLASSIC_BT_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB")
val UUID_SERVICE = UUID.fromString("0783b03e-8535-b5a0-7140-a304d2495cb7")
val UUID_CHARACTERISTIC_READ_NOTIFY = UUID.fromString("0783b03e-8535-b5a0-7140-a304d2495cb8")
val UUID_CHARACTERISTIC_WRITE = UUID.fromString("0783b03e-8535-b5a0-7140-a304d2495cba")
val UUID_DESC_NOTITY = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb")
/**
* 获取UUID
*/
fun getUUid(context: Context?): UUID {
if (context == null) {
return UUID.randomUUID()
}
val androidId =
Settings.System.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
return UUID.nameUUIDFromBytes(androidId.toByteArray())
}
}