fun connect(address: String?): Boolean { if (mBluetoothAdapter == null || address == null) { return false } try { if(BluetoothAdapter.checkBluetoothAddress(address)) { device = mBluetoothAdapter?.getRemoteDevice(address) } else { Toast.makeText(this, "Invalid MAC: Address", Toast.LENGTH_LONG).show() }
} catch (e: IllegalArgumentException) {
e.printStackTrace()
} catch (e: IllegalStateException) {
e.printStackTrace()
}
if (device == null) {
return false
}
mBluetoothGatt = if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
device?.connectGatt(
this, false, mGattCallback,
BluetoothDevice.TRANSPORT_LE
)
} else {
device?.connectGatt(this, false, mGattCallback)
}
Log.d(TAG, "Trying to create a new connection.")
mBluetoothDeviceAddress = address
mConnectionState = STATE_CONNECTING
return true
}