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