|
|
|
@ -11,21 +11,19 @@ import android.content.ServiceConnection
|
|
|
|
|
import android.content.pm.PackageManager
|
|
|
|
|
import android.os.Build
|
|
|
|
|
import android.os.IBinder
|
|
|
|
|
import android.text.TextUtils
|
|
|
|
|
import android.util.Log.*
|
|
|
|
|
import androidx.core.content.ContextCompat.checkSelfPermission
|
|
|
|
|
import com.common.bluetooth.BtConstants.BLUETOOTH_TYPE
|
|
|
|
|
import com.common.bluetooth.service.ble.BluetoothClientBLEAdapter
|
|
|
|
|
import com.common.bluetooth.bean.CommonMsg
|
|
|
|
|
import com.common.bluetooth.callback.BLEClientListener
|
|
|
|
|
import com.common.bluetooth.callback.BtMsgListener
|
|
|
|
|
import com.common.bluetooth.callback.BtServiceListener
|
|
|
|
|
import com.common.bluetooth.callback.MsgReceiverListener
|
|
|
|
|
import com.common.bluetooth.callback.*
|
|
|
|
|
import com.common.bluetooth.interfaces.IBluetoothClient
|
|
|
|
|
import com.common.bluetooth.service.ble.BLEClientService
|
|
|
|
|
import com.common.bluetooth.service.ble.BLEClientService.BLEClientBinder
|
|
|
|
|
import com.common.bluetooth.service.ble.BLEReceiveService
|
|
|
|
|
import com.common.bluetooth.service.ble.BLEReceiveService.ReceiverBinder
|
|
|
|
|
import com.common.bluetooth.service.ble.BluetoothLeClient
|
|
|
|
|
import com.common.bluetooth.service.ble.BluetoothBLeClient
|
|
|
|
|
import com.common.bluetooth.service.bt.BTCClientService
|
|
|
|
|
import com.common.bluetooth.service.bt.BTCReceiverService
|
|
|
|
|
import com.common.bluetooth.service.bt.BluetoothClassicClient
|
|
|
|
@ -38,7 +36,7 @@ import java.util.concurrent.atomic.AtomicBoolean
|
|
|
|
|
/**
|
|
|
|
|
* 蓝牙管理类
|
|
|
|
|
*
|
|
|
|
|
* @author wangym
|
|
|
|
|
* @author Alex Wang
|
|
|
|
|
* @since 2021-10-28
|
|
|
|
|
*/
|
|
|
|
|
object BtManager {
|
|
|
|
@ -52,7 +50,7 @@ object BtManager {
|
|
|
|
|
/**
|
|
|
|
|
* BLE客户端服务
|
|
|
|
|
*/
|
|
|
|
|
private var clientService: BLEClientService? = null
|
|
|
|
|
private var bleClientService: BLEClientService? = null
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 经典蓝牙客户端服务
|
|
|
|
@ -72,18 +70,23 @@ object BtManager {
|
|
|
|
|
/**
|
|
|
|
|
* 消息接收监听
|
|
|
|
|
*/
|
|
|
|
|
var msgReceiverListener: MsgReceiverListener? = null
|
|
|
|
|
var btMsgReceiverListener: BtMsgReceiverListener? = null
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 蓝牙客户端监听
|
|
|
|
|
* 蓝牙状态监听
|
|
|
|
|
*/
|
|
|
|
|
var clientListener: BLEClientListener? = null
|
|
|
|
|
var btStatusListener: BtStatusListener? = null
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 蓝牙服务链接监听
|
|
|
|
|
*/
|
|
|
|
|
var btServiceListener: BtServiceListener? = null
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* BLE扫描结果监听
|
|
|
|
|
*/
|
|
|
|
|
var bleScanListener: BLEScanListener? = null
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 是否绑定了服务端
|
|
|
|
|
*/
|
|
|
|
@ -117,36 +120,44 @@ object BtManager {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 初始化蓝牙服务端模块
|
|
|
|
|
*
|
|
|
|
|
* @param context 上下文
|
|
|
|
|
* @param btType 蓝牙模式
|
|
|
|
|
*/
|
|
|
|
|
fun initServer(context: Context, btType: BLUETOOTH_TYPE) {
|
|
|
|
|
if (btType == BLUETOOTH_TYPE.BLE) {
|
|
|
|
|
mBtClient = BluetoothClientBLEAdapter(
|
|
|
|
|
BluetoothLeClient.getInstance(context)
|
|
|
|
|
BluetoothBLeClient.getInstance(context)
|
|
|
|
|
)
|
|
|
|
|
initBleReceiverService(context)
|
|
|
|
|
} else if (btType == BLUETOOTH_TYPE.CLASSIC) {
|
|
|
|
|
mBtClient = BluetoothClientClassicAdapter(BluetoothClassicClient(context))
|
|
|
|
|
mBtClient =
|
|
|
|
|
BluetoothClientClassicAdapter(BluetoothClassicClient.getInstance(context.applicationContext))
|
|
|
|
|
initClassicReceiverService(context)
|
|
|
|
|
}
|
|
|
|
|
// 检查蓝牙设备是否支持
|
|
|
|
|
mBtClient!!.checkBluetoothDevice(btType)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 初始化蓝牙客户端模块
|
|
|
|
|
*
|
|
|
|
|
* @param context 上下文
|
|
|
|
|
* @param btType 蓝牙模式
|
|
|
|
|
*/
|
|
|
|
|
fun initClient(context: Activity, btType: BLUETOOTH_TYPE) {
|
|
|
|
|
if (btType == BLUETOOTH_TYPE.BLE) {
|
|
|
|
|
mBtClient = BluetoothClientBLEAdapter(
|
|
|
|
|
BluetoothLeClient.getInstance(context)
|
|
|
|
|
BluetoothBLeClient.getInstance(context)
|
|
|
|
|
)
|
|
|
|
|
initBleClientService(context)
|
|
|
|
|
} else if (btType == BLUETOOTH_TYPE.CLASSIC) {
|
|
|
|
|
mBtClient = BluetoothClientClassicAdapter(
|
|
|
|
|
BluetoothClassicClient(context)
|
|
|
|
|
BluetoothClassicClient.getInstance(context.application)
|
|
|
|
|
)
|
|
|
|
|
initClassicClientService(context)
|
|
|
|
|
}
|
|
|
|
|
// 检查蓝牙设备是否支持
|
|
|
|
|
mBtClient!!.checkBluetoothDevice(btType)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -188,9 +199,9 @@ object BtManager {
|
|
|
|
|
object : ServiceConnection {
|
|
|
|
|
override fun onServiceConnected(name: ComponentName, service: IBinder) {
|
|
|
|
|
serverService = (service as ReceiverBinder).service
|
|
|
|
|
serverService?.setMsgReceiveListener(object : MsgReceiverListener {
|
|
|
|
|
serverService?.setMsgReceiveListener(object : BtMsgReceiverListener {
|
|
|
|
|
override fun onMsgReceive(msg: ByteArray) {
|
|
|
|
|
msgReceiverListener?.onMsgReceive(msg)
|
|
|
|
|
btMsgReceiverListener?.onMsgReceive(msg)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
btServiceListener?.onServiceReady()
|
|
|
|
@ -208,8 +219,8 @@ object BtManager {
|
|
|
|
|
override fun onServiceConnected(name: ComponentName, service: IBinder) {
|
|
|
|
|
classicServerService = (service as BTCReceiverService.ClassicReceiverBinder).service
|
|
|
|
|
classicServerService?.setMsgReceiveListener(object : BtMsgListener() {
|
|
|
|
|
override fun socketNotify(state: Int, msg: String) {
|
|
|
|
|
msgReceiverListener?.onMsgReceive(msg.toByteArray())
|
|
|
|
|
override fun messageReceive(state: Int, msg: String) {
|
|
|
|
|
btMsgReceiverListener?.onMsgReceive(msg.toByteArray())
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
btServiceListener?.onServiceReady()
|
|
|
|
@ -225,33 +236,29 @@ object BtManager {
|
|
|
|
|
private val bleClientConn: ServiceConnection by lazy {
|
|
|
|
|
object : ServiceConnection {
|
|
|
|
|
override fun onServiceConnected(name: ComponentName, service: IBinder) {
|
|
|
|
|
clientService = (service as BLEClientBinder).service
|
|
|
|
|
clientService?.setClientListener(object : BLEClientListener {
|
|
|
|
|
bleClientService = (service as BLEClientBinder).service
|
|
|
|
|
bleClientService?.setClientListener(object : BLEClientListener {
|
|
|
|
|
override fun onResult(result: CommonMsg) {
|
|
|
|
|
// 如果连接成功,更新连接的设备
|
|
|
|
|
if (result.msgType == BtConstants.CONNECT_SUCCESS) {
|
|
|
|
|
curConnectMac = result.msg
|
|
|
|
|
btStatusListener?.onConnected(result.msg)
|
|
|
|
|
} else if (result.msgType == BtConstants.DISCONNECT) {
|
|
|
|
|
curConnectMac = ""
|
|
|
|
|
btStatusListener?.onDisconnect("")
|
|
|
|
|
}
|
|
|
|
|
clientListener?.onResult(result)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onNotifyMsgReceive(msg: ByteArray) {
|
|
|
|
|
clientListener?.onNotifyMsgReceive(msg)
|
|
|
|
|
btMsgReceiverListener?.onMsgReceive(msg)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
btServiceListener?.onServiceReady()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onServiceDisconnected(name: ComponentName) {
|
|
|
|
|
clientService = null
|
|
|
|
|
clientListener?.onResult(
|
|
|
|
|
CommonMsg(
|
|
|
|
|
BtConstants.DISCONNECT,
|
|
|
|
|
"disconnect from server"
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
bleClientService = null
|
|
|
|
|
btStatusListener?.onDisconnect("disconnect from server")
|
|
|
|
|
btServiceListener?.onServiceDisConnected()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -262,23 +269,22 @@ object BtManager {
|
|
|
|
|
override fun onServiceConnected(name: ComponentName, service: IBinder) {
|
|
|
|
|
classicClientService = (service as BTCClientService.ClassicClientBinder).service
|
|
|
|
|
classicClientService?.setMsgReceiveListener(object : BtMsgListener() {
|
|
|
|
|
override fun socketNotify(state: Int, msg: String) {
|
|
|
|
|
override fun messageReceive(state: Int, msg: String) {
|
|
|
|
|
when (state) {
|
|
|
|
|
CONNECTED -> {
|
|
|
|
|
curConnectMac = msg
|
|
|
|
|
clientListener?.onResult(
|
|
|
|
|
CommonMsg(
|
|
|
|
|
BtConstants.CONNECT_SUCCESS,
|
|
|
|
|
msg
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
btStatusListener?.onConnected(msg)
|
|
|
|
|
}
|
|
|
|
|
DISCONNECTED -> {
|
|
|
|
|
curConnectMac = ""
|
|
|
|
|
clientListener?.onResult(CommonMsg(BtConstants.DISCONNECT, msg))
|
|
|
|
|
btStatusListener?.onDisconnect(msg)
|
|
|
|
|
}
|
|
|
|
|
MSG -> {
|
|
|
|
|
clientListener?.onNotifyMsgReceive(msg.toByteArray())
|
|
|
|
|
btMsgReceiverListener?.onMsgReceive(msg.toByteArray())
|
|
|
|
|
}
|
|
|
|
|
CONN_ERROR -> {
|
|
|
|
|
curConnectMac = ""
|
|
|
|
|
btStatusListener?.onDisconnect(msg)
|
|
|
|
|
}
|
|
|
|
|
else -> {
|
|
|
|
|
e(TAG, "got error state:$state")
|
|
|
|
@ -302,6 +308,7 @@ object BtManager {
|
|
|
|
|
* @param btType 蓝牙类型
|
|
|
|
|
*/
|
|
|
|
|
fun searchBtDevice(btType: BLUETOOTH_TYPE) {
|
|
|
|
|
d(TAG, "start search $btType")
|
|
|
|
|
if (btType === BLUETOOTH_TYPE.CLASSIC) {
|
|
|
|
|
val mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
|
|
|
|
|
if (mBluetoothAdapter != null) {
|
|
|
|
@ -315,31 +322,76 @@ object BtManager {
|
|
|
|
|
}
|
|
|
|
|
} else if (btType === BLUETOOTH_TYPE.BLE) {
|
|
|
|
|
d(TAG, "ble search start")
|
|
|
|
|
mBtClient!!.search(3000, true)
|
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
|
.subscribe(object : Observer<BluetoothDevice> {
|
|
|
|
|
override fun onSubscribe(d: Disposable) {}
|
|
|
|
|
override fun onNext(bleDevice: BluetoothDevice) {
|
|
|
|
|
d(TAG, "ble onScanResult")
|
|
|
|
|
}
|
|
|
|
|
if (mBtClient != null) {
|
|
|
|
|
mBtClient!!.search(3000, true)
|
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
|
.subscribe(object : Observer<BluetoothDevice> {
|
|
|
|
|
override fun onSubscribe(d: Disposable) {
|
|
|
|
|
d(TAG, "ble onSubscribe")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onError(e: Throwable) {
|
|
|
|
|
d(TAG, "ble onScanFailed")
|
|
|
|
|
}
|
|
|
|
|
override fun onNext(bleDevice: BluetoothDevice) {
|
|
|
|
|
d(TAG, "ble onScanResult")
|
|
|
|
|
bleScanListener?.onResult(bleDevice)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onComplete() {}
|
|
|
|
|
})
|
|
|
|
|
override fun onError(e: Throwable) {
|
|
|
|
|
d(TAG, "ble onScanFailed")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onComplete() {
|
|
|
|
|
d(TAG, "ble onComplete")
|
|
|
|
|
bleScanListener?.onComplete()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 连接设备(BLE的连接方式)
|
|
|
|
|
* 停止扫描蓝牙设备
|
|
|
|
|
*
|
|
|
|
|
* @param btType 蓝牙类型
|
|
|
|
|
*/
|
|
|
|
|
fun stopSearch(btType: BLUETOOTH_TYPE) {
|
|
|
|
|
d(TAG, "stop search $btType")
|
|
|
|
|
mBtClient?.stopSearch()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 连接设备
|
|
|
|
|
*
|
|
|
|
|
* @param mac MAC地址
|
|
|
|
|
*/
|
|
|
|
|
fun connect(mac: String) {
|
|
|
|
|
if (clientService != null) {
|
|
|
|
|
clientService!!.connect(mac, true)
|
|
|
|
|
connect(mac, true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 连接设备
|
|
|
|
|
*
|
|
|
|
|
* @param mac MAC地址
|
|
|
|
|
* @param enableNotify 是否注册通知
|
|
|
|
|
*/
|
|
|
|
|
fun connect(mac: String, enableNotify: Boolean) {
|
|
|
|
|
connect(mac, enableNotify, null)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 连接设备
|
|
|
|
|
*
|
|
|
|
|
* @param mac MAC地址
|
|
|
|
|
* @param enableNotify 是否注册通知
|
|
|
|
|
* @param btConnectListener 链接回调
|
|
|
|
|
*/
|
|
|
|
|
fun connect(mac: String, enableNotify: Boolean, btConnectListener: BtConnectListener?) {
|
|
|
|
|
if (TextUtils.isEmpty(mac)) {
|
|
|
|
|
e(TAG, "connect error with empty mac")
|
|
|
|
|
btConnectListener?.onFail("error mac address")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (bleClientService != null) {
|
|
|
|
|
bleClientService!!.connect(mac, enableNotify, btConnectListener)
|
|
|
|
|
} else {
|
|
|
|
|
w(TAG, "init clientService first")
|
|
|
|
|
}
|
|
|
|
@ -352,15 +404,21 @@ object BtManager {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 连接设备(BLE的连接方式)
|
|
|
|
|
* 断开连接
|
|
|
|
|
*
|
|
|
|
|
* @param mac MAC地址
|
|
|
|
|
* @param mac mac地址
|
|
|
|
|
* @param btType 蓝牙类型
|
|
|
|
|
*/
|
|
|
|
|
fun connect(mac: String, enableNotify: Boolean) {
|
|
|
|
|
if (clientService == null) {
|
|
|
|
|
e(TAG, "writeMsg pls init first")
|
|
|
|
|
} else {
|
|
|
|
|
clientService!!.connect(mac, enableNotify)
|
|
|
|
|
fun disconnect(mac: String, btType: BLUETOOTH_TYPE) {
|
|
|
|
|
if (TextUtils.isEmpty(mac)) {
|
|
|
|
|
e(TAG, "[disConnect] error with empty mac")
|
|
|
|
|
// TODO 添加断开失败回调
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (btType == BLUETOOTH_TYPE.CLASSIC) {
|
|
|
|
|
classicClientService?.disconnect(mac)
|
|
|
|
|
} else if (btType == BLUETOOTH_TYPE.BLE) {
|
|
|
|
|
bleClientService?.disconnect(mac)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -380,21 +438,24 @@ object BtManager {
|
|
|
|
|
* @param msg 传输的内容
|
|
|
|
|
*/
|
|
|
|
|
fun write(msg: ByteArray) {
|
|
|
|
|
if (clientService != null) {
|
|
|
|
|
clientService!!.write(msg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (classicClientService != null) {
|
|
|
|
|
classicClientService!!.write(msg)
|
|
|
|
|
}
|
|
|
|
|
bleClientService?.write(msg)
|
|
|
|
|
classicClientService?.write(msg)
|
|
|
|
|
classicServerService?.write(msg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (classicServerService != null) {
|
|
|
|
|
classicServerService!!.write(msg)
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 读取外围设备内容
|
|
|
|
|
*
|
|
|
|
|
* NOTE: only support for Ble
|
|
|
|
|
*/
|
|
|
|
|
fun read() {
|
|
|
|
|
bleClientService?.read()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取已配对的设备
|
|
|
|
|
*
|
|
|
|
|
* @return 已配对的设备
|
|
|
|
|
*/
|
|
|
|
|
fun getBoundDevices(): Set<BluetoothDevice>? {
|
|
|
|
|
return mBtClient?.getBondedDevices()
|
|
|
|
|